]> Kevux Git Server - rit/commitdiff
global: constify some pointers that are not written to
authorCollin Funk <collin.funk1@gmail.com>
Fri, 6 Feb 2026 01:46:09 +0000 (17:46 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 6 Feb 2026 01:52:49 +0000 (17:52 -0800)
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>
28 files changed:
add-patch.c
apply.c
builtin/commit.c
builtin/receive-pack.c
builtin/remote.c
builtin/shortlog.c
config.c
convert.c
diff.c
diffcore-rename.c
fmt-merge-msg.c
fsck.c
gpg-interface.c
help.c
http-push.c
mailinfo.c
mem-pool.c
merge-ort.c
object-name.c
pack-revindex.c
pkt-line.c
reflog-walk.c
scalar.c
strbuf.c
string-list.c
t/unit-tests/clar/clar/print.h
transport.c
wrapper.c

index 173a532..7024261 100644 (file)
@@ -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 3de4aa4..9de2eb9 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -4144,7 +4144,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 &&
index 8e901fe..0326546 100644 (file)
@@ -816,7 +816,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));
index 9c49174..e8b6f96 100644 (file)
@@ -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);
index 7ffc14b..ace390c 100644 (file)
@@ -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;
index b91acf4..d80bf1a 100644 (file)
@@ -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;
                }
index 7f6d53b..156f2a2 100644 (file)
--- a/config.c
+++ b/config.c
@@ -160,7 +160,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"));
index c7d6a85..a34ec6e 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -1122,7 +1122,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;
diff --git a/diff.c b/diff.c
index a68ddd2..2d92665 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -1961,7 +1961,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));
 
@@ -3049,7 +3049,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;
index 7723bc3..d9476db 100644 (file)
@@ -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("");
 }
 
index c9085ed..1626667 100644 (file)
@@ -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 3afec0d..0f02cf8 100644 (file)
--- 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;
index 47222bf..377c0cf 100644 (file)
@@ -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 = NULL;
 
        /*
         * ssh-keygen output should be:
diff --git a/help.c b/help.c
index 3c36d9c..be334d7 100644 (file)
--- a/help.c
+++ b/help.c
@@ -857,7 +857,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/") &&
index cc0f809..9ae6062 100644 (file)
@@ -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) {
index 99ac596..a2f06db 100644 (file)
@@ -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);
index 62441dc..8bc77cb 100644 (file)
@@ -169,7 +169,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);
 
index e80e4f7..6f30471 100644 (file)
@@ -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,
index 8b862c1..e1b09d8 100644 (file)
@@ -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;
 
index 8598b94..56cd803 100644 (file)
@@ -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")",
index fc583fe..3fc3e9e 100644 (file)
@@ -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;
index 4f1ce04..4dbeaa9 100644 (file)
@@ -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;
 
index c9df934..4efb6ac 100644 (file)
--- a/scalar.c
+++ b/scalar.c
@@ -393,7 +393,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"));
index 59678bf..3939863 100644 (file)
--- 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);
 }
index 08dc009..7c34a42 100644 (file)
@@ -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 */
index 6a2321b..59b7dc1 100644 (file)
@@ -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);
index c7f06a7..845fd44 100644 (file)
@@ -1657,7 +1657,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, '@');
index b794fb2..16f5a63 100644 (file)
--- 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);
 }