Mailing List Archive

svn commit: r1916298 - /httpd/httpd/trunk/modules/mappers/mod_rewrite.c
Author: ylavic
Date: Thu Mar 14 14:38:36 2024
New Revision: 1916298

URL: http://svn.apache.org/viewvc?rev=1916298&view=rev
Log:
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.


Modified:
httpd/httpd/trunk/modules/mappers/mod_rewrite.c

Modified: httpd/httpd/trunk/modules/mappers/mod_rewrite.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/mappers/mod_rewrite.c?rev=1916298&r1=1916297&r2=1916298&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/mappers/mod_rewrite.c (original)
+++ httpd/httpd/trunk/modules/mappers/mod_rewrite.c Thu Mar 14 14:38:36 2024
@@ -1205,7 +1205,7 @@ static char *rewrite_mapfunc_unescape(re
static char *select_random_value_part(request_rec *r, char *value)
{
char *p = value;
- unsigned n = 1;
+ unsigned n = 0;

/* count number of distinct values */
while ((p = ap_strchr(p, '|')) != NULL) {
@@ -1213,19 +1213,16 @@ static char *select_random_value_part(re
++p;
}

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

- /* extract it from the whole string */
- while (--n && (value = ap_strchr(value, '|')) != NULL) {
- ++value;
- }
-
- if (value) { /* should not be NULL, but ... */
- p = ap_strchr(value, '|');
- if (p) {
+ /* extract the n'th part from the whole string */
+ while ((p = ap_strchr(value, '|')) != NULL) {
+ if (!n--) {
*p = '\0';
+ break;
}
+ value = p + 1;
}
}