Skip to content

Commit fb3dbd7

Browse files
committed
mod_rewrite: disambiguate select_random_value_part().
gcc-13's -fsanitize=undefined finds: mod_rewrite.c|1702 col 37| error: '%s' directive argument is null [-Werror=format-overflow=] || 1701 | value = select_random_value_part(r, value); || 1702 | rewritelog((r, 5, NULL, "randomly chosen the subvalue `%s'",value)); || | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ because it's not clear from select_random_value_part() whether it can return NULL or not. Rewrite the function so that it's clearer/simpler. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1916298 13f79535-47bb-0310-9956-ffa450edef68
1 parent b3e19c3 commit fb3dbd7

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

modules/mappers/mod_rewrite.c

+8-11
Original file line numberDiff line numberDiff line change
@@ -1205,27 +1205,24 @@ static char *rewrite_mapfunc_unescape(request_rec *r, char *key)
12051205
static char *select_random_value_part(request_rec *r, char *value)
12061206
{
12071207
char *p = value;
1208-
unsigned n = 1;
1208+
unsigned n = 0;
12091209

12101210
/* count number of distinct values */
12111211
while ((p = ap_strchr(p, '|')) != NULL) {
12121212
++n;
12131213
++p;
12141214
}
12151215

1216-
if (n > 1) {
1217-
n = ap_random_pick(1, n);
1216+
if (n > 0) {
1217+
n = ap_random_pick(0, n);
12181218

1219-
/* extract it from the whole string */
1220-
while (--n && (value = ap_strchr(value, '|')) != NULL) {
1221-
++value;
1222-
}
1223-
1224-
if (value) { /* should not be NULL, but ... */
1225-
p = ap_strchr(value, '|');
1226-
if (p) {
1219+
/* extract the n'th part from the whole string */
1220+
while ((p = ap_strchr(value, '|')) != NULL) {
1221+
if (!n--) {
12271222
*p = '\0';
1223+
break;
12281224
}
1225+
value = p + 1;
12291226
}
12301227
}
12311228

0 commit comments

Comments
 (0)