From b668e52c906b664b353d5a99cfa3ff36f73b341d Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 15 Feb 2026 14:41:20 +0100 Subject: [PATCH] *: placate warnings where strchr/strstr returns constant pointer Newer glibc is now smarter and can propagate const-ness from those! function old new delta readtoken1 3111 3108 -3 Signed-off-by: Denys Vlasenko --- coreutils/od_bloaty.c | 7 ++++--- coreutils/printf.c | 2 +- editors/awk.c | 2 +- klibc-utils/resume.c | 2 +- libbb/get_last_path_component.c | 4 ++-- libbb/replace.c | 3 ++- libbb/strrstr.c | 4 ++-- libbb/xfuncs_printf.c | 2 +- libpwdgrp/uidgid_get.c | 3 ++- miscutils/bc.c | 2 +- networking/httpd.c | 2 +- networking/ifconfig.c | 2 +- networking/ifupdown.c | 2 +- networking/nbd-client.c | 2 +- networking/route.c | 34 ++++++++++++++++----------------- networking/udhcp/d6_socket.c | 3 ++- networking/udhcp/socket.c | 3 ++- printutils/lpr.c | 2 +- procps/nmeter.c | 5 +++-- procps/powertop.c | 5 +++-- scripts/kconfig/confdata.c | 2 +- shell/ash.c | 14 +++++++------- 22 files changed, 57 insertions(+), 50 deletions(-) diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c index e886a4ed2..c0e6681f2 100644 --- a/coreutils/od_bloaty.c +++ b/coreutils/od_bloaty.c @@ -1155,7 +1155,7 @@ dump_strings(off_t address, off_t end_offset) leading '+' return nonzero and set *OFFSET to the offset it denotes. */ static int -parse_old_offset(const char *s, off_t *offset) +parse_old_offset(char *s, off_t *offset) { static const struct suffix_mult Bb[] ALIGN_SUFFIX = { { "B", 1024 }, @@ -1181,7 +1181,8 @@ parse_old_offset(const char *s, off_t *offset) radix = 16; *offset = xstrtooff_sfx(s, radix, Bb); - if (p) p[0] = '.'; + if (p) + p[0] = '.'; /* undo cheating */ return (*offset >= 0); } @@ -1234,7 +1235,7 @@ int od_main(int argc UNUSED_PARAM, char **argv) static const uint8_t doxn_address_pad_len_char[] ALIGN1 = { '7', '7', '6', /* '?' */ }; - char *p; + const char *p; int pos; p = strchr(doxn, str_A[0]); if (!p) diff --git a/coreutils/printf.c b/coreutils/printf.c index 4edcfa9b5..3cd48cfcc 100644 --- a/coreutils/printf.c +++ b/coreutils/printf.c @@ -354,7 +354,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err) /* Add "ll" if integer modifier, then print */ { static const char format_chars[] ALIGN1 = "diouxXfeEgGcs"; - char *p = strchr(format_chars, *f); + char *p = (char*)strchr(format_chars, *f); /* needed - try "printf %" without it */ if (p == NULL || *f == '\0') { bb_error_msg("%s: invalid format", direc_start); diff --git a/editors/awk.c b/editors/awk.c index beba487fb..dd8f4ac42 100644 --- a/editors/awk.c +++ b/editors/awk.c @@ -2841,7 +2841,7 @@ static NOINLINE var *exec_builtin(node *op, var *res) l = strlen(as[0]) - ll; if (ll > 0 && l >= 0) { if (!icase) { - char *s = strstr(as[0], as[1]); + const char *s = strstr(as[0], as[1]); if (s) n = (s - as[0]) + 1; } else { diff --git a/klibc-utils/resume.c b/klibc-utils/resume.c index 179413627..cb8314c75 100644 --- a/klibc-utils/resume.c +++ b/klibc-utils/resume.c @@ -31,7 +31,7 @@ * - /dev/ram (alias to /dev/ram0) * - /dev/mtd */ -static dev_t name_to_dev_t(const char *devname) +static dev_t name_to_dev_t(char *devname) { char devfile[sizeof(int)*3 * 2 + 4]; char *sysname; diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c index 04fdf2a3e..f9d56ad23 100644 --- a/libbb/get_last_path_component.c +++ b/libbb/get_last_path_component.c @@ -24,12 +24,12 @@ const char* FAST_FUNC bb_basename(const char *name) */ char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path) { - char *slash = strrchr(path, '/'); + const char *slash = strrchr(path, '/'); if (!slash || (slash == path && !slash[1])) return (char*)path; - return slash + 1; + return (char*)slash + 1; } /* diff --git a/libbb/replace.c b/libbb/replace.c index bc26b04cc..273330f8a 100644 --- a/libbb/replace.c +++ b/libbb/replace.c @@ -28,7 +28,8 @@ unsigned FAST_FUNC count_strstr(const char *str, const char *sub) char* FAST_FUNC xmalloc_substitute_string(const char *src, int count, const char *sub, const char *repl) { - char *buf, *dst, *end; + char *buf, *dst; + const char *end; size_t sub_len = strlen(sub); size_t repl_len = strlen(repl); diff --git a/libbb/strrstr.c b/libbb/strrstr.c index a173b034f..bea5d1773 100644 --- a/libbb/strrstr.c +++ b/libbb/strrstr.c @@ -19,10 +19,10 @@ char* FAST_FUNC strrstr(const char *haystack, const char *needle) if (!needle[0]) return (char*)haystack + strlen(haystack); while (1) { - char *p = strstr(haystack, needle); + const char *p = strstr(haystack, needle); if (!p) return r; - r = p; + r = (char *)p; haystack = p + 1; } } diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index ed10084b3..8afa1ef68 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -379,7 +379,7 @@ void FAST_FUNC bb_unsetenv(const char *var) char onstack[128 - 16]; /* smaller stack setup code on x86 */ char *tp; - tp = strchr(var, '='); + tp = (char*)strchr(var, '='); if (tp) { /* In case var was putenv'ed, we can't replace '=' * with NUL and unsetenv(var) - it won't work, diff --git a/libpwdgrp/uidgid_get.c b/libpwdgrp/uidgid_get.c index d76eb8298..2aa444416 100644 --- a/libpwdgrp/uidgid_get.c +++ b/libpwdgrp/uidgid_get.c @@ -32,7 +32,8 @@ int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug) { struct passwd *pwd; struct group *gr; - char *user, *group; + char *user; + const char *group; unsigned n; user = (char*)ug; diff --git a/miscutils/bc.c b/miscutils/bc.c index 28bc40c8b..1ef8f418d 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c @@ -5525,7 +5525,7 @@ static void xc_program_printString(const char *str) char c = *str++; if (c == '\\') { static const char esc[] ALIGN1 = "nabfrt""e\\"; - char *n; + const char *n; c = *str++; n = strchr(esc, c); // note: if c is NUL, n = \0 at end of esc diff --git a/networking/httpd.c b/networking/httpd.c index 3739fd55f..3b811cb71 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -1846,7 +1846,7 @@ static void send_cgi_and_exit( */ static NOINLINE void send_file_and_exit(const char *url, int what) { - char *suffix; + const char *suffix; int fd; ssize_t count; diff --git a/networking/ifconfig.c b/networking/ifconfig.c index 9ee232a66..32638e2a3 100644 --- a/networking/ifconfig.c +++ b/networking/ifconfig.c @@ -336,7 +336,7 @@ int ifconfig_main(int argc UNUSED_PARAM, char **argv) #endif char *p; /*char host[128];*/ - const char *host = NULL; /* make gcc happy */ + char *host = NULL; /* make gcc happy */ IF_FEATURE_IFCONFIG_STATUS(char *show_all_param;) did_flags = 0; diff --git a/networking/ifupdown.c b/networking/ifupdown.c index bc2dca506..6832ee0d4 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c @@ -363,7 +363,7 @@ static char *parse(const char *command, struct interface_defn_t *ifd) break; case '%': { - char *nextpercent; + const char *nextpercent; char *varvalue; command++; diff --git a/networking/nbd-client.c b/networking/nbd-client.c index 556fa8c97..4fda66125 100644 --- a/networking/nbd-client.c +++ b/networking/nbd-client.c @@ -260,7 +260,7 @@ int nbdclient_main(int argc, char **argv) // needs some other process to sit in ioctl(nbd, NBD_DO_IT). if (fork() == 0) { /* child */ - char *s = strrchr(device, '/'); + const char *s = strrchr(device, '/'); sprintf(data, "/sys/block/%.32s/pid", s ? s + 1 : device); // Is it up yet? for (;;) { diff --git a/networking/route.c b/networking/route.c index 6e2d30cfd..9d9b72416 100644 --- a/networking/route.c +++ b/networking/route.c @@ -179,7 +179,7 @@ static NOINLINE void INET_setroute(int action, char **args) memset(rt, 0, sizeof(*rt)); { - const char *target = *args++; + char *target = *args++; char *prefix; /* recognize x.x.x.x/mask format. */ @@ -353,25 +353,25 @@ static NOINLINE void INET6_setroute(int action, char **args) int prefix_len, skfd; const char *devname; - /* We know args isn't NULL from the check in route_main. */ - const char *target = *args++; + /* We know args isn't NULL from the check in route_main. */ + char *target = *args++; - if (strcmp(target, "default") == 0) { - prefix_len = 0; - memset(&sa6, 0, sizeof(sa6)); + if (strcmp(target, "default") == 0) { + prefix_len = 0; + memset(&sa6, 0, sizeof(sa6)); + } else { + char *cp; + cp = strchr(target, '/'); /* Yes... const to non is ok. */ + if (cp) { + *cp = '\0'; + prefix_len = xatoul_range(cp + 1, 0, 128); } else { - char *cp; - cp = strchr(target, '/'); /* Yes... const to non is ok. */ - if (cp) { - *cp = '\0'; - prefix_len = xatoul_range(cp + 1, 0, 128); - } else { - prefix_len = 128; - } - if (INET6_resolve(target, (struct sockaddr_in6 *) &sa6) < 0) { - bb_error_msg_and_die("resolving %s", target); - } + prefix_len = 128; } + if (INET6_resolve(target, (struct sockaddr_in6 *) &sa6) < 0) { + bb_error_msg_and_die("resolving %s", target); + } + } /* Clean out the RTREQ structure. */ memset(&rt, 0, sizeof(rt)); diff --git a/networking/udhcp/d6_socket.c b/networking/udhcp/d6_socket.c index acf108367..83df4b396 100644 --- a/networking/udhcp/d6_socket.c +++ b/networking/udhcp/d6_socket.c @@ -116,7 +116,8 @@ int FAST_FUNC d6_listen_socket(int port, const char *inf) bb_simple_perror_msg_and_die("SO_BROADCAST"); /* SO_BINDTODEVICE doesn't work on ethernet aliases (ethN:M) */ - colon = strrchr(inf, ':'); + colon = (char*)strrchr(inf, ':'); + /* NB: inf can really be a *const* string if it's a default, but defaults have no ':' */ if (colon) *colon = '\0'; diff --git a/networking/udhcp/socket.c b/networking/udhcp/socket.c index 35e10688b..5d2283eaf 100644 --- a/networking/udhcp/socket.c +++ b/networking/udhcp/socket.c @@ -90,7 +90,8 @@ int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf) bb_simple_perror_msg_and_die("SO_BROADCAST"); /* SO_BINDTODEVICE doesn't work on ethernet aliases (ethN:M) */ - colon = strrchr(inf, ':'); + colon = (char*)strrchr(inf, ':'); + /* NB: inf can really be a *const* string if it's a default, but defaults have no ':' */ if (colon) *colon = '\0'; diff --git a/printutils/lpr.c b/printutils/lpr.c index 25b0f7235..464208c65 100644 --- a/printutils/lpr.c +++ b/printutils/lpr.c @@ -122,7 +122,7 @@ int lpqr_main(int argc UNUSED_PARAM, char **argv) { // queue name is to the left of '@' - char *s = strchr(queue, '@'); + char *s = (char *)strchr(queue, '@'); if (s) { // server name is to the right of '@' *s = '\0'; diff --git a/procps/nmeter.c b/procps/nmeter.c index dca07eac6..fd8907aac 100644 --- a/procps/nmeter.c +++ b/procps/nmeter.c @@ -913,7 +913,8 @@ int nmeter_main(int argc UNUSED_PARAM, char **argv) // parameters as seen by e.g. ps. Making a copy... cur = xstrdup(argv[0]); while (1) { - char *param, *p; + char *param; + const char *p; prev = cur; again: cur = strchr(cur, '%'); @@ -929,7 +930,7 @@ int nmeter_main(int argc UNUSED_PARAM, char **argv) // format: %[foptstring] cur++; p = strchr(options, cur[0]); - param = cur+1; + param = cur + 1; while (cur[0] != ']') { if (!cur[0]) bb_show_usage(); diff --git a/procps/powertop.c b/procps/powertop.c index 6fe892540..a24748efc 100644 --- a/procps/powertop.c +++ b/procps/powertop.c @@ -238,7 +238,7 @@ static void save_line(const char *string, int count) #if ENABLE_FEATURE_POWERTOP_PROCIRQ static int is_hpet_irq(const char *name) { - char *p; + const char *p; # if BLOATY_HPET_IRQ_NUM_DETECTION long hpet_chan; @@ -423,7 +423,8 @@ static NOINLINE int process_timer_stats(void) // 1, 2159 udisks-daemon hrtimer_start_range_ns (hrtimer_wakeup) // 331 total events, 249.059 events/sec while (fgets(buf, sizeof(buf), fp)) { - const char *count, *process, *func; + const char *process, *func; + char *count; char *p; int idx; unsigned cnt; diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index 249a3195e..d13a7d66b 100644 --- a/scripts/kconfig/confdata.c +++ b/scripts/kconfig/confdata.c @@ -348,7 +348,7 @@ int conf_write(const char *name) dirname[0] = 0; if (name && name[0]) { struct stat st; - char *slash; + const char *slash; if (!stat(name, &st) && S_ISDIR(st.st_mode)) { strcpy(dirname, name); diff --git a/shell/ash.c b/shell/ash.c index 4f824e1b2..19623a9a0 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -8725,7 +8725,7 @@ test_exec(/*const char *fullname,*/ struct stat *statb) } /* Circular dep: find_command->find_builtin->builtintab[]->hashcmd->find_command */ -static struct builtincmd *find_builtin(const char *name); +static const struct builtincmd *find_builtin(const char *name); #if ENABLE_ASH_BASH_NOT_FOUND_HOOK static int evalfun(struct funcnode *func, int argc, char **argv, int flags); #endif @@ -8746,7 +8746,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) struct stat statb; int e; int updatetbl; - struct builtincmd *bcmd; + const struct builtincmd *bcmd; int len; /* If name contains a slash, don't use PATH or hash table */ @@ -10836,10 +10836,10 @@ static const struct builtincmd builtintab[] = { /* * Search the table of builtin commands. */ -static struct builtincmd * +static const struct builtincmd * find_builtin(const char *name) { - struct builtincmd *bp; + const struct builtincmd *bp; bp = bsearch( name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), @@ -12927,12 +12927,12 @@ decode_dollar_squote(void) { static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; int c, cnt; - char *p; char buf[4]; c = pgetc(); - p = strchr(C_escapes, c); - if (p) { + if (strchr(C_escapes, c)) { + char *p; + buf[0] = c; p = buf; cnt = 3;