Skip to content

Commit 4056286

Browse files
committed
more fixes
1 parent 99f93a8 commit 4056286

File tree

2 files changed

+176
-5
lines changed

2 files changed

+176
-5
lines changed

fuzzing/fuzz_local/zxformat_fuzzer.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,168 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
117117
array_to_hexstr_uppercase(buffer, sizeof(buffer), data, (size > 64) ? 64 : size);
118118
}
119119

120+
// Test bip32_to_str
121+
if (size >= 20) {
122+
char bip32Buffer[256];
123+
uint32_t path[5];
124+
const uint8_t pathLen = (data[0] % 5) + 1;
125+
126+
for (uint8_t i = 0; i < pathLen && static_cast<size_t>(i) * 4 + 4 <= size - 1; i++) {
127+
memcpy(&path[i], data + 1 + static_cast<size_t>(i) * 4, sizeof(uint32_t));
128+
}
129+
130+
bip32_to_str(bip32Buffer, sizeof(bip32Buffer), path, pathLen);
131+
bip32_to_str(bip32Buffer, 10, path, pathLen);
132+
bip32_to_str(bip32Buffer, 5, path, pathLen);
133+
bip32_to_str(bip32Buffer, sizeof(bip32Buffer), path, 0);
134+
bip32_to_str(bip32Buffer, sizeof(bip32Buffer), path, 6);
135+
}
136+
137+
// Test str_to_int8
138+
if (size > 2) {
139+
char str[16];
140+
const size_t str_len = (size > 15) ? 15 : size;
141+
memcpy(str, data, str_len);
142+
str[str_len] = '\0';
143+
144+
const char *end = str + str_len;
145+
char error = 0;
146+
str_to_int8(str, end, &error);
147+
str_to_int8(str, end, NULL);
148+
}
149+
150+
// Test number_inplace_trimming
151+
if (size > 8) {
152+
char numberStr[128];
153+
const size_t copy_len = (size > 127) ? 127 : size;
154+
memcpy(numberStr, data, copy_len);
155+
numberStr[copy_len] = '\0';
156+
157+
const uint8_t non_trimmed = data[0] % 10;
158+
number_inplace_trimming(numberStr, non_trimmed);
159+
160+
// Test with decimal point
161+
strcpy(numberStr, "123.456000");
162+
number_inplace_trimming(numberStr, 2);
163+
164+
strcpy(numberStr, "0.000000");
165+
number_inplace_trimming(numberStr, 0);
166+
}
167+
168+
// Test hexstr_to_array
169+
if (size >= 4) {
170+
uint8_t outputArray[128];
171+
char hexStr[256];
172+
173+
// Create a valid hex string from data
174+
const size_t hexLen = (size > 127) ? 127 : size;
175+
array_to_hexstr(hexStr, sizeof(hexStr), data, hexLen);
176+
177+
hexstr_to_array(outputArray, sizeof(outputArray), hexStr, strlen(hexStr));
178+
hexstr_to_array(outputArray, 10, hexStr, strlen(hexStr));
179+
hexstr_to_array(outputArray, sizeof(outputArray), hexStr, 3);
180+
}
181+
182+
// Test to_uppercase and to_lowercase
183+
if (size >= 1) {
184+
uint8_t letter = data[0];
185+
to_uppercase(&letter);
186+
187+
letter = data[0];
188+
to_lowercase(&letter);
189+
190+
to_uppercase(NULL);
191+
to_lowercase(NULL);
192+
}
193+
194+
// Test array_to_uppercase and array_to_lowercase
195+
if (size >= 4) {
196+
uint8_t upperArray[64];
197+
uint8_t lowerArray[64];
198+
const size_t array_len = (size > 64) ? 64 : size;
199+
200+
memcpy(upperArray, data, array_len);
201+
memcpy(lowerArray, data, array_len);
202+
203+
array_to_uppercase(upperArray, array_len);
204+
array_to_lowercase(lowerArray, array_len);
205+
206+
array_to_uppercase(NULL, array_len);
207+
array_to_lowercase(NULL, array_len);
208+
}
209+
210+
// Test pageStringHex
211+
if (size >= 16) {
212+
char hexOutput[128];
213+
const uint16_t hexOutputLen = sizeof(hexOutput);
214+
215+
const uint16_t inValueLen = (size > 256) ? 256 : size;
216+
const uint8_t pageIdx = data[0] % 10;
217+
uint8_t pageCount = 0;
218+
219+
pageStringHex(hexOutput, hexOutputLen, (const char*)data, inValueLen, pageIdx, &pageCount);
220+
pageStringHex(hexOutput, 10, (const char*)data, inValueLen, pageIdx, &pageCount);
221+
pageStringHex(hexOutput, 1, (const char*)data, inValueLen, pageIdx, &pageCount);
222+
pageStringHex(hexOutput, hexOutputLen, (const char*)data, 0, pageIdx, &pageCount);
223+
}
224+
225+
// Test formatBufferData
226+
if (size >= 8) {
227+
char formattedOutput[256];
228+
const uint16_t outputLen = sizeof(formattedOutput);
229+
230+
const uint64_t dataLen = (size > 500) ? 500 : size;
231+
const uint8_t pageIdx = data[0] % 10;
232+
uint8_t pageCount = 0;
233+
234+
formatBufferData(data, dataLen, formattedOutput, outputLen, pageIdx, &pageCount);
235+
formatBufferData(data, dataLen, formattedOutput, 10, pageIdx, &pageCount);
236+
formatBufferData(data, 501, formattedOutput, outputLen, pageIdx, &pageCount);
237+
238+
// Test with non-ASCII data
239+
uint8_t nonAsciiData[64];
240+
for (size_t i = 0; i < 64 && i < size; i++) {
241+
nonAsciiData[i] = data[i] | 0x80;
242+
}
243+
formatBufferData(nonAsciiData, 64, formattedOutput, outputLen, pageIdx, &pageCount);
244+
}
245+
246+
// Test uint64_from_BEarray
247+
if (size >= 8) {
248+
uint8_t beArray[8];
249+
memcpy(beArray, data, 8);
250+
251+
uint64_t result = uint64_from_BEarray(beArray);
252+
253+
// Test with different patterns
254+
if (size >= 16) {
255+
memcpy(beArray, data + 8, 8);
256+
const uint64_t result2 = uint64_from_BEarray(beArray);
257+
// Use results to avoid dead store warning
258+
if (result > result2) {
259+
result = result2;
260+
}
261+
}
262+
263+
// Test with all zeros
264+
memset(beArray, 0, 8);
265+
const uint64_t zeroResult = uint64_from_BEarray(beArray);
266+
if (zeroResult != 0) {
267+
result = zeroResult;
268+
}
269+
270+
// Test with all ones
271+
memset(beArray, 0xFF, 8);
272+
const uint64_t onesResult = uint64_from_BEarray(beArray);
273+
if (onesResult != 0xFFFFFFFFFFFFFFFF) {
274+
result = onesResult;
275+
}
276+
277+
// Force use of result
278+
if (result == 0) {
279+
beArray[0] = 0;
280+
}
281+
}
282+
120283
return 0;
121284
}

include/zxformat.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,20 @@ __Z_INLINE int8_t str_to_int8(const char *start, const char *end, char *error) {
126126
}
127127

128128
int64_t value = 0;
129-
int multiplier = 1;
130-
for (const char *s = end - 1; s >= start; s--) {
131-
int delta = (*s - '0');
129+
const int64_t max_value = INT64_MAX;
130+
131+
for (const char *s = start; s < end; s++) {
132+
int64_t delta = (*s - '0');
132133
if (delta >= 0 && delta <= 9) {
133-
value += (delta * multiplier);
134-
multiplier *= 10;
134+
// Check for overflow before multiplication and addition
135+
if (value > (max_value - delta) / 10) {
136+
if (error != NULL) {
137+
*error = 1;
138+
}
139+
return 0;
140+
}
141+
142+
value = value * 10 + delta;
135143
} else {
136144
if (error != NULL) {
137145
*error = 1;

0 commit comments

Comments
 (0)