1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-05 17:23:15 +02:00

api: restrict number to integer in function string_parse_size

This commit is contained in:
Sébastien Helleu
2022-09-25 13:39:11 +02:00
parent 4d74a89cfc
commit e7b6e8c60f
8 changed files with 36 additions and 45 deletions
+2 -2
View File
@@ -1970,8 +1970,8 @@ unsigned long long weechat_string_parse_size (const char *size);
Arguments:
* _size_: the size as string: float number followed optional spaces and optional
unit (lower or upper case), which is one of:
* _size_: the size as string: positive integer number followed by optional
spaces and optional unit (lower or upper case), which is one of:
** _b_: bytes
** _k_: kilobytes (1k = 1000 bytes)
** _m_: megabytes (1m = 1000k = 1,000,000 bytes)
+3 -3
View File
@@ -2006,9 +2006,9 @@ unsigned long long weechat_string_parse_size (const char *size);
Paramètres :
* _size_ : la taille sous forme de chaîne : nombre décimal suivi par des espaces
optionnels et une unité optionnelle (en minuscules ou majuscules), qui est une
des suivantes :
* _size_ : la taille sous forme de chaîne : nombre entier positif suivi par des
espaces optionnels et une unité optionnelle (en minuscules ou majuscules),
qui est une des suivantes :
** _b_ : octets
** _k_ : kilo-octets (1k = 1000 octets)
** _m_ : méga-octets (1m = 1000k = 1 000 000 octets)
+2 -2
View File
@@ -2060,8 +2060,8 @@ unsigned long long weechat_string_parse_size (const char *size);
Argomenti:
* _size_: the size as string: float number followed optional spaces and optional
unit (lower or upper case), which is one of:
* _size_: the size as string: positive integer number followed by optional
spaces and optional unit (lower or upper case), which is one of:
** _b_: bytes
** _k_: kilobytes (1k = 1000 bytes)
** _m_: megabytes (1m = 1000k = 1,000,000 bytes)
+2 -2
View File
@@ -1997,8 +1997,8 @@ unsigned long long weechat_string_parse_size (const char *size);
引数:
* _size_: the size as string: float number followed optional spaces and optional
unit (lower or upper case), which is one of:
* _size_: the size as string: positive integer number followed by optional
spaces and optional unit (lower or upper case), which is one of:
** _b_: bytes
** _k_: kilobytes (1k = 1000 bytes)
** _m_: megabytes (1m = 1000k = 1,000,000 bytes)
+2 -2
View File
@@ -1891,8 +1891,8 @@ unsigned long long weechat_string_parse_size (const char *size);
Аргументи:
* _size_: the size as string: float number followed optional spaces and optional
unit (lower or upper case), which is one of:
* _size_: the size as string: positive integer number followed by optional
spaces and optional unit (lower or upper case), which is one of:
** _b_: bytes
** _k_: kilobytes (1k = 1000 bytes)
** _m_: megabytes (1m = 1000k = 1,000,000 bytes)
+12 -13
View File
@@ -2981,9 +2981,9 @@ string_format_size (unsigned long long size)
/*
* Parses a string with a size and returns the size in bytes.
*
* The format is "123" or "123X" or "123 X" where 123 is any positive number
* as a float (for example: 123 or 4.56) and X the unit, which can be one of
* (lower or upper case are accepted):
* The format is "123" or "123x" or "123 x" where "123" is any positive
* integer number and "x" the unit, which can be one of (lower or upper case
* are accepted):
*
* b bytes (default if unit is missing)
* k kilobytes (1k = 1000 bytes)
@@ -2991,9 +2991,6 @@ string_format_size (unsigned long long size)
* g gigabytes (1g = 1000m = 1,000,000,000 bytes)
* t terabytes (1t = 1000g = 1,000,000,000,000 bytes)
*
* Note: decimals of the float number are ignored if the unit is bytes
* (eg: "5.9" or "5.9B" returns 5).
*
* Returns the parsed size, 0 if error.
*/
@@ -3002,7 +2999,7 @@ string_parse_size (const char *size)
{
const char *pos;
char *str_number, *error;
double number;
long long number;
unsigned long long result;
str_number = NULL;
@@ -3012,7 +3009,7 @@ string_parse_size (const char *size)
goto end;
pos = size;
while ((pos[0] == '.') || isdigit (pos[0]))
while (isdigit (pos[0]))
{
pos++;
}
@@ -3024,9 +3021,11 @@ string_parse_size (const char *size)
if (!str_number)
goto end;
number = strtod (str_number, &error);
number = strtoll (str_number, &error, 10);
if (!error || error[0])
goto end;
if (number < 0)
goto end;
while (pos[0] == ' ')
{
@@ -3047,19 +3046,19 @@ string_parse_size (const char *size)
break;
case 'k':
case 'K':
result = number * 1000.0;
result = number * 1000ULL;
break;
case 'm':
case 'M':
result = number * 1000.0 * 1000.0;
result = number * 1000ULL * 1000ULL;
break;
case 'g':
case 'G':
result = number * 1000.0 * 1000.0 * 1000.0;
result = number * 1000ULL * 1000ULL * 1000ULL;
break;
case 't':
case 'T':
result = number * 1000.0 * 1000.0 * 1000.0 * 1000.0;
result = number * 1000ULL * 1000ULL * 1000ULL * 1000ULL;
break;
}
+3 -3
View File
@@ -79,9 +79,9 @@ def test_strings():
check(weechat.string_parse_size('1') == 1)
check(weechat.string_parse_size('12b') == 12)
check(weechat.string_parse_size('123 b') == 123)
check(weechat.string_parse_size('1.34k') == 1340)
check(weechat.string_parse_size('1.5m') == 1500000)
check(weechat.string_parse_size('2.1g') == 2100000000)
check(weechat.string_parse_size('120k') == 120000)
check(weechat.string_parse_size('1500m') == 1500000000)
check(weechat.string_parse_size('3g') == 3000000000)
check(weechat.string_color_code_size('') == 0)
check(weechat.string_color_code_size('test') == 0)
str_color = weechat.color('yellow,red')
+10 -18
View File
@@ -1942,11 +1942,6 @@ TEST(CoreString, ParseSize)
CHECK(string_parse_size ("42b") == 42ULL);
CHECK(string_parse_size ("42B") == 42ULL);
/* decimals ignored for bytes */
CHECK(string_parse_size ("42.9") == 42ULL);
CHECK(string_parse_size ("42.9b") == 42ULL);
CHECK(string_parse_size ("42.9B") == 42ULL);
CHECK(string_parse_size ("999") == 999ULL);
CHECK(string_parse_size ("999b") == 999ULL);
CHECK(string_parse_size ("999B") == 999ULL);
@@ -1958,29 +1953,26 @@ TEST(CoreString, ParseSize)
CHECK(string_parse_size ("1k") == 1000ULL);
CHECK(string_parse_size ("1K") == 1000ULL);
CHECK(string_parse_size ("1.34k") == 1340ULL);
CHECK(string_parse_size ("1.34K") == 1340ULL);
CHECK(string_parse_size ("14.08k") == 14080ULL);
CHECK(string_parse_size ("14.08K") == 14080ULL);
CHECK(string_parse_size ("12k") == 12000ULL);
CHECK(string_parse_size ("12K") == 12000ULL);
CHECK(string_parse_size ("1m") == 1000000ULL);
CHECK(string_parse_size ("1M") == 1000000ULL);
CHECK(string_parse_size ("1.5m") == 1500000ULL);
CHECK(string_parse_size ("1.5M") == 1500000ULL);
CHECK(string_parse_size ("30m") == 30000000ULL);
CHECK(string_parse_size ("30M") == 30000000ULL);
CHECK(string_parse_size ("1g") == 1000000000ULL);
CHECK(string_parse_size ("1G") == 1000000000ULL);
CHECK(string_parse_size ("1.2345g") == 1234500000ULL);
CHECK(string_parse_size ("1.2345G") == 1234500000ULL);
CHECK(string_parse_size ("1234m") == 1234000000ULL);
CHECK(string_parse_size ("1234m") == 1234000000ULL);
CHECK(string_parse_size ("1t") == 1000000000000ULL);
CHECK(string_parse_size ("1T") == 1000000000000ULL);
CHECK(string_parse_size ("15g") == 15000000000ULL);
CHECK(string_parse_size ("15G") == 15000000000ULL);
CHECK(string_parse_size ("1.23456789t") == 1234567890000ULL);
CHECK(string_parse_size ("1.23456789T") == 1234567890000ULL);
CHECK(string_parse_size ("8t") == 8000000000000ULL);
CHECK(string_parse_size ("8T") == 8000000000000ULL);
}
/*