summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2022-09-25 13:39:11 +0200
committerSébastien Helleu <flashcode@flashtux.org>2022-09-27 15:50:43 +0200
commite7b6e8c60f0001c4b228bda570ec0d1b7dc3a739 (patch)
tree2c1c1b9814fe64f456c5dfefb76ecc8d5dc0c4a9
parent4d74a89cfcdce4d9207f51c786a2a1fc278c5a11 (diff)
downloadweechat-e7b6e8c60f0001c4b228bda570ec0d1b7dc3a739.zip
api: restrict number to integer in function string_parse_size
-rw-r--r--doc/en/weechat_plugin_api.en.adoc4
-rw-r--r--doc/fr/weechat_plugin_api.fr.adoc6
-rw-r--r--doc/it/weechat_plugin_api.it.adoc4
-rw-r--r--doc/ja/weechat_plugin_api.ja.adoc4
-rw-r--r--doc/sr/weechat_plugin_api.sr.adoc4
-rw-r--r--src/core/wee-string.c25
-rw-r--r--tests/scripts/python/testapi.py6
-rw-r--r--tests/unit/core/test-core-string.cpp28
8 files changed, 36 insertions, 45 deletions
diff --git a/doc/en/weechat_plugin_api.en.adoc b/doc/en/weechat_plugin_api.en.adoc
index 5d46dd205..b6093fd52 100644
--- a/doc/en/weechat_plugin_api.en.adoc
+++ b/doc/en/weechat_plugin_api.en.adoc
@@ -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)
diff --git a/doc/fr/weechat_plugin_api.fr.adoc b/doc/fr/weechat_plugin_api.fr.adoc
index 790fdf10c..f6d6af359 100644
--- a/doc/fr/weechat_plugin_api.fr.adoc
+++ b/doc/fr/weechat_plugin_api.fr.adoc
@@ -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)
diff --git a/doc/it/weechat_plugin_api.it.adoc b/doc/it/weechat_plugin_api.it.adoc
index e3f94f74e..ddd9c2d4b 100644
--- a/doc/it/weechat_plugin_api.it.adoc
+++ b/doc/it/weechat_plugin_api.it.adoc
@@ -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)
diff --git a/doc/ja/weechat_plugin_api.ja.adoc b/doc/ja/weechat_plugin_api.ja.adoc
index 0a1b8516b..755974b45 100644
--- a/doc/ja/weechat_plugin_api.ja.adoc
+++ b/doc/ja/weechat_plugin_api.ja.adoc
@@ -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)
diff --git a/doc/sr/weechat_plugin_api.sr.adoc b/doc/sr/weechat_plugin_api.sr.adoc
index 36b189a3a..a1e761a5f 100644
--- a/doc/sr/weechat_plugin_api.sr.adoc
+++ b/doc/sr/weechat_plugin_api.sr.adoc
@@ -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)
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index 44d0dd9cf..6afbd7774 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -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;
}
diff --git a/tests/scripts/python/testapi.py b/tests/scripts/python/testapi.py
index 6d3833a1a..5e9df9a13 100644
--- a/tests/scripts/python/testapi.py
+++ b/tests/scripts/python/testapi.py
@@ -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')
diff --git a/tests/unit/core/test-core-string.cpp b/tests/unit/core/test-core-string.cpp
index d7b4612ea..16ccf36a1 100644
--- a/tests/unit/core/test-core-string.cpp
+++ b/tests/unit/core/test-core-string.cpp
@@ -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);
}
/*