Skip to content

Commit d18575c

Browse files
committed
Merge branch 'jk/c23-const-preserving-fixes-more' into jch
Further work to adjust the codebase for C23 that changes functions like strchr() that discarded constness when they return a pointer into a const string to preserve constness. * jk/c23-const-preserving-fixes-more: refs/files-backend: drop const to fix strchr() warning http: drop const to fix strstr() warning range-diff: drop const to fix strstr() warnings pkt-line: make packet_reader.line non-const skip_prefix(): check const match between in and out params pseudo-merge: fix disk reads from find_pseudo_merge() find_last_dir_sep(): convert inline function to macro run-command: explicitly cast away constness when assigning to void pager: explicitly cast away strchr() constness transport-helper: drop const to fix strchr() warnings http: add const to fix strchr() warnings convert: add const to fix strchr() warnings
2 parents e5f5443 + eafa1ad commit d18575c

15 files changed

Lines changed: 69 additions & 40 deletions

builtin/receive-pack.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,8 +1029,8 @@ static int read_proc_receive_report(struct packet_reader *reader,
10291029

10301030
for (;;) {
10311031
struct object_id old_oid, new_oid;
1032-
const char *head;
1033-
const char *refname;
1032+
char *head;
1033+
char *refname;
10341034
char *p;
10351035
enum packet_read_status status;
10361036

@@ -1054,7 +1054,8 @@ static int read_proc_receive_report(struct packet_reader *reader,
10541054
}
10551055
*p++ = '\0';
10561056
if (!strcmp(head, "option")) {
1057-
const char *key, *val;
1057+
char *key;
1058+
const char *val;
10581059

10591060
if (!hint || !(report || new_report)) {
10601061
if (!once++)

convert.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,8 @@ static int ident_to_worktree(const char *src, size_t len,
11681168
struct strbuf *buf, int ident)
11691169
{
11701170
struct object_id oid;
1171-
char *to_free = NULL, *dollar, *spc;
1171+
char *to_free = NULL;
1172+
const char *dollar, *spc;
11721173
int cnt;
11731174

11741175
if (!ident)

git-compat-util.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,7 @@ static inline int is_path_owned_by_current_uid(const char *path,
335335
#endif
336336

337337
#ifndef find_last_dir_sep
338-
static inline char *git_find_last_dir_sep(const char *path)
339-
{
340-
return strrchr(path, '/');
341-
}
342-
#define find_last_dir_sep git_find_last_dir_sep
338+
#define find_last_dir_sep(path) strrchr((path), '/')
343339
#endif
344340

345341
#ifndef has_dir_sep
@@ -495,6 +491,23 @@ static inline bool skip_prefix(const char *str, const char *prefix,
495491
return false;
496492
}
497493

494+
/*
495+
* Check that an out-parameter that is "at least as const as" a matching
496+
* in-parameter. For example, skip_prefix() will return "out" that is a subset
497+
* of "str". So:
498+
*
499+
* const str, const out: ok
500+
* non-const str, const out: ok
501+
* non-const str, non-const out: ok
502+
* const str, non-const out: compile error
503+
*
504+
* See the skip_prefix macro below for an example of use.
505+
*/
506+
#define CONST_OUTPARAM(in, out) \
507+
((const char **)(0 ? ((*(out) = (in)),(out)) : (out)))
508+
#define skip_prefix(str, prefix, out) \
509+
skip_prefix((str), (prefix), CONST_OUTPARAM((str), (out)))
510+
498511
/*
499512
* Like skip_prefix, but promises never to read past "len" bytes of the input
500513
* buffer, and returns the remaining number of bytes in "out" via "outlen".
@@ -901,6 +914,9 @@ static inline bool skip_iprefix(const char *str, const char *prefix,
901914
return false;
902915
}
903916

917+
#define skip_iprefix(str, prefix, out) \
918+
skip_iprefix((str), (prefix), CONST_OUTPARAM((str), (out)))
919+
904920
/*
905921
* Like skip_prefix_mem, but compare case-insensitively. Note that the
906922
* comparison is done via tolower(), so it is strictly ASCII (no multi-byte

hex.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ int get_oid_hex(const char *hex, struct object_id *oid)
5454
return get_oid_hex_algop(hex, oid, the_hash_algo);
5555
}
5656

57-
int parse_oid_hex_algop(const char *hex, struct object_id *oid,
58-
const char **end,
59-
const struct git_hash_algo *algop)
57+
int parse_oid_hex_algop_impl(const char *hex, struct object_id *oid,
58+
const char **end,
59+
const struct git_hash_algo *algop)
6060
{
6161
int ret = get_oid_hex_algop(hex, oid, algop);
6262
if (!ret)

hex.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ char *oid_to_hex(const struct object_id *oid); /* same static buffer */
4040
* other invalid character. end is only updated on success; otherwise, it is
4141
* unmodified.
4242
*/
43-
int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end,
44-
const struct git_hash_algo *algo);
43+
int parse_oid_hex_algop_impl(const char *hex, struct object_id *oid, const char **end,
44+
const struct git_hash_algo *algo);
45+
#define parse_oid_hex_algop(hex, oid, end, algo) \
46+
parse_oid_hex_algop_impl((hex), (oid), CONST_OUTPARAM((hex), (end)), (algo))
4547

4648
/*
4749
* These functions work like get_oid_hex and parse_oid_hex, but they will parse

http-push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static struct object_list *objects;
9999

100100
struct repo {
101101
char *url;
102-
char *path;
102+
const char *path;
103103
int path_len;
104104
int has_info_refs;
105105
int can_update_info_refs;

http.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ static int has_proxy_cert_password(void)
748748
static int redact_sensitive_header(struct strbuf *header, size_t offset)
749749
{
750750
int ret = 0;
751-
const char *sensitive_header;
751+
char *sensitive_header;
752752

753753
if (trace_curl_redact &&
754754
(skip_iprefix(header->buf + offset, "Authorization:", &sensitive_header) ||
@@ -765,7 +765,7 @@ static int redact_sensitive_header(struct strbuf *header, size_t offset)
765765
} else if (trace_curl_redact &&
766766
skip_iprefix(header->buf + offset, "Cookie:", &sensitive_header)) {
767767
struct strbuf redacted_header = STRBUF_INIT;
768-
const char *cookie;
768+
char *cookie;
769769

770770
while (isspace(*sensitive_header))
771771
sensitive_header++;

pager.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ static void setup_pager_env(struct strvec *env)
118118
split_cmdline_strerror(n));
119119

120120
for (i = 0; i < n; i++) {
121-
char *cp = strchr(argv[i], '=');
121+
/* we know this is writable because it was split from pager_env */
122+
char *cp = strchr((char *)argv[i], '=');
122123

123124
if (!cp)
124125
die("malformed build-time PAGER_ENV");

pkt-line.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct packet_reader {
184184
int pktlen;
185185

186186
/* the last line read */
187-
const char *line;
187+
char *line;
188188

189189
/* indicates if a line has been peeked */
190190
int line_peeked;

pseudo-merge.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -638,31 +638,37 @@ static int pseudo_merge_commit_cmp(const void *va, const void *vb)
638638
return 0;
639639
}
640640

641-
static struct pseudo_merge_commit *find_pseudo_merge(const struct pseudo_merge_map *pm,
642-
uint32_t pos)
641+
static int find_pseudo_merge(const struct pseudo_merge_map *pm, uint32_t pos,
642+
struct pseudo_merge_commit *out)
643643
{
644+
const unsigned char *at;
645+
644646
if (!pm->commits_nr)
645-
return NULL;
647+
return 0;
646648

647-
return bsearch(&pos, pm->commits, pm->commits_nr,
648-
PSEUDO_MERGE_COMMIT_RAWSZ, pseudo_merge_commit_cmp);
649+
at = bsearch(&pos, pm->commits, pm->commits_nr,
650+
PSEUDO_MERGE_COMMIT_RAWSZ, pseudo_merge_commit_cmp);
651+
if (!at)
652+
return 0;
653+
654+
read_pseudo_merge_commit_at(out, at);
655+
return 1;
649656
}
650657

651658
int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm,
652659
struct bitmap *result,
653660
struct commit *commit, uint32_t commit_pos)
654661
{
655662
struct pseudo_merge *merge;
656-
struct pseudo_merge_commit *merge_commit;
663+
struct pseudo_merge_commit merge_commit;
657664
int ret = 0;
658665

659-
merge_commit = find_pseudo_merge(pm, commit_pos);
660-
if (!merge_commit)
666+
if (!find_pseudo_merge(pm, commit_pos, &merge_commit))
661667
return 0;
662668

663-
if (merge_commit->pseudo_merge_ofs & ((uint64_t)1<<63)) {
669+
if (merge_commit.pseudo_merge_ofs & ((uint64_t)1<<63)) {
664670
struct pseudo_merge_commit_ext ext = { 0 };
665-
off_t ofs = merge_commit->pseudo_merge_ofs & ~((uint64_t)1<<63);
671+
off_t ofs = merge_commit.pseudo_merge_ofs & ~((uint64_t)1<<63);
666672
uint32_t i;
667673

668674
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,
673679
}
674680

675681
for (i = 0; i < ext.nr; i++) {
676-
if (nth_pseudo_merge_ext(pm, &ext, merge_commit, i) < 0)
682+
if (nth_pseudo_merge_ext(pm, &ext, &merge_commit, i) < 0)
677683
return ret;
678684

679685
merge = pseudo_merge_at(pm, &commit->object.oid,
680-
merge_commit->pseudo_merge_ofs);
686+
merge_commit.pseudo_merge_ofs);
681687

682688
if (!merge)
683689
return ret;
@@ -687,7 +693,7 @@ int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm,
687693
}
688694
} else {
689695
merge = pseudo_merge_at(pm, &commit->object.oid,
690-
merge_commit->pseudo_merge_ofs);
696+
merge_commit.pseudo_merge_ofs);
691697

692698
if (!merge)
693699
return ret;

0 commit comments

Comments
 (0)