Skip to content

Commit 6e1479c

Browse files
committed
build: constify strchr()/memchr() results
To fix building with glibc-2.43. (also seen with gcc 16 on Fedora rawhide/f44) Also: - scope a variable while there. - fix altering the const format buffer on bad syntax. Reported-by: Gustavo Costa Fixes #430 Reported-by: Michael Ablassmeier Fixes #431 Closes #432
1 parent f553d82 commit 6e1479c

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

trurl.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ static void pathadd(struct option *o, const char *path)
499499

500500
static char *encodeassign(const char *query)
501501
{
502-
char *p = strchr(query, '=');
502+
const char *p = strchr(query, '=');
503503
char *urle;
504504
if(p) {
505505
/* URL encode the left and the right side of the '=' separately */
@@ -614,7 +614,7 @@ static int getarg(struct option *o,
614614
gap = false;
615615
}
616616
else if((flag[0] == '-') && (flag[1] == '-')) {
617-
char *equals = strchr(&flag[2], '=');
617+
const char *equals = strchr(&flag[2], '=');
618618
if(equals) {
619619
arg = (const char *)&equals[1];
620620
gap = false;
@@ -868,9 +868,10 @@ static void get(struct option *o, CURLU *uh)
868868
else {
869869
/* this is meant as a variable to output */
870870
const char *start = ptr;
871-
char *end;
872-
char *cl;
871+
const char *end;
872+
const char *cl;
873873
size_t vlen;
874+
size_t badlen = 0;
874875
bool isquery = false;
875876
bool queryall = false;
876877
bool strict = false; /* strict mode, fail on URL decode problems */
@@ -930,7 +931,7 @@ static void get(struct option *o, CURLU *uh)
930931
else {
931932
/* syntax error */
932933
vlen = 0;
933-
end[1] = '\0';
934+
badlen = end - start + 1;
934935
}
935936
break;
936937
}
@@ -945,7 +946,7 @@ static void get(struct option *o, CURLU *uh)
945946
queryall);
946947
}
947948
else if(!vlen)
948-
errorf(o, ERROR_GET, "Bad --get syntax: %s", start);
949+
errorf(o, ERROR_GET, "Bad --get syntax: %.*s", (int)badlen, start);
949950
else if(!strncmp(ptr, "url", vlen))
950951
showurl(stream, o, mods, uh);
951952
else {
@@ -1029,7 +1030,7 @@ static void get(struct option *o, CURLU *uh)
10291030
static const struct var *setone(CURLU *uh, const char *setline,
10301031
struct option *o)
10311032
{
1032-
char *ptr = strchr(setline, '=');
1033+
const char *ptr = strchr(setline, '=');
10331034
const struct var *v = NULL;
10341035
if(ptr && (ptr > setline)) {
10351036
size_t vlen = ptr - setline;
@@ -1274,8 +1275,8 @@ static bool trim(struct option *o)
12741275
}
12751276

12761277
for(i = 0; i < nqpairs; i++) {
1277-
char *q = qpairs[i].str;
1278-
char *sep = strchr(q, '=');
1278+
const char *q = qpairs[i].str;
1279+
const char *sep = strchr(q, '=');
12791280
size_t qlen;
12801281
if(sep)
12811282
qlen = sep - q;
@@ -1554,10 +1555,9 @@ static bool extractqpairs(CURLU *uh, struct option *o)
15541555
/* extract the query */
15551556
if(!curl_url_get(uh, CURLUPART_QUERY, &q, 0)) {
15561557
char *p = q;
1557-
char *amp;
15581558
while(*p) {
15591559
size_t len;
1560-
amp = strchr(p, o->qsep[0]);
1560+
char *amp = strchr(p, o->qsep[0]);
15611561
if(!amp)
15621562
len = strlen(p);
15631563
else
@@ -1692,7 +1692,7 @@ static char *canonical_path(const char *path)
16921692
{
16931693
/* split the path per slash, URL decode + encode, then put together again */
16941694
size_t len = strlen(path);
1695-
char *sl;
1695+
const char *sl;
16961696
char *dupe = NULL;
16971697

16981698
do {
@@ -1819,7 +1819,8 @@ static void singleurl(struct option *o,
18191819
size_t plen;
18201820
const char *w;
18211821
size_t wlen;
1822-
char *sep;
1822+
const char *sep;
1823+
char *sepw;
18231824
bool urlencode = true;
18241825
const struct var *v;
18251826

@@ -1860,10 +1861,10 @@ static void singleurl(struct option *o,
18601861
w = iinfo->ptr;
18611862
}
18621863

1863-
sep = strchr(w, ' ');
1864-
if(sep) {
1865-
wlen = sep - w;
1866-
iinfo->ptr = sep + 1; /* next word is here */
1864+
sepw = strchr(w, ' ');
1865+
if(sepw) {
1866+
wlen = sepw - w;
1867+
iinfo->ptr = sepw + 1; /* next word is here */
18671868
}
18681869
else {
18691870
/* last word */

0 commit comments

Comments
 (0)