Skip to content

Commit de42b5a

Browse files
committed
Merge branch 'jk/c23-const-preserving-fixes' into seen
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. Comments? * jk/c23-const-preserving-fixes: config: store allocated string in non-const pointer rev-parse: avoid writing to const string for parent marks revision: avoid writing to const string for parent marks rev-parse: simplify dotdot parsing revision: make handle_dotdot() interface less confusing
2 parents 771fa1d + d385845 commit de42b5a

File tree

3 files changed

+58
-56
lines changed

3 files changed

+58
-56
lines changed

builtin/config.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ static int get_urlmatch(const struct config_location_options *opts,
838838
const char *var, const char *url)
839839
{
840840
int ret;
841+
char *section;
841842
char *section_tail;
842843
struct config_display_options display_opts = *_display_opts;
843844
struct string_list_item *item;
@@ -851,8 +852,8 @@ static int get_urlmatch(const struct config_location_options *opts,
851852
if (!url_normalize(url, &config.url))
852853
die("%s", config.url.err);
853854

854-
config.section = xstrdup_tolower(var);
855-
section_tail = strchr(config.section, '.');
855+
config.section = section = xstrdup_tolower(var);
856+
section_tail = strchr(section, '.');
856857
if (section_tail) {
857858
*section_tail = '\0';
858859
config.key = section_tail + 1;
@@ -886,7 +887,7 @@ static int get_urlmatch(const struct config_location_options *opts,
886887
string_list_clear(&values, 1);
887888
free(config.url.url);
888889

889-
free((void *)config.section);
890+
free(section);
890891
return ret;
891892
}
892893

builtin/rev-parse.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,20 @@ static int show_file(const char *arg, int output_prefix)
267267

268268
static int try_difference(const char *arg)
269269
{
270-
char *dotdot;
270+
const char *dotdot;
271271
struct object_id start_oid;
272272
struct object_id end_oid;
273273
const char *end;
274274
const char *start;
275+
char *to_free;
275276
int symmetric;
276277
static const char head_by_default[] = "HEAD";
277278

278279
if (!(dotdot = strstr(arg, "..")))
279280
return 0;
281+
start = to_free = xmemdupz(arg, dotdot - arg);
280282
end = dotdot + 2;
281-
start = arg;
282283
symmetric = (*end == '.');
283-
284-
*dotdot = 0;
285284
end += symmetric;
286285

287286
if (!*end)
@@ -295,7 +294,7 @@ static int try_difference(const char *arg)
295294
* Just ".."? That is not a range but the
296295
* pathspec for the parent directory.
297296
*/
298-
*dotdot = '.';
297+
free(to_free);
299298
return 0;
300299
}
301300

@@ -308,7 +307,7 @@ static int try_difference(const char *arg)
308307
a = lookup_commit_reference(the_repository, &start_oid);
309308
b = lookup_commit_reference(the_repository, &end_oid);
310309
if (!a || !b) {
311-
*dotdot = '.';
310+
free(to_free);
312311
return 0;
313312
}
314313
if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0)
@@ -318,55 +317,56 @@ static int try_difference(const char *arg)
318317
show_rev(REVERSED, &commit->object.oid, NULL);
319318
}
320319
}
321-
*dotdot = '.';
320+
free(to_free);
322321
return 1;
323322
}
324-
*dotdot = '.';
323+
free(to_free);
325324
return 0;
326325
}
327326

328327
static int try_parent_shorthands(const char *arg)
329328
{
330-
char *dotdot;
329+
const char *mark;
331330
struct object_id oid;
332331
struct commit *commit;
333332
struct commit_list *parents;
334333
int parent_number;
335334
int include_rev = 0;
336335
int include_parents = 0;
337336
int exclude_parent = 0;
337+
char *to_free;
338338

339-
if ((dotdot = strstr(arg, "^!"))) {
339+
if ((mark = strstr(arg, "^!"))) {
340340
include_rev = 1;
341-
if (dotdot[2])
341+
if (mark[2])
342342
return 0;
343-
} else if ((dotdot = strstr(arg, "^@"))) {
343+
} else if ((mark = strstr(arg, "^@"))) {
344344
include_parents = 1;
345-
if (dotdot[2])
345+
if (mark[2])
346346
return 0;
347-
} else if ((dotdot = strstr(arg, "^-"))) {
347+
} else if ((mark = strstr(arg, "^-"))) {
348348
include_rev = 1;
349349
exclude_parent = 1;
350350

351-
if (dotdot[2]) {
351+
if (mark[2]) {
352352
char *end;
353-
exclude_parent = strtoul(dotdot + 2, &end, 10);
353+
exclude_parent = strtoul(mark + 2, &end, 10);
354354
if (*end != '\0' || !exclude_parent)
355355
return 0;
356356
}
357357
} else
358358
return 0;
359359

360-
*dotdot = 0;
360+
arg = to_free = xmemdupz(arg, mark - arg);
361361
if (repo_get_oid_committish(the_repository, arg, &oid) ||
362362
!(commit = lookup_commit_reference(the_repository, &oid))) {
363-
*dotdot = '^';
363+
free(to_free);
364364
return 0;
365365
}
366366

367367
if (exclude_parent &&
368368
exclude_parent > commit_list_count(commit->parents)) {
369-
*dotdot = '^';
369+
free(to_free);
370370
return 0;
371371
}
372372

@@ -387,7 +387,7 @@ static int try_parent_shorthands(const char *arg)
387387
free(name);
388388
}
389389

390-
*dotdot = '^';
390+
free(to_free);
391391
return 1;
392392
}
393393

revision.c

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,41 +2038,32 @@ static void prepare_show_merge(struct rev_info *revs)
20382038
free(prune);
20392039
}
20402040

2041-
static int dotdot_missing(const char *arg, char *dotdot,
2041+
static int dotdot_missing(const char *full_name,
20422042
struct rev_info *revs, int symmetric)
20432043
{
20442044
if (revs->ignore_missing)
20452045
return 0;
2046-
/* de-munge so we report the full argument */
2047-
*dotdot = '.';
20482046
die(symmetric
20492047
? "Invalid symmetric difference expression %s"
2050-
: "Invalid revision range %s", arg);
2048+
: "Invalid revision range %s", full_name);
20512049
}
20522050

2053-
static int handle_dotdot_1(const char *arg, char *dotdot,
2051+
static int handle_dotdot_1(const char *a_name, const char *b_name,
2052+
const char *full_name, int symmetric,
20542053
struct rev_info *revs, int flags,
20552054
int cant_be_filename,
20562055
struct object_context *a_oc,
20572056
struct object_context *b_oc)
20582057
{
2059-
const char *a_name, *b_name;
20602058
struct object_id a_oid, b_oid;
20612059
struct object *a_obj, *b_obj;
20622060
unsigned int a_flags, b_flags;
2063-
int symmetric = 0;
20642061
unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
20652062
unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
20662063

2067-
a_name = arg;
20682064
if (!*a_name)
20692065
a_name = "HEAD";
20702066

2071-
b_name = dotdot + 2;
2072-
if (*b_name == '.') {
2073-
symmetric = 1;
2074-
b_name++;
2075-
}
20762067
if (!*b_name)
20772068
b_name = "HEAD";
20782069

@@ -2081,15 +2072,13 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
20812072
return -1;
20822073

20832074
if (!cant_be_filename) {
2084-
*dotdot = '.';
2085-
verify_non_filename(revs->prefix, arg);
2086-
*dotdot = '\0';
2075+
verify_non_filename(revs->prefix, full_name);
20872076
}
20882077

20892078
a_obj = parse_object(revs->repo, &a_oid);
20902079
b_obj = parse_object(revs->repo, &b_oid);
20912080
if (!a_obj || !b_obj)
2092-
return dotdot_missing(arg, dotdot, revs, symmetric);
2081+
return dotdot_missing(full_name, revs, symmetric);
20932082

20942083
if (!symmetric) {
20952084
/* just A..B */
@@ -2103,7 +2092,7 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
21032092
a = lookup_commit_reference(revs->repo, &a_obj->oid);
21042093
b = lookup_commit_reference(revs->repo, &b_obj->oid);
21052094
if (!a || !b)
2106-
return dotdot_missing(arg, dotdot, revs, symmetric);
2095+
return dotdot_missing(full_name, revs, symmetric);
21072096

21082097
if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
21092098
commit_list_free(exclude);
@@ -2132,16 +2121,23 @@ static int handle_dotdot(const char *arg,
21322121
int cant_be_filename)
21332122
{
21342123
struct object_context a_oc = {0}, b_oc = {0};
2135-
char *dotdot = strstr(arg, "..");
2124+
const char *dotdot = strstr(arg, "..");
2125+
char *tmp;
2126+
int symmetric = 0;
21362127
int ret;
21372128

21382129
if (!dotdot)
21392130
return -1;
21402131

2141-
*dotdot = '\0';
2142-
ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2143-
&a_oc, &b_oc);
2144-
*dotdot = '.';
2132+
tmp = xmemdupz(arg, dotdot - arg);
2133+
dotdot += 2;
2134+
if (*dotdot == '.') {
2135+
symmetric = 1;
2136+
dotdot++;
2137+
}
2138+
ret = handle_dotdot_1(tmp, dotdot, arg, symmetric, revs, flags,
2139+
cant_be_filename, &a_oc, &b_oc);
2140+
free(tmp);
21452141

21462142
object_context_release(&a_oc);
21472143
object_context_release(&b_oc);
@@ -2151,7 +2147,10 @@ static int handle_dotdot(const char *arg,
21512147
static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
21522148
{
21532149
struct object_context oc = {0};
2154-
char *mark;
2150+
const char *mark;
2151+
char *arg_minus_at = NULL;
2152+
char *arg_minus_excl = NULL;
2153+
char *arg_minus_dash = NULL;
21552154
struct object *object;
21562155
struct object_id oid;
21572156
int local_flags;
@@ -2178,18 +2177,17 @@ static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int fl
21782177

21792178
mark = strstr(arg, "^@");
21802179
if (mark && !mark[2]) {
2181-
*mark = 0;
2182-
if (add_parents_only(revs, arg, flags, 0)) {
2180+
arg_minus_at = xmemdupz(arg, mark - arg);
2181+
if (add_parents_only(revs, arg_minus_at, flags, 0)) {
21832182
ret = 0;
21842183
goto out;
21852184
}
2186-
*mark = '^';
21872185
}
21882186
mark = strstr(arg, "^!");
21892187
if (mark && !mark[2]) {
2190-
*mark = 0;
2191-
if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2192-
*mark = '^';
2188+
arg_minus_excl = xmemdupz(arg, mark - arg);
2189+
if (add_parents_only(revs, arg_minus_excl, flags ^ (UNINTERESTING | BOTTOM), 0))
2190+
arg = arg_minus_excl;
21932191
}
21942192
mark = strstr(arg, "^-");
21952193
if (mark) {
@@ -2203,9 +2201,9 @@ static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int fl
22032201
}
22042202
}
22052203

2206-
*mark = 0;
2207-
if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2208-
*mark = '^';
2204+
arg_minus_dash = xmemdupz(arg, mark - arg);
2205+
if (add_parents_only(revs, arg_minus_dash, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2206+
arg = arg_minus_dash;
22092207
}
22102208

22112209
local_flags = 0;
@@ -2240,6 +2238,9 @@ static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int fl
22402238

22412239
out:
22422240
object_context_release(&oc);
2241+
free(arg_minus_at);
2242+
free(arg_minus_excl);
2243+
free(arg_minus_dash);
22432244
return ret;
22442245
}
22452246

0 commit comments

Comments
 (0)