global: constify some pointers that are not written to

The recent glibc 2.43 release had the following change listed in its NEWS file: For ISO C23, the functions bsearch, memchr, strchr, strpbrk, strrchr, strstr, wcschr, wcspbrk, wcsrchr, wcsstr and wmemchr that return pointers into their input arrays now have definitions as macros that return a pointer to a const-qualified type when the input argument is a pointer to a const-qualified type. When compiling with GCC 15, which defaults to -std=gnu23, this causes many warnings like this: merge-ort.c: In function ‘apply_directory_rename_modifications’: merge-ort.c:2734:36: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers] 2734 | char *last_slash = strrchr(cur_path, '/'); | ^~~~~~~ This patch fixes the more obvious ones by making them const when we do not write to the returned pointer. Signed-off-by: Collin Funk <collin.funk1@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Collin Funk committed Feb 5, 2026 at 17:46 UTC 4ac4705afa3ab660e206c2b870bfae2ddb647ffa
28 files changed +34 -31
add-patch.c
+1 -1
@@ -342,7 +342,7 @@ static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk)
342 {
343 struct hunk_header *header = &hunk->header;
344 const char *line = s->plain.buf + hunk->start, *p = line;
345 - char *eol = memchr(p, '\n', s->plain.len - hunk->start);
345 + const char *eol = memchr(p, '\n', s->plain.len - hunk->start);
346
347 if (!eol)
348 eol = s->plain.buf + s->plain.len;
apply.c
+1 -1
@@ -4144,7 +4144,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
4144 */
4145 struct fragment *hunk = p->fragments;
4146 static const char heading[] = "-Subproject commit ";
4147 - char *preimage;
4147 + const char *preimage;
4148
4149 if (/* does the patch have only one hunk? */
4150 hunk && !hunk->next &&
builtin/commit.c
+1 -1
@@ -816,7 +816,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
816 logfile);
817 hook_arg1 = "message";
818 } else if (use_message) {
819 - char *buffer;
819 + const char *buffer;
820 buffer = strstr(use_message_buffer, "\n\n");
821 if (buffer)
822 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
builtin/receive-pack.c
+1 -1
@@ -393,7 +393,7 @@ struct command {
393 static void proc_receive_ref_append(const char *prefix)
394 {
395 struct proc_receive_ref *ref_pattern;
396 - char *p;
396 + const char *p;
397 int len;
398
399 CALLOC_ARRAY(ref_pattern, 1);
builtin/remote.c
+1 -1
@@ -332,7 +332,7 @@ static int config_read_branches(const char *key, const char *value,
332 info->remote_name = xstrdup(value);
333 break;
334 case MERGE: {
335 - char *space = strchr(value, ' ');
335 + const char *space = strchr(value, ' ');
336 value = abbrev_branch(value);
337 while (space) {
338 char *merge;
builtin/shortlog.c
+1 -1
@@ -76,7 +76,7 @@ static void insert_one_record(struct shortlog *log,
76 if (!eol)
77 eol = oneline + strlen(oneline);
78 if (starts_with(oneline, "[PATCH")) {
79 - char *eob = strchr(oneline, ']');
79 + const char *eob = strchr(oneline, ']');
80 if (eob && (!eol || eob < eol))
81 oneline = eob + 1;
82 }
config.c
+1 -1
@@ -160,7 +160,7 @@ static int handle_path_include(const struct key_value_info *kvi,
160 * based on the including config file.
161 */
162 if (!is_absolute_path(path)) {
163 - char *slash;
163 + const char *slash;
164
165 if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE) {
166 ret = error(_("relative config includes must come from files"));
convert.c
+2 -1
@@ -1122,7 +1122,8 @@ static int count_ident(const char *cp, unsigned long size)
1122 static int ident_to_git(const char *src, size_t len,
1123 struct strbuf *buf, int ident)
1124 {
1125 - char *dst, *dollar;
1125 + char *dst;
1126 + const char *dollar;
1127
1128 if (!ident || (src && !count_ident(src, len)))
1129 return 0;
diff.c
+2 -2
@@ -1961,7 +1961,7 @@ static int fn_out_diff_words_write_helper(struct diff_options *o,
1961 struct strbuf sb = STRBUF_INIT;
1962
1963 while (count) {
1964 - char *p = memchr(buf, '\n', count);
1964 + const char *p = memchr(buf, '\n', count);
1965 if (print)
1966 strbuf_addstr(&sb, diff_line_prefix(o));
1967
@@ -3049,7 +3049,7 @@ static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
3049 struct dirstat_file *f = dir->files;
3050 int namelen = strlen(f->name);
3051 unsigned long changes;
3052 - char *slash;
3052 + const char *slash;
3053
3054 if (namelen < baselen)
3055 break;
diffcore-rename.c
+1 -1
@@ -379,7 +379,7 @@ struct dir_rename_info {
379
380 static char *get_dirname(const char *filename)
381 {
382 - char *slash = strrchr(filename, '/');
382 + const char *slash = strrchr(filename, '/');
383 return slash ? xstrndup(filename, slash - filename) : xstrdup("");
384 }
385
fmt-merge-msg.c
+2 -1
@@ -246,7 +246,8 @@ static void add_branch_desc(struct strbuf *out, const char *name)
246 static void record_person_from_buf(int which, struct string_list *people,
247 const char *buffer)
248 {
249 - char *name_buf, *name, *name_end;
249 + char *name_buf;
250 + const char *name, *name_end;
251 struct string_list_item *elem;
252 const char *field;
253
fsck.c
+1 -1
@@ -1026,7 +1026,7 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
1026 int *tagged_type)
1027 {
1028 int ret = 0;
1029 - char *eol;
1029 + const char *eol;
1030 struct strbuf sb = STRBUF_INIT;
1031 const char *buffer_end = buffer + size;
1032 const char *p;
gpg-interface.c
+1 -1
@@ -398,7 +398,7 @@ static void parse_ssh_output(struct signature_check *sigc)
398 {
399 const char *line, *principal, *search;
400 char *to_free;
401 - char *key = NULL;
401 + const char *key = NULL;
402
403 /*
404 * ssh-keygen output should be:
help.c
+1 -1
@@ -857,7 +857,7 @@ struct similar_ref_cb {
857 static int append_similar_ref(const struct reference *ref, void *cb_data)
858 {
859 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
860 - char *branch = strrchr(ref->name, '/') + 1;
860 + const char *branch = strrchr(ref->name, '/') + 1;
861
862 /* A remote branch of the same name is deemed similar */
863 if (starts_with(ref->name, "refs/remotes/") &&
http-push.c
+1 -1
@@ -1768,7 +1768,7 @@ int cmd_main(int argc, const char **argv)
1768 usage(http_push_usage);
1769 }
1770 if (!repo->url) {
1771 - char *path = strstr(arg, "//");
1771 + const char *path = strstr(arg, "//");
1772 str_end_url_with_slash(arg, &repo->url);
1773 repo->path_len = strlen(repo->url);
1774 if (path) {
mailinfo.c
+1 -1
@@ -1141,7 +1141,7 @@ static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf
1141 {
1142 const char *sp = data->buf;
1143 while (1) {
1144 - char *ep = strchr(sp, '\n');
1144 + const char *ep = strchr(sp, '\n');
1145 int len;
1146 if (!ep)
1147 len = strlen(sp);
mem-pool.c
+1 -1
@@ -169,7 +169,7 @@ char *mem_pool_strdup(struct mem_pool *pool, const char *str)
169
170 char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
171 {
172 - char *p = memchr(str, '\0', len);
172 + const char *p = memchr(str, '\0', len);
173 size_t actual_len = (p ? p - str : len);
174 char *ret = mem_pool_alloc(pool, actual_len+1);
175
merge-ort.c
+1 -1
@@ -2731,7 +2731,7 @@ static void apply_directory_rename_modifications(struct merge_options *opt,
2731
2732 while (1) {
2733 /* Find the parent directory of cur_path */
2734 - char *last_slash = strrchr(cur_path, '/');
2734 + const char *last_slash = strrchr(cur_path, '/');
2735 if (last_slash) {
2736 parent_name = mem_pool_strndup(&opt->priv->pool,
2737 cur_path,
object-name.c
+1 -1
@@ -1756,7 +1756,7 @@ int repo_interpret_branch_name(struct repository *r,
1756 struct strbuf *buf,
1757 const struct interpret_branch_name_options *options)
1758 {
1759 - char *at;
1759 + const char *at;
1760 const char *start;
1761 int len;
1762
pack-revindex.c
+1 -1
@@ -544,7 +544,7 @@ static int midx_key_to_pack_pos(struct multi_pack_index *m,
544 struct midx_pack_key *key,
545 uint32_t *pos)
546 {
547 - uint32_t *found;
547 + const uint32_t *found;
548
549 if (key->pack >= m->num_packs + m->num_packs_in_base)
550 BUG("MIDX pack lookup out of bounds (%"PRIu32" >= %"PRIu32")",
pkt-line.c
+3 -3
@@ -384,10 +384,10 @@ int packet_length(const char lenbuf_hex[4], size_t size)
384 hexval(lenbuf_hex[3]);
385 }
386
387 -static char *find_packfile_uri_path(const char *buffer)
387 +static const char *find_packfile_uri_path(const char *buffer)
388 {
389 const char *URI_MARK = "://";
390 - char *path;
390 + const char *path;
391 int len;
392
393 /* First char is sideband mark */
@@ -417,7 +417,7 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
417 {
418 int len;
419 char linelen[4];
420 - char *uri_path_start;
420 + const char *uri_path_start;
421
422 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
423 *pktlen = -1;
reflog-walk.c
+2 -1
@@ -157,7 +157,8 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
157 int recno = -1;
158 struct string_list_item *item;
159 struct complete_reflogs *reflogs;
160 - char *branch, *at = strchr(name, '@');
160 + char *branch;
161 + const char *at = strchr(name, '@');
162 struct commit_reflog *commit_reflog;
163 enum selector_type selector = SELECTOR_NONE;
164
scalar.c
+1 -1
@@ -393,7 +393,7 @@ static int delete_enlistment(struct strbuf *enlistment)
393 {
394 struct strbuf parent = STRBUF_INIT;
395 size_t offset;
396 - char *path_sep;
396 + const char *path_sep;
397
398 if (unregister_dir())
399 return error(_("failed to unregister repository"));
strbuf.c
+1 -1
@@ -1119,6 +1119,6 @@ void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix)
1119
1120 void strbuf_strip_file_from_path(struct strbuf *sb)
1121 {
1122 - char *path_sep = find_last_dir_sep(sb->buf);
1122 + const char *path_sep = find_last_dir_sep(sb->buf);
1123 strbuf_setlen(sb, path_sep ? path_sep - sb->buf + 1 : 0);
1124 }
string-list.c
+1 -1
@@ -327,7 +327,7 @@ static int split_string(struct string_list *list, const char *string, const char
327 BUG("string_list_split() called without strdup_strings");
328
329 for (;;) {
330 - char *end;
330 + const char *end;
331
332 if (flags & STRING_LIST_SPLIT_TRIM) {
333 /* ltrim */
t/unit-tests/clar/clar/print.h
+1 -1
@@ -127,7 +127,7 @@ static void clar_print_tap_error(int num, const struct clar_report *report, cons
127
128 static void print_escaped(const char *str)
129 {
130 - char *c;
130 + const char *c;
131
132 while ((c = strchr(str, '\'')) != NULL) {
133 printf("%.*s", (int)(c - str), str);
transport.c
+1 -1
@@ -1657,7 +1657,7 @@ int transport_disconnect(struct transport *transport)
1657 */
1658 char *transport_anonymize_url(const char *url)
1659 {
1660 - char *scheme_prefix, *anon_part;
1660 + const char *scheme_prefix, *anon_part;
1661 size_t anon_len, prefix_len = 0;
1662
1663 anon_part = strchr(url, '@');
wrapper.c
+1 -1
@@ -115,7 +115,7 @@ void *xmemdupz(const void *data, size_t len)
115
116 char *xstrndup(const char *str, size_t len)
117 {
118 - char *p = memchr(str, '\0', len);
118 + const char *p = memchr(str, '\0', len);
119 return xmemdupz(str, p ? p - str : len);
120 }
121