Skip to content

Commit cffdd6b

Browse files
committed
Sanitizers
Some UB elimination
1 parent 0e11769 commit cffdd6b

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

ffc.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,13 +1723,15 @@ void ffc_sv_normalize(ffc_sv* sv) {
17231723

17241724
ffc_internal ffc_inline
17251725
uint64_t ffc_uint64_hi64_1(uint64_t r0, bool* truncated) {
1726+
FFC_DEBUG_ASSERT(r0 != 0);
17261727
*truncated = false;
17271728
int shl = (int)ffc_count_leading_zeroes(r0);
17281729
return r0 << shl;
17291730
}
17301731

17311732
ffc_internal ffc_inline
17321733
uint64_t ffc_uint64_hi64_2(uint64_t r0, uint64_t r1, bool* truncated) {
1734+
FFC_DEBUG_ASSERT(r0 != 0);
17331735
int shl = (int)ffc_count_leading_zeroes(r0);
17341736
if (shl == 0) {
17351737
*truncated = r1 != 0;
@@ -3135,28 +3137,28 @@ float ffc_parse_float_simple(size_t len, const char *s, ffc_outcome *outcome) {
31353137

31363138
ffc_result ffc_parse_i64(size_t len, const char *input, int base, int64_t *out) {
31373139
char *pend = (char*)(input + len);
3138-
ffc_int_value value_out;
3140+
ffc_int_value value_out = {0};
31393141
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_S64, ffc_parse_options_default(), base);
31403142
*out = value_out.s64;
31413143
return result;
31423144
}
31433145
ffc_result ffc_parse_u64(size_t len, const char *input, int base, uint64_t *out) {
31443146
char *pend = (char*)(input + len);
3145-
ffc_int_value value_out;
3147+
ffc_int_value value_out = {0};
31463148
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_U64, ffc_parse_options_default(), base);
31473149
*out = value_out.u64;
31483150
return result;
31493151
}
31503152
ffc_result ffc_parse_i32(size_t len, const char *input, int base, int32_t *out) {
31513153
char *pend = (char*)(input + len);
3152-
ffc_int_value value_out;
3154+
ffc_int_value value_out = {0};
31533155
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_S32, ffc_parse_options_default(), base);
31543156
*out = value_out.s32;
31553157
return result;
31563158
}
31573159
ffc_result ffc_parse_u32(size_t len, const char *input, int base, uint32_t *out) {
31583160
char *pend = (char*)(input + len);
3159-
ffc_int_value value_out;
3161+
ffc_int_value value_out = {0};
31603162
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_U32, ffc_parse_options_default(), base);
31613163
*out = value_out.u32;
31623164
return result;
@@ -3202,4 +3204,3 @@ ffc_result ffc_parse_u32(size_t len, const char *input, int base, uint32_t *out)
32023204

32033205
#endif /* FFC_H */
32043206

3205-

src/bigint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ void ffc_sv_normalize(ffc_sv* sv) {
158158

159159
ffc_internal ffc_inline
160160
uint64_t ffc_uint64_hi64_1(uint64_t r0, bool* truncated) {
161+
FFC_DEBUG_ASSERT(r0 != 0);
161162
*truncated = false;
162163
int shl = (int)ffc_count_leading_zeroes(r0);
163164
return r0 << shl;
164165
}
165166

166167
ffc_internal ffc_inline
167168
uint64_t ffc_uint64_hi64_2(uint64_t r0, uint64_t r1, bool* truncated) {
169+
FFC_DEBUG_ASSERT(r0 != 0);
168170
int shl = (int)ffc_count_leading_zeroes(r0);
169171
if (shl == 0) {
170172
*truncated = r1 != 0;

src/ffc.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,28 +419,28 @@ float ffc_parse_float_simple(size_t len, const char *s, ffc_outcome *outcome) {
419419

420420
ffc_result ffc_parse_i64(size_t len, const char *input, int base, int64_t *out) {
421421
char *pend = (char*)(input + len);
422-
ffc_int_value value_out;
422+
ffc_int_value value_out = {0};
423423
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_S64, ffc_parse_options_default(), base);
424424
*out = value_out.s64;
425425
return result;
426426
}
427427
ffc_result ffc_parse_u64(size_t len, const char *input, int base, uint64_t *out) {
428428
char *pend = (char*)(input + len);
429-
ffc_int_value value_out;
429+
ffc_int_value value_out = {0};
430430
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_U64, ffc_parse_options_default(), base);
431431
*out = value_out.u64;
432432
return result;
433433
}
434434
ffc_result ffc_parse_i32(size_t len, const char *input, int base, int32_t *out) {
435435
char *pend = (char*)(input + len);
436-
ffc_int_value value_out;
436+
ffc_int_value value_out = {0};
437437
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_S32, ffc_parse_options_default(), base);
438438
*out = value_out.s32;
439439
return result;
440440
}
441441
ffc_result ffc_parse_u32(size_t len, const char *input, int base, uint32_t *out) {
442442
char *pend = (char*)(input + len);
443-
ffc_int_value value_out;
443+
ffc_int_value value_out = {0};
444444
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_U32, ffc_parse_options_default(), base);
445445
*out = value_out.u32;
446446
return result;
@@ -485,4 +485,3 @@ ffc_result ffc_parse_u32(size_t len, const char *input, int base, uint32_t *out)
485485
#endif
486486

487487
#endif /* FFC_H */
488-

test_src/test.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ bool float_eq(float exp, float act) {
115115
};
116116
}
117117

118-
void assert_double(size_t len, char *input, double exp, double act) {
118+
void assert_double(size_t len, const char *input, double exp, double act) {
119119
if (!double_eq(exp, act)) {
120120
printf("\n\ninput: %.*s\n", (int)len, input);
121121
printf("\texp: %f\n\tact: %f\n\n", exp, act);
@@ -124,7 +124,7 @@ void assert_double(size_t len, char *input, double exp, double act) {
124124
}
125125
}
126126

127-
void assert_float(size_t len, char *input, float exp, float act) {
127+
void assert_float(size_t len, const char *input, float exp, float act) {
128128
if (!float_eq(exp, act)) {
129129
printf("\n\ninput: %.*s\n", (int)len, input);
130130
printf("\texp: %f\n\tact: %f\n\n", exp, act);
@@ -133,10 +133,10 @@ void assert_float(size_t len, char *input, float exp, float act) {
133133
}
134134
}
135135

136-
void verify_ext(size_t len, char input[len], ffc_value exp_value, ffc_value_kind vk, ffc_outcome exp_outcome, ffc_parse_options options) {
136+
void verify_ext(size_t len, const char *input, ffc_value exp_value, ffc_value_kind vk, ffc_outcome exp_outcome, ffc_parse_options options) {
137137
ffc_value value;
138138

139-
ffc_result result = ffc_from_chars(input, &input[len], options, &value, vk);
139+
ffc_result result = ffc_from_chars((char*)input, (char*)&input[len], options, &value, vk);
140140

141141
if (exp_outcome != result.outcome) {
142142
printf("\n\ninput: %.*s\n", (int)len, input);
@@ -158,19 +158,19 @@ void verify_ext(size_t len, char input[len], ffc_value exp_value, ffc_value_kind
158158
}
159159
}
160160

161-
void verify_double_ext(size_t len, char input[len], double exp_value, ffc_outcome exp_outcome, ffc_parse_options options) {
161+
void verify_double_ext(size_t len, const char *input, double exp_value, ffc_outcome exp_outcome, ffc_parse_options options) {
162162
ffc_value expected;
163163
expected.d = exp_value;
164164
verify_ext(len, input, expected, FFC_VALUE_KIND_DOUBLE, exp_outcome, options);
165165
}
166166

167-
void verify_float_ext(size_t len, char input[len], float exp_value, ffc_outcome exp_outcome, ffc_parse_options options) {
167+
void verify_float_ext(size_t len, const char *input, float exp_value, ffc_outcome exp_outcome, ffc_parse_options options) {
168168
ffc_value expected;
169169
expected.f = exp_value;
170170
verify_ext(len, input, expected, FFC_VALUE_KIND_FLOAT, exp_outcome, options);
171171
}
172172

173-
void verify_float(char *input, float exp_value) {
173+
void verify_float(const char *input, float exp_value) {
174174
verify_float_ext(strlen(input), input, exp_value, FFC_OUTCOME_OK, ffc_parse_options_default());
175175
}
176176

@@ -634,4 +634,3 @@ int main(void) {
634634

635635
return 0;
636636
}
637-

0 commit comments

Comments
 (0)