Skip to content

Commit 3eabc35

Browse files
committed
Merge branch 'jk/c23-const-preserving-fixes-more'
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: git-compat-util: fix CONST_OUTPARAM typo and indentation 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 b15384c + 58589c2 commit 3eabc35

File tree

15 files changed

+73
-45
lines changed

15 files changed

+73
-45
lines changed

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: 24 additions & 9 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
@@ -467,6 +463,21 @@ void set_warn_routine(report_fn routine);
467463
report_fn get_warn_routine(void);
468464
void set_die_is_recursing_routine(int (*routine)(void));
469465

466+
/*
467+
* Check that an out-parameter is "at least as const as" a matching
468+
* in-parameter. For example, skip_prefix() will return "out" that is a subset
469+
* of "str". So:
470+
*
471+
* const str, const out: ok
472+
* non-const str, const out: ok
473+
* non-const str, non-const out: ok
474+
* const str, non-const out: compile error
475+
*
476+
* See the skip_prefix macro below for an example of use.
477+
*/
478+
#define CONST_OUTPARAM(in, out) \
479+
((const char **)(0 ? ((*(out) = (in)),(out)) : (out)))
480+
470481
/*
471482
* If the string "str" begins with the string found in "prefix", return true.
472483
* The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
@@ -483,8 +494,10 @@ void set_die_is_recursing_routine(int (*routine)(void));
483494
* [skip prefix if present, otherwise use whole string]
484495
* skip_prefix(name, "refs/heads/", &name);
485496
*/
486-
static inline bool skip_prefix(const char *str, const char *prefix,
487-
const char **out)
497+
#define skip_prefix(str, prefix, out) \
498+
skip_prefix_impl((str), (prefix), CONST_OUTPARAM((str), (out)))
499+
static inline bool skip_prefix_impl(const char *str, const char *prefix,
500+
const char **out)
488501
{
489502
do {
490503
if (!*prefix) {
@@ -889,8 +902,10 @@ static inline size_t xsize_t(off_t len)
889902
* is done via tolower(), so it is strictly ASCII (no multi-byte characters or
890903
* locale-specific conversions).
891904
*/
892-
static inline bool skip_iprefix(const char *str, const char *prefix,
893-
const char **out)
905+
#define skip_iprefix(str, prefix, out) \
906+
skip_iprefix_impl((str), (prefix), CONST_OUTPARAM((str), (out)))
907+
static inline bool skip_iprefix_impl(const char *str, const char *prefix,
908+
const char **out)
894909
{
895910
do {
896911
if (!*prefix) {

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+
#define parse_oid_hex_algop(hex, oid, end, algo) \
44+
parse_oid_hex_algop_impl((hex), (oid), CONST_OUTPARAM((hex), (end)), (algo))
45+
int parse_oid_hex_algop_impl(const char *hex, struct object_id *oid, const char **end,
46+
const struct git_hash_algo *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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ const char *git_pager(struct repository *r, int stdout_is_tty)
108108

109109
static void setup_pager_env(struct strvec *env)
110110
{
111-
const char **argv;
111+
char **argv;
112112
int i;
113113
char *pager_env = xstrdup(PAGER_ENV);
114-
int n = split_cmdline(pager_env, &argv);
114+
/* split_cmdline splits in place, so we know the result is writable */
115+
int n = split_cmdline(pager_env, (const char ***)&argv);
115116

116117
if (n < 0)
117118
die("malformed build-time PAGER_ENV: %s",

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)