Skip to content

Commit c3e1867

Browse files
committed
fixes
1 parent 2ef7541 commit c3e1867

File tree

4 files changed

+105
-43
lines changed

4 files changed

+105
-43
lines changed

fuzzing/Makefile

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,24 @@ fuzz_report_html:
152152
@echo "Generating unified coverage report for all fuzzers..."
153153
@rm -rf $(FUZZ_COVERAGE_DIR)/report_html_unified
154154
@# Generate HTML report with all fuzzer binaries
155-
@llvm-cov show \
156-
$(FUZZ_BUILD_DIR)/fuzz-base58 \
157-
-object $(FUZZ_BUILD_DIR)/fuzz-base64 \
158-
-object $(FUZZ_BUILD_DIR)/fuzz-bech32 \
159-
-object $(FUZZ_BUILD_DIR)/fuzz-hexutils \
160-
-object $(FUZZ_BUILD_DIR)/fuzz-segwit_addr \
161-
-object $(FUZZ_BUILD_DIR)/fuzz-bignum \
162-
-object $(FUZZ_BUILD_DIR)/fuzz-zxformat \
163-
-object $(FUZZ_BUILD_DIR)/fuzz-timeutils \
164-
-instr-profile=$(FUZZ_COVERAGE_DIR)/coverage.profdata \
165-
-format=html \
166-
-output-dir=$(FUZZ_COVERAGE_DIR)/report_html_unified \
167-
-show-line-counts-or-regions \
168-
-show-instantiations \
169-
-show-expansions
155+
@FUZZ_BINARIES=($(FUZZ_BUILD_DIR)/fuzz-*); \
156+
if [ -e "$${FUZZ_BINARIES[0]}" ]; then \
157+
FIRST_BINARY="$${FUZZ_BINARIES[0]}"; \
158+
OBJECT_FLAGS=""; \
159+
for bin in "$${FUZZ_BINARIES[@]:1}"; do \
160+
OBJECT_FLAGS="$$OBJECT_FLAGS -object $$bin"; \
161+
done; \
162+
llvm-cov show $$FIRST_BINARY $$OBJECT_FLAGS \
163+
-instr-profile=$(FUZZ_COVERAGE_DIR)/coverage.profdata \
164+
-format=html \
165+
-output-dir=$(FUZZ_COVERAGE_DIR)/report_html_unified \
166+
-show-line-counts-or-regions \
167+
-show-instantiations \
168+
-show-expansions; \
169+
else \
170+
echo "No fuzz binaries found in $(FUZZ_BUILD_DIR)"; \
171+
exit 1; \
172+
fi
170173
@echo "Unified HTML coverage report generated in $(FUZZ_COVERAGE_DIR)/report_html_unified"
171174
@# Open report in default browser (cross-platform)
172175
@open $(FUZZ_COVERAGE_DIR)/report_html_unified/index.html 2>/dev/null || \

fuzzing/fuzz_local/base58_fuzzer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include <stddef.h>
22
#include <stdint.h>
33
#include <string.h>
4-
5-
#include <iostream>
4+
#include <vector>
65

76
extern "C" {
87
#include "base58.h"

include/zxformat.h

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
extern "C" {
2020
#endif
2121

22+
#include <stdbool.h>
2223
#include "zxerror.h"
2324
#include "zxmacros.h"
2425

@@ -118,19 +119,18 @@ __Z_INLINE void bip32_to_str(char *s, uint32_t max, const uint32_t *path, uint8_
118119

119120
__Z_INLINE void bip44_to_str(char *s, uint32_t max, const uint32_t path[5]) { bip32_to_str(s, max, path, 5); }
120121

121-
__Z_INLINE int8_t str_to_int8(const char *start, const char *end, char *error) {
122-
int sign = 1;
123-
if (*start == '-') {
124-
sign = -1;
125-
start++;
122+
__Z_INLINE uint64_t parse_digits_to_uint64(const char *start, const char *end, uint64_t limit, char *error) {
123+
if (error != NULL) {
124+
*error = 0;
126125
}
127-
126+
128127
uint64_t value = 0;
129-
const uint64_t limit = (sign < 0) ? ((uint64_t)INT64_MAX + 1u) : (uint64_t)INT64_MAX;
128+
bool has_digits = false;
130129

131130
for (const char *s = start; s < end; s++) {
132131
uint64_t delta = (uint64_t)(*s - '0');
133132
if (delta <= 9u) {
133+
has_digits = true;
134134
// Check for overflow before multiplication and addition
135135
if (value > (limit - delta) / 10u) {
136136
if (error != NULL) {
@@ -147,6 +147,32 @@ __Z_INLINE int8_t str_to_int8(const char *start, const char *end, char *error) {
147147
}
148148
}
149149

150+
// Check if no digits were processed
151+
if (!has_digits) {
152+
if (error != NULL) {
153+
*error = 1;
154+
}
155+
return 0;
156+
}
157+
158+
return value;
159+
}
160+
161+
__Z_INLINE int8_t str_to_int8(const char *start, const char *end, char *error) {
162+
int sign = 1;
163+
if (*start == '-') {
164+
sign = -1;
165+
start++;
166+
}
167+
168+
const uint64_t limit = (sign < 0) ? ((uint64_t)INT64_MAX + 1u) : (uint64_t)INT64_MAX;
169+
uint64_t value = parse_digits_to_uint64(start, end, limit, error);
170+
171+
// If parsing failed, error is already set by the helper function
172+
if (error != NULL && *error != 0) {
173+
return 0;
174+
}
175+
150176
int64_t signed_value;
151177
if (sign < 0) {
152178
if (value == ((uint64_t)INT64_MAX + 1u)) {
@@ -174,26 +200,12 @@ __Z_INLINE int64_t str_to_int64(const char *start, const char *end, char *error)
174200
start++;
175201
}
176202

177-
uint64_t value = 0;
178203
const uint64_t limit = (sign < 0) ? ((uint64_t)INT64_MAX + 1u) : (uint64_t)INT64_MAX;
179-
180-
for (const char *s = start; s < end; s++) {
181-
uint64_t delta = (uint64_t)(*s - '0');
182-
if (delta <= 9u) {
183-
// Check for overflow before multiplication and addition
184-
if (value > (limit - delta) / 10u) {
185-
if (error != NULL) {
186-
*error = 1;
187-
}
188-
return 0;
189-
}
190-
value = value * 10u + delta;
191-
} else {
192-
if (error != NULL) {
193-
*error = 1;
194-
}
195-
return 0;
196-
}
204+
uint64_t value = parse_digits_to_uint64(start, end, limit, error);
205+
206+
// If parsing failed, error is already set by the helper function
207+
if (error != NULL && *error != 0) {
208+
return 0;
197209
}
198210

199211
if (sign < 0) {

tests/macros.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,30 @@ TEST(STR_TO_INT8, DummyData_Negative) {
436436
EXPECT_EQ(1, error);
437437
}
438438

439+
TEST(STR_TO_INT8, EmptyString) {
440+
const char* empty = "";
441+
char error = 0;
442+
int8_t result = str_to_int8(empty, empty, &error);
443+
EXPECT_EQ(0, result);
444+
EXPECT_EQ(1, error); // Should set error flag for empty string
445+
}
446+
447+
TEST(STR_TO_INT8, JustMinusSign) {
448+
const char* just_minus = "-";
449+
char error = 0;
450+
int8_t result = str_to_int8(just_minus, just_minus + 1, &error);
451+
EXPECT_EQ(0, result);
452+
EXPECT_EQ(1, error); // Should set error flag for just minus sign
453+
}
454+
455+
TEST(STR_TO_INT8, NoDigits) {
456+
const char* no_digits = "abc";
457+
char error = 0;
458+
int8_t result = str_to_int8(no_digits, no_digits + 3, &error);
459+
EXPECT_EQ(0, result);
460+
EXPECT_EQ(1, error); // Should set error flag for non-digit characters
461+
}
462+
439463
TEST(STR_TO_INT64, Min) {
440464
char numberStr[] = "-9223372036854775808";
441465
char error = 0;
@@ -518,6 +542,30 @@ TEST(STR_TO_INT64, MixedInvalidCharacters) {
518542
EXPECT_EQ(1, error);
519543
}
520544

545+
TEST(STR_TO_INT64, EmptyString) {
546+
const char* empty = "";
547+
char error = 0;
548+
int64_t result = str_to_int64(empty, empty, &error);
549+
EXPECT_EQ(0, result);
550+
EXPECT_EQ(1, error); // Should set error flag for empty string
551+
}
552+
553+
TEST(STR_TO_INT64, JustMinusSign) {
554+
const char* just_minus = "-";
555+
char error = 0;
556+
int64_t result = str_to_int64(just_minus, just_minus + 1, &error);
557+
EXPECT_EQ(0, result);
558+
EXPECT_EQ(1, error); // Should set error flag for just minus sign
559+
}
560+
561+
TEST(STR_TO_INT64, ErrorParameterInitialization) {
562+
char numberStr[] = "42";
563+
char error = 99; // Initialize with non-zero value
564+
int64_t result = str_to_int64(numberStr, numberStr + strlen(numberStr), &error);
565+
EXPECT_EQ(42, result);
566+
EXPECT_EQ(0, error); // Error should be initialized to 0 for valid input
567+
}
568+
521569
TEST(CASE_CONVERSION, ToUppercase) {
522570
uint8_t letter = 'a';
523571
zxerr_t result = to_uppercase(&letter);

0 commit comments

Comments
 (0)