1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* parse_int.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#include "util/parse.h"
#include "util/test.h"
#include "sqfs/error.h"
int main(int argc, char **argv)
{
sqfs_s64 s_out;
sqfs_u64 out;
size_t diff;
int ret;
(void)argc; (void)argv;
/* must begin with a digit */
ret = parse_uint("a1234", -1, &diff, 0, 0, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED);
/* can end with a non-digit... */
ret = parse_uint("1234a", -1, &diff, 0, 0, &out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(out, 1234);
TEST_EQUAL_UI(diff, 4);
/* ...unless diff is NULL */
ret = parse_uint("1234a", -1, NULL, 0, 0, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED);
/* numeric overflow is cought */
ret = parse_uint("18446744073709551616", -1, NULL, 0, 0, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_OVERFLOW);
/* buffer length is adherered to */
ret = parse_uint("18446744073709551616", 5, NULL, 0, 0, &out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(out, 18446);
ret = parse_uint("18446744073709551616", 5, &diff, 0, 0, &out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(diff, 5);
TEST_EQUAL_UI(out, 18446);
/* if vmin/vmax differ, check the range */
ret = parse_uint("1234", -1, NULL, 0, 1000, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_OUT_OF_BOUNDS);
ret = parse_uint("1234", -1, NULL, 0, 2000, &out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(out, 1234);
ret = parse_uint("1234", -1, NULL, 2000, 3000, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_OUT_OF_BOUNDS);
/* int version accepts '-' prefix */
ret = parse_int("1234", -1, NULL, 0, 0, &s_out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_I(s_out, 1234);
ret = parse_int("-1234", -1, NULL, 0, 0, &s_out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_I(s_out, -1234);
ret = parse_int("- 1234", -1, NULL, 0, 0, &s_out);
TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED);
ret = parse_int("+1234", -1, NULL, 0, 0, &s_out);
TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED);
ret = parse_int("-1234", -1, NULL, -1000, 1000, &s_out);
TEST_EQUAL_I(ret, SQFS_ERROR_OUT_OF_BOUNDS);
/**** octal version *****/
/* must begin with a digit */
ret = parse_uint_oct("a1234", -1, &diff, 0, 0, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED);
/* can end with a non-digit... */
ret = parse_uint_oct("1234a", -1, &diff, 0, 0, &out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(out, 01234);
TEST_EQUAL_UI(diff, 4);
/* ...unless diff is NULL */
ret = parse_uint_oct("1234a", -1, NULL, 0, 0, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_CORRUPTED);
/* numeric overflow is cought */
ret = parse_uint_oct("2000000000000000000000", -1, NULL, 0, 0, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_OVERFLOW);
/* buffer length is adherered to */
ret = parse_uint_oct("2000000000000000000000", 5, NULL, 0, 0, &out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(out, 020000);
ret = parse_uint_oct("2000000000000000000000", 5, &diff, 0, 0, &out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(diff, 5);
TEST_EQUAL_UI(out, 020000);
/* if vmin/vmax differ, check the range */
ret = parse_uint_oct("1234", -1, NULL, 0, 01000, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_OUT_OF_BOUNDS);
ret = parse_uint_oct("1234", -1, NULL, 0, 02000, &out);
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(out, 01234);
ret = parse_uint_oct("1234", -1, NULL, 02000, 03000, &out);
TEST_EQUAL_I(ret, SQFS_ERROR_OUT_OF_BOUNDS);
return EXIT_SUCCESS;
}
|