diff --git a/add-patch.c b/add-patch.c index 173a53241ebf07..70242617ef01e5 100644 --- a/add-patch.c +++ b/add-patch.c @@ -342,7 +342,7 @@ static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk) { struct hunk_header *header = &hunk->header; const char *line = s->plain.buf + hunk->start, *p = line; - char *eol = memchr(p, '\n', s->plain.len - hunk->start); + const char *eol = memchr(p, '\n', s->plain.len - hunk->start); if (!eol) eol = s->plain.buf + s->plain.len; diff --git a/apply.c b/apply.c index 2ee980f8d45102..a5f98b63608b02 100644 --- a/apply.c +++ b/apply.c @@ -4164,7 +4164,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid) */ struct fragment *hunk = p->fragments; static const char heading[] = "-Subproject commit "; - char *preimage; + const char *preimage; if (/* does the patch have only one hunk? */ hunk && !hunk->next && diff --git a/bloom.c b/bloom.c index 77a6fddf720912..a805ac0c296b37 100644 --- a/bloom.c +++ b/bloom.c @@ -501,7 +501,7 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r, struct hashmap_iter iter; for (i = 0; i < diff_queued_diff.nr; i++) { - const char *path = diff_queued_diff.queue[i]->two->path; + char *path = diff_queued_diff.queue[i]->two->path; /* * Add each leading directory of the changed file, i.e. for @@ -523,7 +523,7 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r, free(e); if (!last_slash) - last_slash = (char*)path; + last_slash = path; *last_slash = '\0'; } while (*path); diff --git a/builtin/commit.c b/builtin/commit.c index 450b384435943f..68f3d8d8113bc5 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -933,7 +933,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, logfile); hook_arg1 = "message"; } else if (use_message) { - char *buffer; + const char *buffer; buffer = strstr(use_message_buffer, "\n\n"); if (buffer) strbuf_addstr(&sb, skip_blank_lines(buffer + 2)); diff --git a/builtin/config.c b/builtin/config.c index 7c4857be622904..cf4ba0f7cc6f22 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -838,6 +838,7 @@ static int get_urlmatch(const struct config_location_options *opts, const char *var, const char *url) { int ret; + char *section; char *section_tail; struct config_display_options display_opts = *_display_opts; struct string_list_item *item; @@ -851,8 +852,8 @@ static int get_urlmatch(const struct config_location_options *opts, if (!url_normalize(url, &config.url)) die("%s", config.url.err); - config.section = xstrdup_tolower(var); - section_tail = strchr(config.section, '.'); + config.section = section = xstrdup_tolower(var); + section_tail = strchr(section, '.'); if (section_tail) { *section_tail = '\0'; config.key = section_tail + 1; @@ -886,7 +887,7 @@ static int get_urlmatch(const struct config_location_options *opts, string_list_clear(&values, 1); free(config.url.url); - free((void *)config.section); + free(section); return ret; } diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 9c491746168a6f..7a880835663376 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -393,7 +393,7 @@ struct command { static void proc_receive_ref_append(const char *prefix) { struct proc_receive_ref *ref_pattern; - char *p; + const char *p; int len; CALLOC_ARRAY(ref_pattern, 1); @@ -989,8 +989,8 @@ static int read_proc_receive_report(struct packet_reader *reader, for (;;) { struct object_id old_oid, new_oid; - const char *head; - const char *refname; + char *head; + char *refname; char *p; enum packet_read_status status; @@ -1014,7 +1014,8 @@ static int read_proc_receive_report(struct packet_reader *reader, } *p++ = '\0'; if (!strcmp(head, "option")) { - const char *key, *val; + char *key; + const char *val; if (!hint || !(report || new_report)) { if (!once++) diff --git a/builtin/remote.c b/builtin/remote.c index 7ffc14ba15743a..ace390c671d61c 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -332,7 +332,7 @@ static int config_read_branches(const char *key, const char *value, info->remote_name = xstrdup(value); break; case MERGE: { - char *space = strchr(value, ' '); + const char *space = strchr(value, ' '); value = abbrev_branch(value); while (space) { char *merge; diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 9032cc6327c004..7fe7f1e9b5b5f7 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -267,21 +267,20 @@ static int show_file(const char *arg, int output_prefix) static int try_difference(const char *arg) { - char *dotdot; + const char *dotdot; struct object_id start_oid; struct object_id end_oid; const char *end; const char *start; + char *to_free; int symmetric; static const char head_by_default[] = "HEAD"; if (!(dotdot = strstr(arg, ".."))) return 0; + start = to_free = xmemdupz(arg, dotdot - arg); end = dotdot + 2; - start = arg; symmetric = (*end == '.'); - - *dotdot = 0; end += symmetric; if (!*end) @@ -295,7 +294,7 @@ static int try_difference(const char *arg) * Just ".."? That is not a range but the * pathspec for the parent directory. */ - *dotdot = '.'; + free(to_free); return 0; } @@ -308,7 +307,7 @@ static int try_difference(const char *arg) a = lookup_commit_reference(the_repository, &start_oid); b = lookup_commit_reference(the_repository, &end_oid); if (!a || !b) { - *dotdot = '.'; + free(to_free); return 0; } if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) @@ -318,16 +317,16 @@ static int try_difference(const char *arg) show_rev(REVERSED, &commit->object.oid, NULL); } } - *dotdot = '.'; + free(to_free); return 1; } - *dotdot = '.'; + free(to_free); return 0; } static int try_parent_shorthands(const char *arg) { - char *dotdot; + const char *mark; struct object_id oid; struct commit *commit; struct commit_list *parents; @@ -335,38 +334,39 @@ static int try_parent_shorthands(const char *arg) int include_rev = 0; int include_parents = 0; int exclude_parent = 0; + char *to_free; - if ((dotdot = strstr(arg, "^!"))) { + if ((mark = strstr(arg, "^!"))) { include_rev = 1; - if (dotdot[2]) + if (mark[2]) return 0; - } else if ((dotdot = strstr(arg, "^@"))) { + } else if ((mark = strstr(arg, "^@"))) { include_parents = 1; - if (dotdot[2]) + if (mark[2]) return 0; - } else if ((dotdot = strstr(arg, "^-"))) { + } else if ((mark = strstr(arg, "^-"))) { include_rev = 1; exclude_parent = 1; - if (dotdot[2]) { + if (mark[2]) { char *end; - exclude_parent = strtoul(dotdot + 2, &end, 10); + exclude_parent = strtoul(mark + 2, &end, 10); if (*end != '\0' || !exclude_parent) return 0; } } else return 0; - *dotdot = 0; + arg = to_free = xmemdupz(arg, mark - arg); if (repo_get_oid_committish(the_repository, arg, &oid) || !(commit = lookup_commit_reference(the_repository, &oid))) { - *dotdot = '^'; + free(to_free); return 0; } if (exclude_parent && exclude_parent > commit_list_count(commit->parents)) { - *dotdot = '^'; + free(to_free); return 0; } @@ -387,7 +387,7 @@ static int try_parent_shorthands(const char *arg) free(name); } - *dotdot = '^'; + free(to_free); return 1; } diff --git a/builtin/shortlog.c b/builtin/shortlog.c index b91acf45c8fe59..d80bf1a7d055fc 100644 --- a/builtin/shortlog.c +++ b/builtin/shortlog.c @@ -76,7 +76,7 @@ static void insert_one_record(struct shortlog *log, if (!eol) eol = oneline + strlen(oneline); if (starts_with(oneline, "[PATCH")) { - char *eob = strchr(oneline, ']'); + const char *eob = strchr(oneline, ']'); if (eob && (!eol || eob < eol)) oneline = eob + 1; } diff --git a/config.c b/config.c index 83be9642199b18..e1f329ffb02ff6 100644 --- a/config.c +++ b/config.c @@ -162,7 +162,7 @@ static int handle_path_include(const struct key_value_info *kvi, * based on the including config file. */ if (!is_absolute_path(path)) { - char *slash; + const char *slash; if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE) { ret = error(_("relative config includes must come from files")); diff --git a/convert.c b/convert.c index 7bf10d3178d752..7fb83caacd0b22 100644 --- a/convert.c +++ b/convert.c @@ -1133,7 +1133,8 @@ static int count_ident(const char *cp, unsigned long size) static int ident_to_git(const char *src, size_t len, struct strbuf *buf, int ident) { - char *dst, *dollar; + char *dst; + const char *dollar; if (!ident || (src && !count_ident(src, len))) return 0; @@ -1181,7 +1182,8 @@ static int ident_to_worktree(const char *src, size_t len, struct strbuf *buf, int ident) { struct object_id oid; - char *to_free = NULL, *dollar, *spc; + char *to_free = NULL; + const char *dollar, *spc; int cnt; if (!ident) diff --git a/diff.c b/diff.c index 65d0facc486f7e..43e6752905ba6e 100644 --- a/diff.c +++ b/diff.c @@ -1972,7 +1972,7 @@ static int fn_out_diff_words_write_helper(struct diff_options *o, struct strbuf sb = STRBUF_INIT; while (count) { - char *p = memchr(buf, '\n', count); + const char *p = memchr(buf, '\n', count); if (print) strbuf_addstr(&sb, diff_line_prefix(o)); @@ -3060,7 +3060,7 @@ static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir, struct dirstat_file *f = dir->files; int namelen = strlen(f->name); unsigned long changes; - char *slash; + const char *slash; if (namelen < baselen) break; diff --git a/diffcore-rename.c b/diffcore-rename.c index 7723bc3334e084..d9476db35acbf7 100644 --- a/diffcore-rename.c +++ b/diffcore-rename.c @@ -379,7 +379,7 @@ struct dir_rename_info { static char *get_dirname(const char *filename) { - char *slash = strrchr(filename, '/'); + const char *slash = strrchr(filename, '/'); return slash ? xstrndup(filename, slash - filename) : xstrdup(""); } diff --git a/dir.c b/dir.c index 39f7b3f87db7aa..7a604a8d0d8674 100644 --- a/dir.c +++ b/dir.c @@ -3605,15 +3605,15 @@ int get_sparse_checkout_patterns(struct pattern_list *pl) int remove_path(const char *name) { - char *slash; + const char *last; if (unlink(name) && !is_missing_file_error(errno)) return -1; - slash = strrchr(name, '/'); - if (slash) { + last = strrchr(name, '/'); + if (last) { char *dirs = xstrdup(name); - slash = dirs + (slash - name); + char *slash = dirs + (last - name); do { *slash = '\0'; if (startup_info->original_cwd && diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c index c9085edc40e934..1626667c0dc5c6 100644 --- a/fmt-merge-msg.c +++ b/fmt-merge-msg.c @@ -246,7 +246,8 @@ static void add_branch_desc(struct strbuf *out, const char *name) static void record_person_from_buf(int which, struct string_list *people, const char *buffer) { - char *name_buf, *name, *name_end; + char *name_buf; + const char *name, *name_end; struct string_list_item *elem; const char *field; diff --git a/fsck.c b/fsck.c index 9ae4e3cbbb3634..5573a6f19733e9 100644 --- a/fsck.c +++ b/fsck.c @@ -1026,7 +1026,7 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer, int *tagged_type) { int ret = 0; - char *eol; + const char *eol; struct strbuf sb = STRBUF_INIT; const char *buffer_end = buffer + size; const char *p; diff --git a/git-compat-util.h b/git-compat-util.h index 44cd216c58e5da..08bd78a16d3285 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -348,11 +348,7 @@ static inline int is_path_owned_by_current_uid(const char *path, #endif #ifndef find_last_dir_sep -static inline char *git_find_last_dir_sep(const char *path) -{ - return strrchr(path, '/'); -} -#define find_last_dir_sep git_find_last_dir_sep +#define find_last_dir_sep(path) strrchr((path), '/') #endif #ifndef has_dir_sep @@ -498,6 +494,21 @@ void set_warn_routine(report_fn routine); report_fn get_warn_routine(void); void set_die_is_recursing_routine(int (*routine)(void)); +/* + * Check that an out-parameter is "at least as const as" a matching + * in-parameter. For example, skip_prefix() will return "out" that is a subset + * of "str". So: + * + * const str, const out: ok + * non-const str, const out: ok + * non-const str, non-const out: ok + * const str, non-const out: compile error + * + * See the skip_prefix macro below for an example of use. + */ +#define CONST_OUTPARAM(in, out) \ + ((const char **)(0 ? ((*(out) = (in)),(out)) : (out))) + /* * If the string "str" begins with the string found in "prefix", return true. * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in @@ -514,8 +525,10 @@ void set_die_is_recursing_routine(int (*routine)(void)); * [skip prefix if present, otherwise use whole string] * skip_prefix(name, "refs/heads/", &name); */ -static inline bool skip_prefix(const char *str, const char *prefix, - const char **out) +#define skip_prefix(str, prefix, out) \ + skip_prefix_impl((str), (prefix), CONST_OUTPARAM((str), (out))) +static inline bool skip_prefix_impl(const char *str, const char *prefix, + const char **out) { do { if (!*prefix) { @@ -926,8 +939,10 @@ static inline size_t xsize_t(off_t len) * is done via tolower(), so it is strictly ASCII (no multi-byte characters or * locale-specific conversions). */ -static inline bool skip_iprefix(const char *str, const char *prefix, - const char **out) +#define skip_iprefix(str, prefix, out) \ + skip_iprefix_impl((str), (prefix), CONST_OUTPARAM((str), (out))) +static inline bool skip_iprefix_impl(const char *str, const char *prefix, + const char **out) { do { if (!*prefix) { diff --git a/gpg-interface.c b/gpg-interface.c index 47222bf31b6ef2..87fb6605fba174 100644 --- a/gpg-interface.c +++ b/gpg-interface.c @@ -398,7 +398,7 @@ static void parse_ssh_output(struct signature_check *sigc) { const char *line, *principal, *search; char *to_free; - char *key = NULL; + const char *key; /* * ssh-keygen output should be: diff --git a/help.c b/help.c index 162441c06f8ec9..c1ec9a40cd8007 100644 --- a/help.c +++ b/help.c @@ -873,7 +873,7 @@ struct similar_ref_cb { static int append_similar_ref(const struct reference *ref, void *cb_data) { struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data); - char *branch = strrchr(ref->name, '/') + 1; + const char *branch = strrchr(ref->name, '/') + 1; /* A remote branch of the same name is deemed similar */ if (starts_with(ref->name, "refs/remotes/") && diff --git a/hex.c b/hex.c index 865a232167553d..bc756722ca623b 100644 --- a/hex.c +++ b/hex.c @@ -54,9 +54,9 @@ int get_oid_hex(const char *hex, struct object_id *oid) return get_oid_hex_algop(hex, oid, the_hash_algo); } -int parse_oid_hex_algop(const char *hex, struct object_id *oid, - const char **end, - const struct git_hash_algo *algop) +int parse_oid_hex_algop_impl(const char *hex, struct object_id *oid, + const char **end, + const struct git_hash_algo *algop) { int ret = get_oid_hex_algop(hex, oid, algop); if (!ret) diff --git a/hex.h b/hex.h index e9ccb54065c0bc..1e9a65d83a4f6b 100644 --- a/hex.h +++ b/hex.h @@ -40,8 +40,10 @@ char *oid_to_hex(const struct object_id *oid); /* same static buffer */ * other invalid character. end is only updated on success; otherwise, it is * unmodified. */ -int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end, - const struct git_hash_algo *algo); +#define parse_oid_hex_algop(hex, oid, end, algo) \ + parse_oid_hex_algop_impl((hex), (oid), CONST_OUTPARAM((hex), (end)), (algo)) +int parse_oid_hex_algop_impl(const char *hex, struct object_id *oid, const char **end, + const struct git_hash_algo *algo); /* * These functions work like get_oid_hex and parse_oid_hex, but they will parse diff --git a/hook.c b/hook.c index 9adb7aba23df51..7419a96c035ec3 100644 --- a/hook.c +++ b/hook.c @@ -181,20 +181,14 @@ static char *get_post_index_change_sentinel_name(struct repository *r) { struct strbuf path = STRBUF_INIT; const char *sid = tr2_sid_get(); - char *slash = strchr(sid, '/'); - - /* - * Name is based on top-level SID, so children can indicate that - * the top-level process should run the post-command hook. - */ - if (slash) - *slash = 0; + const char *slash = strchrnul(sid, '/'); /* * Do not write to hooks directory, as it could be redirected * somewhere like the source tree. */ - repo_git_path_replace(r, &path, "info/index-change-%s.snt", sid); + repo_git_path_replace(r, &path, "info/index-change-%.*s.snt", + (int)(slash - sid), sid); return strbuf_detach(&path, NULL); } diff --git a/http-push.c b/http-push.c index cc0f80934615ba..96df6344ee6245 100644 --- a/http-push.c +++ b/http-push.c @@ -99,7 +99,7 @@ static struct object_list *objects; struct repo { char *url; - char *path; + const char *path; int path_len; int has_info_refs; int can_update_info_refs; @@ -1768,7 +1768,7 @@ int cmd_main(int argc, const char **argv) usage(http_push_usage); } if (!repo->url) { - char *path = strstr(arg, "//"); + const char *path = strstr(arg, "//"); str_end_url_with_slash(arg, &repo->url); repo->path_len = strlen(repo->url); if (path) { diff --git a/http.c b/http.c index be1fd6f41e8241..51722a21491af6 100644 --- a/http.c +++ b/http.c @@ -766,7 +766,7 @@ static int has_proxy_cert_password(void) static int redact_sensitive_header(struct strbuf *header, size_t offset) { int ret = 0; - const char *sensitive_header; + char *sensitive_header; if (trace_curl_redact && (skip_iprefix(header->buf + offset, "Authorization:", &sensitive_header) || @@ -783,7 +783,7 @@ static int redact_sensitive_header(struct strbuf *header, size_t offset) } else if (trace_curl_redact && skip_iprefix(header->buf + offset, "Cookie:", &sensitive_header)) { struct strbuf redacted_header = STRBUF_INIT; - const char *cookie; + char *cookie; while (isspace(*sensitive_header)) sensitive_header++; diff --git a/mailinfo.c b/mailinfo.c index 550bd185e34b97..5dd8bd5aab9e10 100644 --- a/mailinfo.c +++ b/mailinfo.c @@ -1141,7 +1141,7 @@ static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf { const char *sp = data->buf; while (1) { - char *ep = strchr(sp, '\n'); + const char *ep = strchr(sp, '\n'); int len; if (!ep) len = strlen(sp); diff --git a/mem-pool.c b/mem-pool.c index 0fab0a5ef26472..89bca70f713692 100644 --- a/mem-pool.c +++ b/mem-pool.c @@ -179,7 +179,7 @@ char *mem_pool_strdup(struct mem_pool *pool, const char *str) char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len) { - char *p = memchr(str, '\0', len); + const char *p = memchr(str, '\0', len); size_t actual_len = (p ? p - str : len); char *ret = mem_pool_alloc(pool, actual_len+1); diff --git a/merge-ort.c b/merge-ort.c index 60f7ad562e3951..0741101267a0fc 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -2731,7 +2731,7 @@ static void apply_directory_rename_modifications(struct merge_options *opt, while (1) { /* Find the parent directory of cur_path */ - char *last_slash = strrchr(cur_path, '/'); + const char *last_slash = strrchr(cur_path, '/'); if (last_slash) { parent_name = mem_pool_strndup(&opt->priv->pool, cur_path, diff --git a/object-name.c b/object-name.c index 1c4216a69517e3..1c4978467cb441 100644 --- a/object-name.c +++ b/object-name.c @@ -1756,7 +1756,7 @@ int repo_interpret_branch_name(struct repository *r, struct strbuf *buf, const struct interpret_branch_name_options *options) { - char *at; + const char *at; const char *start; int len; diff --git a/pack-revindex.c b/pack-revindex.c index 7a9661429168f3..704032e56d00e0 100644 --- a/pack-revindex.c +++ b/pack-revindex.c @@ -544,7 +544,7 @@ static int midx_key_to_pack_pos(struct multi_pack_index *m, struct midx_pack_key *key, uint32_t *pos) { - uint32_t *found; + const uint32_t *found; if (key->pack >= m->num_packs + m->num_packs_in_base) BUG("MIDX pack lookup out of bounds (%"PRIu32" >= %"PRIu32")", diff --git a/pager.c b/pager.c index 5531fff50eb73f..35b210e0484f90 100644 --- a/pager.c +++ b/pager.c @@ -108,10 +108,11 @@ const char *git_pager(struct repository *r, int stdout_is_tty) static void setup_pager_env(struct strvec *env) { - const char **argv; + char **argv; int i; char *pager_env = xstrdup(PAGER_ENV); - int n = split_cmdline(pager_env, &argv); + /* split_cmdline splits in place, so we know the result is writable */ + int n = split_cmdline(pager_env, (const char ***)&argv); if (n < 0) die("malformed build-time PAGER_ENV: %s", diff --git a/pkt-line.c b/pkt-line.c index a2d9c9c656fb50..e479a4c3e13157 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -384,10 +384,10 @@ int packet_length(const char lenbuf_hex[4], size_t size) hexval(lenbuf_hex[3]); } -static char *find_packfile_uri_path(const char *buffer) +static const char *find_packfile_uri_path(const char *buffer) { const char *URI_MARK = "://"; - char *path; + const char *path; int len; /* First char is sideband mark */ @@ -417,7 +417,7 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer, { int len; char linelen[4]; - char *uri_path_start; + const char *uri_path_start; if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) { *pktlen = -1; diff --git a/pkt-line.h b/pkt-line.h index 10fd9a812e1935..5c43e253993983 100644 --- a/pkt-line.h +++ b/pkt-line.h @@ -185,7 +185,7 @@ struct packet_reader { int pktlen; /* the last line read */ - const char *line; + char *line; /* indicates if a line has been peeked */ int line_peeked; diff --git a/pseudo-merge.c b/pseudo-merge.c index a2d5bd85f95ebf..ff18b6c364245e 100644 --- a/pseudo-merge.c +++ b/pseudo-merge.c @@ -638,14 +638,21 @@ static int pseudo_merge_commit_cmp(const void *va, const void *vb) return 0; } -static struct pseudo_merge_commit *find_pseudo_merge(const struct pseudo_merge_map *pm, - uint32_t pos) +static int find_pseudo_merge(const struct pseudo_merge_map *pm, uint32_t pos, + struct pseudo_merge_commit *out) { + const unsigned char *at; + if (!pm->commits_nr) - return NULL; + return 0; - return bsearch(&pos, pm->commits, pm->commits_nr, - PSEUDO_MERGE_COMMIT_RAWSZ, pseudo_merge_commit_cmp); + at = bsearch(&pos, pm->commits, pm->commits_nr, + PSEUDO_MERGE_COMMIT_RAWSZ, pseudo_merge_commit_cmp); + if (!at) + return 0; + + read_pseudo_merge_commit_at(out, at); + return 1; } int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm, @@ -653,16 +660,15 @@ int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm, struct commit *commit, uint32_t commit_pos) { struct pseudo_merge *merge; - struct pseudo_merge_commit *merge_commit; + struct pseudo_merge_commit merge_commit; int ret = 0; - merge_commit = find_pseudo_merge(pm, commit_pos); - if (!merge_commit) + if (!find_pseudo_merge(pm, commit_pos, &merge_commit)) return 0; - if (merge_commit->pseudo_merge_ofs & ((uint64_t)1<<63)) { + if (merge_commit.pseudo_merge_ofs & ((uint64_t)1<<63)) { struct pseudo_merge_commit_ext ext = { 0 }; - off_t ofs = merge_commit->pseudo_merge_ofs & ~((uint64_t)1<<63); + off_t ofs = merge_commit.pseudo_merge_ofs & ~((uint64_t)1<<63); uint32_t i; if (pseudo_merge_ext_at(pm, &ext, ofs) < -1) { @@ -673,11 +679,11 @@ int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm, } for (i = 0; i < ext.nr; i++) { - if (nth_pseudo_merge_ext(pm, &ext, merge_commit, i) < 0) + if (nth_pseudo_merge_ext(pm, &ext, &merge_commit, i) < 0) return ret; merge = pseudo_merge_at(pm, &commit->object.oid, - merge_commit->pseudo_merge_ofs); + merge_commit.pseudo_merge_ofs); if (!merge) return ret; @@ -687,7 +693,7 @@ int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm, } } else { merge = pseudo_merge_at(pm, &commit->object.oid, - merge_commit->pseudo_merge_ofs); + merge_commit.pseudo_merge_ofs); if (!merge) return ret; diff --git a/range-diff.c b/range-diff.c index 57edff40a85f24..155bf0f2531d7c 100644 --- a/range-diff.c +++ b/range-diff.c @@ -88,7 +88,7 @@ static int read_patches(const char *range, struct string_list *list, line = contents.buf; size = contents.len; for (; size > 0; size -= len, line += len) { - const char *p; + char *p; char *eol; eol = memchr(line, '\n', size); diff --git a/ref-filter.c b/ref-filter.c index c318f9ca0ec8dd..db83d3b32c3e20 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2173,32 +2173,34 @@ static inline char *copy_advance(char *dst, const char *src) return dst; } -static const char *lstrip_ref_components(const char *refname, int len) +static int normalize_component_count(const char *refname, int len) { - long remaining = len; - const char *start = xstrdup(refname); - const char *to_free = start; - if (len < 0) { - int i; - const char *p = refname; + int slashes = 0; + + for (const char *p = refname; *p; p++) { + if (*p == '/') + slashes++; + } - /* Find total no of '/' separated path-components */ - for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) - ; /* * The number of components we need to strip is now * the total minus the components to be left (Plus one * because we count the number of '/', but the number * of components is one more than the no of '/'). */ - remaining = i + len + 1; + len = slashes + len + 1; } + return len; +} + +static const char *lstrip_ref_components(const char *refname, int len) +{ + int remaining = normalize_component_count(refname, len); while (remaining > 0) { - switch (*start++) { + switch (*refname++) { case '\0': - free((char *)to_free); return xstrdup(""); case '/': remaining--; @@ -2206,42 +2208,21 @@ static const char *lstrip_ref_components(const char *refname, int len) } } - start = xstrdup(start); - free((char *)to_free); - return start; + return xstrdup(refname); } static const char *rstrip_ref_components(const char *refname, int len) { - long remaining = len; - const char *start = xstrdup(refname); - const char *to_free = start; - - if (len < 0) { - int i; - const char *p = refname; + int remaining = normalize_component_count(refname, len); + const char *end = refname + strlen(refname); - /* Find total no of '/' separated path-components */ - for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) - ; - /* - * The number of components we need to strip is now - * the total minus the components to be left (Plus one - * because we count the number of '/', but the number - * of components is one more than the no of '/'). - */ - remaining = i + len + 1; - } - - while (remaining-- > 0) { - char *p = strrchr(start, '/'); - if (!p) { - free((char *)to_free); + while (remaining > 0) { + if (end == refname) return xstrdup(""); - } else - p[0] = '\0'; + if (*--end == '/') + remaining--; } - return start; + return xmemdupz(refname, end - refname); } static const char *show_ref(struct refname_atom *atom, const char *refname) diff --git a/reflog-walk.c b/reflog-walk.c index 4f1ce047498116..4dbeaa93a7703f 100644 --- a/reflog-walk.c +++ b/reflog-walk.c @@ -157,7 +157,8 @@ int add_reflog_for_walk(struct reflog_walk_info *info, int recno = -1; struct string_list_item *item; struct complete_reflogs *reflogs; - char *branch, *at = strchr(name, '@'); + char *branch; + const char *at = strchr(name, '@'); struct commit_reflog *commit_reflog; enum selector_type selector = SELECTOR_NONE; diff --git a/refs/files-backend.c b/refs/files-backend.c index 80515d3e0833b1..664a62e0d7992f 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -2183,7 +2183,7 @@ static int show_one_reflog_ent(struct files_ref_store *refs, char *email_end, *message; timestamp_t timestamp; int tz; - const char *p = sb->buf; + char *p = sb->buf; /* old SP new SP name SP time TAB msg LF */ if (!sb->len || sb->buf[sb->len - 1] != '\n' || diff --git a/revision.c b/revision.c index 72750f973f1630..219b9b6b9dacd4 100644 --- a/revision.c +++ b/revision.c @@ -2037,41 +2037,32 @@ static void prepare_show_merge(struct rev_info *revs) free(prune); } -static int dotdot_missing(const char *arg, char *dotdot, +static int dotdot_missing(const char *full_name, struct rev_info *revs, int symmetric) { if (revs->ignore_missing) return 0; - /* de-munge so we report the full argument */ - *dotdot = '.'; die(symmetric ? "Invalid symmetric difference expression %s" - : "Invalid revision range %s", arg); + : "Invalid revision range %s", full_name); } -static int handle_dotdot_1(const char *arg, char *dotdot, +static int handle_dotdot_1(const char *a_name, const char *b_name, + const char *full_name, int symmetric, struct rev_info *revs, int flags, int cant_be_filename, struct object_context *a_oc, struct object_context *b_oc) { - const char *a_name, *b_name; struct object_id a_oid, b_oid; struct object *a_obj, *b_obj; unsigned int a_flags, b_flags; - int symmetric = 0; unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM); unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH; - a_name = arg; if (!*a_name) a_name = "HEAD"; - b_name = dotdot + 2; - if (*b_name == '.') { - symmetric = 1; - b_name++; - } if (!*b_name) b_name = "HEAD"; @@ -2080,15 +2071,13 @@ static int handle_dotdot_1(const char *arg, char *dotdot, return -1; if (!cant_be_filename) { - *dotdot = '.'; - verify_non_filename(revs->prefix, arg); - *dotdot = '\0'; + verify_non_filename(revs->prefix, full_name); } a_obj = parse_object(revs->repo, &a_oid); b_obj = parse_object(revs->repo, &b_oid); if (!a_obj || !b_obj) - return dotdot_missing(arg, dotdot, revs, symmetric); + return dotdot_missing(full_name, revs, symmetric); if (!symmetric) { /* just A..B */ @@ -2102,7 +2091,7 @@ static int handle_dotdot_1(const char *arg, char *dotdot, a = lookup_commit_reference(revs->repo, &a_obj->oid); b = lookup_commit_reference(revs->repo, &b_obj->oid); if (!a || !b) - return dotdot_missing(arg, dotdot, revs, symmetric); + return dotdot_missing(full_name, revs, symmetric); if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) { free_commit_list(exclude); @@ -2131,16 +2120,23 @@ static int handle_dotdot(const char *arg, int cant_be_filename) { struct object_context a_oc = {0}, b_oc = {0}; - char *dotdot = strstr(arg, ".."); + const char *dotdot = strstr(arg, ".."); + char *tmp; + int symmetric = 0; int ret; if (!dotdot) return -1; - *dotdot = '\0'; - ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename, - &a_oc, &b_oc); - *dotdot = '.'; + tmp = xmemdupz(arg, dotdot - arg); + dotdot += 2; + if (*dotdot == '.') { + symmetric = 1; + dotdot++; + } + ret = handle_dotdot_1(tmp, dotdot, arg, symmetric, revs, flags, + cant_be_filename, &a_oc, &b_oc); + free(tmp); object_context_release(&a_oc); object_context_release(&b_oc); @@ -2150,7 +2146,10 @@ static int handle_dotdot(const char *arg, static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt) { struct object_context oc = {0}; - char *mark; + const char *mark; + char *arg_minus_at = NULL; + char *arg_minus_excl = NULL; + char *arg_minus_dash = NULL; struct object *object; struct object_id oid; int local_flags; @@ -2177,18 +2176,17 @@ static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int fl mark = strstr(arg, "^@"); if (mark && !mark[2]) { - *mark = 0; - if (add_parents_only(revs, arg, flags, 0)) { + arg_minus_at = xmemdupz(arg, mark - arg); + if (add_parents_only(revs, arg_minus_at, flags, 0)) { ret = 0; goto out; } - *mark = '^'; } mark = strstr(arg, "^!"); if (mark && !mark[2]) { - *mark = 0; - if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0)) - *mark = '^'; + arg_minus_excl = xmemdupz(arg, mark - arg); + if (add_parents_only(revs, arg_minus_excl, flags ^ (UNINTERESTING | BOTTOM), 0)) + arg = arg_minus_excl; } mark = strstr(arg, "^-"); if (mark) { @@ -2202,9 +2200,9 @@ static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int fl } } - *mark = 0; - if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent)) - *mark = '^'; + arg_minus_dash = xmemdupz(arg, mark - arg); + if (add_parents_only(revs, arg_minus_dash, flags ^ (UNINTERESTING | BOTTOM), exclude_parent)) + arg = arg_minus_dash; } local_flags = 0; @@ -2239,6 +2237,9 @@ static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int fl out: object_context_release(&oc); + free(arg_minus_at); + free(arg_minus_excl); + free(arg_minus_dash); return ret; } diff --git a/run-command.c b/run-command.c index 497883a7defd62..a6958e8885d046 100644 --- a/run-command.c +++ b/run-command.c @@ -606,11 +606,11 @@ static void trace_add_env(struct strbuf *dst, const char *const *deltaenv) /* Last one wins, see run-command.c:prep_childenv() for context */ for (e = deltaenv; e && *e; e++) { struct strbuf key = STRBUF_INIT; - char *equals = strchr(*e, '='); + const char *equals = strchr(*e, '='); if (equals) { strbuf_add(&key, *e, equals - *e); - string_list_insert(&envs, key.buf)->util = equals + 1; + string_list_insert(&envs, key.buf)->util = (void *)(equals + 1); } else { string_list_insert(&envs, *e)->util = NULL; } diff --git a/scalar.c b/scalar.c index 0e73108f9d598a..d4d6fa839c8691 100644 --- a/scalar.c +++ b/scalar.c @@ -697,7 +697,7 @@ static int delete_enlistment(struct strbuf *enlistment) { struct strbuf parent = STRBUF_INIT; size_t offset; - char *path_sep; + const char *path_sep; if (unregister_dir()) return error(_("failed to unregister repository")); diff --git a/send-pack.c b/send-pack.c index a85d5695d75947..2bd7a8df3831df 100644 --- a/send-pack.c +++ b/send-pack.c @@ -178,8 +178,8 @@ static int receive_status(struct repository *r, ret = receive_unpack_status(reader); while (1) { struct object_id old_oid, new_oid; - const char *head; - const char *refname; + char *head; + char *refname; char *p; if (packet_reader_read(reader) != PACKET_READ_NORMAL) break; @@ -193,7 +193,8 @@ static int receive_status(struct repository *r, *p++ = '\0'; if (!strcmp(head, "option")) { - const char *key, *val; + char *key; + const char *val; if (!hint || !(report || new_report)) { if (!once++) diff --git a/strbuf.c b/strbuf.c index 59678bf5b03e0b..3939863cf31ffd 100644 --- a/strbuf.c +++ b/strbuf.c @@ -1119,6 +1119,6 @@ void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix) void strbuf_strip_file_from_path(struct strbuf *sb) { - char *path_sep = find_last_dir_sep(sb->buf); + const char *path_sep = find_last_dir_sep(sb->buf); strbuf_setlen(sb, path_sep ? path_sep - sb->buf + 1 : 0); } diff --git a/string-list.c b/string-list.c index 08dc00984ccbd6..7c34a425da5d0a 100644 --- a/string-list.c +++ b/string-list.c @@ -327,7 +327,7 @@ static int split_string(struct string_list *list, const char *string, const char BUG("string_list_split() called without strdup_strings"); for (;;) { - char *end; + const char *end; if (flags & STRING_LIST_SPLIT_TRIM) { /* ltrim */ diff --git a/t/unit-tests/clar/clar/print.h b/t/unit-tests/clar/clar/print.h index 6a2321b399d192..59b7dc14a104a8 100644 --- a/t/unit-tests/clar/clar/print.h +++ b/t/unit-tests/clar/clar/print.h @@ -127,7 +127,7 @@ static void clar_print_tap_error(int num, const struct clar_report *report, cons static void print_escaped(const char *str) { - char *c; + const char *c; while ((c = strchr(str, '\'')) != NULL) { printf("%.*s", (int)(c - str), str); diff --git a/transport-helper.c b/transport-helper.c index af0089ce9a8246..9e1793138c4bbf 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -807,7 +807,8 @@ static int push_update_ref_status(struct strbuf *buf, if (starts_with(buf->buf, "option ")) { struct object_id old_oid, new_oid; - const char *key, *val; + char *key; + const char *val; char *p; if (!state->hint || !(state->report || state->new_report)) diff --git a/transport.c b/transport.c index 39bb387bc10fcd..8744d14a9a9b82 100644 --- a/transport.c +++ b/transport.c @@ -1658,7 +1658,7 @@ int transport_disconnect(struct transport *transport) */ char *transport_anonymize_url(const char *url) { - char *scheme_prefix, *anon_part; + const char *scheme_prefix, *anon_part; size_t anon_len, prefix_len = 0; anon_part = strchr(url, '@'); diff --git a/virtualfilesystem.c b/virtualfilesystem.c index 269af2de1d969d..b3adf9a0894b35 100644 --- a/virtualfilesystem.c +++ b/virtualfilesystem.c @@ -148,7 +148,7 @@ int is_included_in_virtualfilesystem(const char *pathname, int pathlen) static void parent_directory_hashmap_add(struct hashmap *map, const char *pattern, const int patternlen) { - char *slash; + const char *slash; struct virtualfilesystem *vfs; /* diff --git a/wrapper.c b/wrapper.c index d7fe98098fa6bc..5d1a81ae55777d 100644 --- a/wrapper.c +++ b/wrapper.c @@ -115,7 +115,7 @@ void *xmemdupz(const void *data, size_t len) char *xstrndup(const char *str, size_t len) { - char *p = memchr(str, '\0', len); + const char *p = memchr(str, '\0', len); return xmemdupz(str, p ? p - str : len); }