strparse: switch to curl_off_t as base data type

- add hex and octal parsers to the Curl_str_* family
- make curlx_strtoofft use these parsers
- remove all use of strtol() and strtoul() in library code
- generally use Curl_str_* more than strtoofft, for stricter parsing
- supports 64-bit universally, instead of 'long' which differs in size
  between platforms

Extended the unit test 1664 to verify hex and octal parsing.

Closes #16336
This commit is contained in:
Daniel Stenberg 2025-02-14 11:29:08 +01:00
parent 876db1070b
commit b4538ec522
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
46 changed files with 538 additions and 497 deletions

View file

@ -97,9 +97,9 @@ Curl_str_number
10: (" 123") 8, [0] line 0
11: ("") 8, [0] line 0
Curl_str_number / max
0: ("9223372036854775808") 0, [9223372036854775808] line 19
1: ("9223372036854775809") 0, [9223372036854775809] line 19
2: ("18446744073709551615") 0, [18446744073709551615] line 20
0: ("9223372036854775807") 0, [9223372036854775807] line 19
1: ("9223372036854775808") 7, [0] line 18
2: ("18446744073709551615") 7, [0] line 19
3: ("18446744073709551616") 7, [0] line 19
4: ("18446744073709551617") 7, [0] line 19
Curl_str_newline
@ -115,6 +115,38 @@ Curl_str_newline
8: ("
") 0, line 1
9: ("") 6, line 0
Curl_str_hex
0: ("1") 0, [1] line 1
1: ("1000") 0, [4096] line 4
2: ("1234") 0, [4660] line 4
3: ("1235") 0, [4661] line 4
4: ("1236") 1, [0] line 3
5: ("01234") 0, [4660] line 5
6: ("00000000000000000000000000001234") 0, [4660] line 32
7: ("0123 345") 0, [291] line 4
8: ("0123O345") 0, [291] line 4
9: ("-12") 8, [0] line 0
10: (" 123") 8, [0] line 0
11: ("") 8, [0] line 0
Curl_str_octal
0: ("1") 0, [1] line 1
1: ("1000") 0, [512] line 4
2: ("1234") 0, [668] line 4
3: ("1235") 0, [669] line 4
4: ("1236") 1, [0] line 3
5: ("01234") 0, [668] line 5
6: ("00000000000000000000000000001234") 0, [668] line 32
7: ("0123 345") 0, [83] line 4
8: ("0123O345") 0, [83] line 4
9: ("-12") 8, [0] line 0
10: (" 123") 8, [0] line 0
11: ("") 8, [0] line 0
Curl_str_octal / max
0: ("777777777777777777777") 0, [9223372036854775807] line 21
1: ("1000000000000000000000") 7, [0] line 21
Curl_str_hex / max
0: ("7FFFFFFFFFFFFFFF") 0, [9223372036854775807] line 16
1: ("8000000000000000") 7, [0] line 15
</stdout>
</verify>
</testcase>

View file

@ -29,6 +29,8 @@ CURLX_SRCS = \
../../lib/mprintf.c \
../../lib/nonblock.c \
../../lib/strtoofft.c \
../../lib/strparse.c \
../../lib/strequal.c \
../../lib/warnless.c \
../../lib/timediff.c \
../../lib/dynbuf.c \
@ -41,10 +43,12 @@ CURLX_HDRS = \
../../lib/curlx.h \
../../lib/nonblock.h \
../../lib/strtoofft.h \
../../lib/strcase.h \
../../lib/warnless.h \
../../lib/timediff.h \
../../lib/curl_ctype.h \
../../lib/dynbuf.h \
../../lib/strcase.h \
../../lib/strdup.h \
../../lib/curl_get_line.h \
../../lib/curl_multibyte.h

View file

@ -196,7 +196,7 @@ UNITTEST_START
};
printf("Curl_str_number\n");
for(i = 0; nums[i]; i++) {
size_t num;
curl_off_t num;
const char *line = nums[i];
const char *orgline = line;
int rc = Curl_str_number(&line, &num, 1235);
@ -206,10 +206,10 @@ UNITTEST_START
}
{
/* SIZE_T_MAX is typically 18446744073709551615 */
/* CURL_OFF_T is typically 9223372036854775807 */
static const char *nums[] = {
"9223372036854775808", /* 2^63 */
"9223372036854775809", /* 2^63 + 1 */
"9223372036854775807", /* 2^63 -1 */
"9223372036854775808", /* 2^63 */
"18446744073709551615", /* 2^64 - 1 */
"18446744073709551616", /* 2^64 */
"18446744073709551617", /* 2^64 + 1 */
@ -217,11 +217,11 @@ UNITTEST_START
};
printf("Curl_str_number / max\n");
for(i = 0; nums[i]; i++) {
size_t num;
curl_off_t num;
const char *line = nums[i];
const char *orgline = line;
int rc = Curl_str_number(&line, &num, SIZE_T_MAX);
printf("%u: (\"%s\") %d, [%zu] line %d\n",
int rc = Curl_str_number(&line, &num, CURL_OFF_T_MAX);
printf("%u: (\"%s\") %d, [%" CURL_FORMAT_CURL_OFF_T "] line %d\n",
i, orgline, rc, num, (int)(line - orgline));
}
}
@ -250,5 +250,95 @@ UNITTEST_START
}
}
{
static const char *nums[] = {
"1",
"1000",
"1234",
"1235",
"1236",
"01234",
"00000000000000000000000000001234",
"0123 345",
"0123O345",
"-12",
" 123",
"",
NULL
};
printf("Curl_str_hex\n");
for(i = 0; nums[i]; i++) {
curl_off_t num;
const char *line = nums[i];
const char *orgline = line;
int rc = Curl_str_hex(&line, &num, 0x1235);
printf("%u: (\"%s\") %d, [%u] line %d\n",
i, orgline, rc, (int)num, (int)(line - orgline));
}
}
{
static const char *nums[] = {
"1",
"1000",
"1234",
"1235",
"1236",
"01234",
"00000000000000000000000000001234",
"0123 345",
"0123O345",
"-12",
" 123",
"",
NULL
};
printf("Curl_str_octal\n");
for(i = 0; nums[i]; i++) {
curl_off_t num;
const char *line = nums[i];
const char *orgline = line;
int rc = Curl_str_octal(&line, &num, 01235);
printf("%u: (\"%s\") %d, [%u] line %d\n",
i, orgline, rc, (int)num, (int)(line - orgline));
}
}
{
/* CURL_OFF_T is typically 2^63-1 */
static const char *nums[] = {
"777777777777777777777", /* 2^63 -1 */
"1000000000000000000000", /* 2^63 */
NULL
};
printf("Curl_str_octal / max\n");
for(i = 0; nums[i]; i++) {
curl_off_t num;
const char *line = nums[i];
const char *orgline = line;
int rc = Curl_str_octal(&line, &num, CURL_OFF_T_MAX);
printf("%u: (\"%s\") %d, [%" CURL_FORMAT_CURL_OFF_T "] line %d\n",
i, orgline, rc, num, (int)(line - orgline));
}
}
{
/* CURL_OFF_T is typically 2^63-1 */
static const char *nums[] = {
"7FFFFFFFFFFFFFFF", /* 2^63 -1 */
"8000000000000000", /* 2^63 */
NULL
};
printf("Curl_str_hex / max\n");
for(i = 0; nums[i]; i++) {
curl_off_t num;
const char *line = nums[i];
const char *orgline = line;
int rc = Curl_str_hex(&line, &num, CURL_OFF_T_MAX);
printf("%u: (\"%s\") %d, [%" CURL_FORMAT_CURL_OFF_T "] line %d\n",
i, orgline, rc, num, (int)(line - orgline));
}
}
}
UNITTEST_STOP