summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/util-linux/util-linux/include-strutils-cleanup-strto-functions.patch
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-core/util-linux/util-linux/include-strutils-cleanup-strto-functions.patch')
-rw-r--r--meta/recipes-core/util-linux/util-linux/include-strutils-cleanup-strto-functions.patch270
1 files changed, 270 insertions, 0 deletions
diff --git a/meta/recipes-core/util-linux/util-linux/include-strutils-cleanup-strto-functions.patch b/meta/recipes-core/util-linux/util-linux/include-strutils-cleanup-strto-functions.patch
new file mode 100644
index 0000000000..5d5a370821
--- /dev/null
+++ b/meta/recipes-core/util-linux/util-linux/include-strutils-cleanup-strto-functions.patch
@@ -0,0 +1,270 @@
1From 84825b161ba5d18da4142893b9789b3fc71284d9 Mon Sep 17 00:00:00 2001
2From: Karel Zak <kzak@redhat.com>
3Date: Tue, 22 Jun 2021 14:20:42 +0200
4Subject: [PATCH] include/strutils: cleanup strto..() functions
5
6* add ul_strtos64() and ul_strtou64()
7* add simple test
8
9Addresses: https://github.com/karelzak/util-linux/issues/1358
10Signed-off-by: Karel Zak <kzak@redhat.com>
11
12Upstream-Backport: [https://github.com/util-linux/util-linux/commit/84825b161ba5d18da4142893b9789b3fc71284d9]
13Signed-off-by: Ranjitsinh Rathod <ranjitsinh.rathod@kpit.com>
14
15---
16 include/strutils.h | 3 +
17 lib/strutils.c | 174 ++++++++++++++++++++++++++-------------------
18 2 files changed, 105 insertions(+), 72 deletions(-)
19
20diff --git a/include/strutils.h b/include/strutils.h
21index e75a2f0e17..389e849905 100644
22--- a/include/strutils.h
23+++ b/include/strutils.h
24@@ -19,6 +19,9 @@ extern int parse_size(const char *str, uintmax_t *res, int *power);
25 extern int strtosize(const char *str, uintmax_t *res);
26 extern uintmax_t strtosize_or_err(const char *str, const char *errmesg);
27
28+extern int ul_strtos64(const char *str, int64_t *num, int base);
29+extern int ul_strtou64(const char *str, uint64_t *num, int base);
30+
31 extern int16_t strtos16_or_err(const char *str, const char *errmesg);
32 extern uint16_t strtou16_or_err(const char *str, const char *errmesg);
33 extern uint16_t strtox16_or_err(const char *str, const char *errmesg);
34diff --git a/lib/strutils.c b/lib/strutils.c
35index ee2c835495..d9976dca70 100644
36--- a/lib/strutils.c
37+++ b/lib/strutils.c
38@@ -319,39 +319,80 @@ char *strndup(const char *s, size_t n)
39 }
40 #endif
41
42-static uint32_t _strtou32_or_err(const char *str, const char *errmesg, int base);
43-static uint64_t _strtou64_or_err(const char *str, const char *errmesg, int base);
44+/*
45+ * convert strings to numbers; returns <0 on error, and 0 on success
46+ */
47+int ul_strtos64(const char *str, int64_t *num, int base)
48+{
49+ char *end = NULL;
50
51-int16_t strtos16_or_err(const char *str, const char *errmesg)
52+ errno = 0;
53+ if (str == NULL || *str == '\0')
54+ return -EINVAL;
55+ *num = (int64_t) strtoimax(str, &end, base);
56+
57+ if (errno || str == end || (end && *end))
58+ return -EINVAL;
59+ return 0;
60+}
61+
62+int ul_strtou64(const char *str, uint64_t *num, int base)
63 {
64- int32_t num = strtos32_or_err(str, errmesg);
65+ char *end = NULL;
66
67- if (num < INT16_MIN || num > INT16_MAX) {
68- errno = ERANGE;
69- err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
70- }
71- return num;
72+ errno = 0;
73+ if (str == NULL || *str == '\0')
74+ return -EINVAL;
75+ *num = (uint64_t) strtoumax(str, &end, base);
76+
77+ if (errno || str == end || (end && *end))
78+ return -EINVAL;
79+ return 0;
80 }
81
82-static uint16_t _strtou16_or_err(const char *str, const char *errmesg, int base)
83+/*
84+ * Covert strings to numbers and print message on error.
85+ *
86+ * Note that hex functions (strtox..()) returns unsigned numbers, if you need
87+ * something else then use ul_strtos64(s, &n, 16).
88+ */
89+int64_t strtos64_or_err(const char *str, const char *errmesg)
90 {
91- uint32_t num = _strtou32_or_err(str, errmesg, base);
92+ int64_t num = 0;
93
94- if (num > UINT16_MAX) {
95- errno = ERANGE;
96- err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
97+ if (ul_strtos64(str, &num, 10) != 0) {
98+ if (errno == ERANGE)
99+ err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
100+
101+ errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
102 }
103 return num;
104 }
105
106-uint16_t strtou16_or_err(const char *str, const char *errmesg)
107+uint64_t strtou64_or_err(const char *str, const char *errmesg)
108 {
109- return _strtou16_or_err(str, errmesg, 10);
110+ uint64_t num = 0;
111+
112+ if (ul_strtou64(str, &num, 10)) {
113+ if (errno == ERANGE)
114+ err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
115+
116+ errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
117+ }
118+ return num;
119 }
120
121-uint16_t strtox16_or_err(const char *str, const char *errmesg)
122+uint64_t strtox64_or_err(const char *str, const char *errmesg)
123 {
124- return _strtou16_or_err(str, errmesg, 16);
125+ uint64_t num = 0;
126+
127+ if (ul_strtou64(str, &num, 16)) {
128+ if (errno == ERANGE)
129+ err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
130+
131+ errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
132+ }
133+ return num;
134 }
135
136 int32_t strtos32_or_err(const char *str, const char *errmesg)
137@@ -365,9 +406,9 @@ int32_t strtos32_or_err(const char *str, const char *errmesg)
138 return num;
139 }
140
141-static uint32_t _strtou32_or_err(const char *str, const char *errmesg, int base)
142+uint32_t strtou32_or_err(const char *str, const char *errmesg)
143 {
144- uint64_t num = _strtou64_or_err(str, errmesg, base);
145+ uint64_t num = strtou64_or_err(str, errmesg);
146
147 if (num > UINT32_MAX) {
148 errno = ERANGE;
149@@ -376,66 +417,48 @@ static uint32_t _strtou32_or_err(const char *str, const char *errmesg, int base)
150 return num;
151 }
152
153-uint32_t strtou32_or_err(const char *str, const char *errmesg)
154-{
155- return _strtou32_or_err(str, errmesg, 10);
156-}
157-
158 uint32_t strtox32_or_err(const char *str, const char *errmesg)
159 {
160- return _strtou32_or_err(str, errmesg, 16);
161+ uint64_t num = strtox64_or_err(str, errmesg);
162+
163+ if (num > UINT32_MAX) {
164+ errno = ERANGE;
165+ err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
166+ }
167+ return num;
168 }
169
170-int64_t strtos64_or_err(const char *str, const char *errmesg)
171+int16_t strtos16_or_err(const char *str, const char *errmesg)
172 {
173- int64_t num;
174- char *end = NULL;
175-
176- errno = 0;
177- if (str == NULL || *str == '\0')
178- goto err;
179- num = strtoimax(str, &end, 10);
180-
181- if (errno || str == end || (end && *end))
182- goto err;
183+ int64_t num = strtos64_or_err(str, errmesg);
184
185- return num;
186-err:
187- if (errno == ERANGE)
188+ if (num < INT16_MIN || num > INT16_MAX) {
189+ errno = ERANGE;
190 err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
191-
192- errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
193+ }
194+ return num;
195 }
196
197-static uint64_t _strtou64_or_err(const char *str, const char *errmesg, int base)
198+uint16_t strtou16_or_err(const char *str, const char *errmesg)
199 {
200- uintmax_t num;
201- char *end = NULL;
202-
203- errno = 0;
204- if (str == NULL || *str == '\0')
205- goto err;
206- num = strtoumax(str, &end, base);
207-
208- if (errno || str == end || (end && *end))
209- goto err;
210+ uint64_t num = strtou64_or_err(str, errmesg);
211
212- return num;
213-err:
214- if (errno == ERANGE)
215+ if (num > UINT16_MAX) {
216+ errno = ERANGE;
217 err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
218-
219- errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
220+ }
221+ return num;
222 }
223
224-uint64_t strtou64_or_err(const char *str, const char *errmesg)
225+uint16_t strtox16_or_err(const char *str, const char *errmesg)
226 {
227- return _strtou64_or_err(str, errmesg, 10);
228-}
229+ uint64_t num = strtox64_or_err(str, errmesg);
230
231-uint64_t strtox64_or_err(const char *str, const char *errmesg)
232-{
233- return _strtou64_or_err(str, errmesg, 16);
234+ if (num > UINT16_MAX) {
235+ errno = ERANGE;
236+ err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
237+ }
238+ return num;
239 }
240
241 double strtod_or_err(const char *str, const char *errmesg)
242@@ -1051,15 +1051,25 @@ static int test_strutils_cmp_paths(int a
243
244 int main(int argc, char *argv[])
245 {
246- if (argc == 3 && strcmp(argv[1], "--size") == 0)
247+ if (argc == 3 && strcmp(argv[1], "--size") == 0) {
248 return test_strutils_sizes(argc - 1, argv + 1);
249
250- else if (argc == 4 && strcmp(argv[1], "--cmp-paths") == 0)
251+ } else if (argc == 4 && strcmp(argv[1], "--cmp-paths") == 0) {
252 return test_strutils_cmp_paths(argc - 1, argv + 1);
253
254+ } else if (argc == 3 && strcmp(argv[1], "--str2num") == 0) {
255+ uint64_t n;
256+
257+ if (ul_strtou64(argv[2], &n, 10) == 0) {
258+ printf("'%s' --> %ju\n", argv[2], (uintmax_t) n);
259+ return EXIT_SUCCESS;
260+ }
261+ }
262+
263 else {
264 fprintf(stderr, "usage: %1$s --size <number>[suffix]\n"
265- " %1$s --cmp-paths <path> <path>\n",
266+ " %1$s --cmp-paths <path> <path>\n"
267+ " %1$s --num2num <str>\n",
268 argv[0]);
269 exit(EXIT_FAILURE);
270 }