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>
This commit is contained in:
Collin Funk
2026-02-05 17:46:09 -08:00
committed by Junio C Hamano
parent 67ad42147a
commit 4ac4705afa
28 changed files with 34 additions and 31 deletions

View 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;

View File

@@ -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 &&

View 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));

View 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);

View 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;

View 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;
}

View File

@@ -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"));

View File

@@ -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;

4
diff.c
View File

@@ -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;

View 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("");
}

View 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;

2
fsck.c
View File

@@ -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;

View 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:

2
help.c
View File

@@ -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/") &&

View 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) {

View 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);

View 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);

View 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,

View 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;

View 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")",

View 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;

View 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;

View File

@@ -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"));

View File

@@ -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);
}

View 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 */

View 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);

View 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, '@');

View File

@@ -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);
}