Skip to content

Commit d260349

Browse files
Fix *some* C89/C90 issues in the code
This fixes the issues in all the headers (even in the private headers), but not all of them in the .c files. I only fixed those that didn't make the code look much uglier or those for which GCC complains with -std=c90. The compiler only warns about mixed mixed variable declarations and code, unless you use -pedantic-errors. Signed-off-by: Thiago Macieira <[email protected]>
1 parent 9e22610 commit d260349

File tree

8 files changed

+54
-35
lines changed

8 files changed

+54
-35
lines changed

src/cbor.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,11 @@ CBOR_INLINE_API bool cbor_value_is_text_string(const CborValue *value)
409409

410410
CBOR_INLINE_API CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
411411
{
412+
uint64_t v;
412413
assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
413414
if (!cbor_value_is_length_known(value))
414415
return CborErrorUnknownLength;
415-
uint64_t v = _cbor_value_extract_int64_helper(value);
416+
v = _cbor_value_extract_int64_helper(value);
416417
*length = (size_t)v;
417418
if (*length != v)
418419
return CborErrorDataTooLarge;
@@ -462,10 +463,11 @@ CBOR_INLINE_API bool cbor_value_is_map(const CborValue *value)
462463

463464
CBOR_INLINE_API CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
464465
{
466+
uint64_t v;
465467
assert(cbor_value_is_array(value));
466468
if (!cbor_value_is_length_known(value))
467469
return CborErrorUnknownLength;
468-
uint64_t v = _cbor_value_extract_int64_helper(value);
470+
v = _cbor_value_extract_int64_helper(value);
469471
*length = (size_t)v;
470472
if (*length != v)
471473
return CborErrorDataTooLarge;
@@ -474,10 +476,11 @@ CBOR_INLINE_API CborError cbor_value_get_array_length(const CborValue *value, si
474476

475477
CBOR_INLINE_API CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
476478
{
479+
uint64_t v;
477480
assert(cbor_value_is_map(value));
478481
if (!cbor_value_is_length_known(value))
479482
return CborErrorUnknownLength;
480-
uint64_t v = _cbor_value_extract_int64_helper(value);
483+
v = _cbor_value_extract_int64_helper(value);
481484
*length = (size_t)v;
482485
if (*length != v)
483486
return CborErrorDataTooLarge;
@@ -495,9 +498,10 @@ CBOR_INLINE_API bool cbor_value_is_float(const CborValue *value)
495498
{ return value->type == CborFloatType; }
496499
CBOR_INLINE_API CborError cbor_value_get_float(const CborValue *value, float *result)
497500
{
501+
uint32_t data;
498502
assert(cbor_value_is_float(value));
499503
assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
500-
uint32_t data = (uint32_t)_cbor_value_decode_int64_internal(value);
504+
data = (uint32_t)_cbor_value_decode_int64_internal(value);
501505
memcpy(result, &data, sizeof(*result));
502506
return CborNoError;
503507
}
@@ -506,9 +510,10 @@ CBOR_INLINE_API bool cbor_value_is_double(const CborValue *value)
506510
{ return value->type == CborDoubleType; }
507511
CBOR_INLINE_API CborError cbor_value_get_double(const CborValue *value, double *result)
508512
{
513+
uint64_t data;
509514
assert(cbor_value_is_double(value));
510515
assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
511-
uint64_t data = _cbor_value_decode_int64_internal(value);
516+
data = _cbor_value_decode_int64_internal(value);
512517
memcpy(result, &data, sizeof(*result));
513518
return CborNoError;
514519
}

src/cborencoder.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,12 @@ CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value)
386386
*/
387387
CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value)
388388
{
389+
unsigned size;
389390
uint8_t buf[1 + sizeof(uint64_t)];
390391
cbor_assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType);
391392
buf[0] = fpType;
392393

393-
unsigned size = 2U << (fpType - CborHalfFloatType);
394+
size = 2U << (fpType - CborHalfFloatType);
394395
if (size == 8)
395396
put64(buf + 1, *(const uint64_t*)value);
396397
else if (size == 4)

src/cborparser.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ static inline uint64_t get64(const uint8_t *ptr)
165165

166166
CborError CBOR_INTERNAL_API_CC _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len)
167167
{
168+
size_t bytesNeeded;
168169
uint8_t additional_information = **ptr & SmallValueMask;
169170
++*ptr;
170171
if (additional_information < Value8Bit) {
@@ -174,7 +175,7 @@ CborError CBOR_INTERNAL_API_CC _cbor_value_extract_number(const uint8_t **ptr, c
174175
if (unlikely(additional_information > Value64Bit))
175176
return CborErrorIllegalNumber;
176177

177-
size_t bytesNeeded = (size_t)(1 << (additional_information - Value8Bit));
178+
bytesNeeded = (size_t)(1 << (additional_information - Value8Bit));
178179
if (unlikely(bytesNeeded > (size_t)(end - *ptr))) {
179180
return CborErrorUnexpectedEOF;
180181
} else if (bytesNeeded == 1) {
@@ -479,6 +480,9 @@ CborError cbor_value_advance_fixed(CborValue *it)
479480

480481
static CborError advance_recursive(CborValue *it, int nestingLevel)
481482
{
483+
CborError err;
484+
CborValue recursed;
485+
482486
if (is_fixed_type(it->type))
483487
return advance_internal(it);
484488

@@ -491,8 +495,6 @@ static CborError advance_recursive(CborValue *it, int nestingLevel)
491495
if (nestingLevel == 0)
492496
return CborErrorNestingTooDeep;
493497

494-
CborError err;
495-
CborValue recursed;
496498
err = cbor_value_enter_container(it, &recursed);
497499
if (err)
498500
return err;
@@ -815,8 +817,9 @@ CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
815817
*/
816818
CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
817819
{
820+
uint64_t v;
818821
cbor_assert(cbor_value_is_integer(value));
819-
uint64_t v = _cbor_value_extract_int64_helper(value);
822+
v = _cbor_value_extract_int64_helper(value);
820823

821824
/* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
822825
* "[if] the new type is signed and the value cannot be represented in it; either the
@@ -854,8 +857,9 @@ CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
854857
*/
855858
CborError cbor_value_get_int_checked(const CborValue *value, int *result)
856859
{
860+
uint64_t v;
857861
cbor_assert(cbor_value_is_integer(value));
858-
uint64_t v = _cbor_value_extract_int64_helper(value);
862+
v = _cbor_value_extract_int64_helper(value);
859863

860864
/* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
861865
* "[if] the new type is signed and the value cannot be represented in it; either the
@@ -1076,13 +1080,12 @@ static uintptr_t iterate_memcpy(char *dest, const uint8_t *src, size_t len)
10761080
static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
10771081
bool *result, CborValue *next, IterateFunction func)
10781082
{
1079-
cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
1080-
10811083
CborError err;
10821084
CborValue tmp;
10831085
size_t total = 0;
10841086
const void *ptr;
10851087

1088+
cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
10861089
if (!next)
10871090
next = &tmp;
10881091
*next = *value;
@@ -1215,6 +1218,7 @@ CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
12151218
*/
12161219
CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
12171220
{
1221+
size_t len;
12181222
CborValue copy = *value;
12191223
CborError err = cbor_value_skip_tag(&copy);
12201224
if (err)
@@ -1224,7 +1228,7 @@ CborError cbor_value_text_string_equals(const CborValue *value, const char *stri
12241228
return CborNoError;
12251229
}
12261230

1227-
size_t len = strlen(string);
1231+
len = strlen(string);
12281232
return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
12291233
}
12301234

@@ -1302,9 +1306,10 @@ CborError cbor_value_text_string_equals(const CborValue *value, const char *stri
13021306
*/
13031307
CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
13041308
{
1305-
cbor_assert(cbor_value_is_map(map));
1309+
CborError err;
13061310
size_t len = strlen(string);
1307-
CborError err = cbor_value_enter_container(map, element);
1311+
cbor_assert(cbor_value_is_map(map));
1312+
err = cbor_value_enter_container(map, element);
13081313
if (err)
13091314
goto error;
13101315

@@ -1413,10 +1418,11 @@ CborError cbor_value_map_find_value(const CborValue *map, const char *string, Cb
14131418
*/
14141419
CborError cbor_value_get_half_float(const CborValue *value, void *result)
14151420
{
1421+
uint16_t v;
14161422
cbor_assert(cbor_value_is_half_float(value));
14171423

14181424
/* size has been computed already */
1419-
uint16_t v = get16(value->ptr + 1);
1425+
v = get16(value->ptr + 1);
14201426
memcpy(result, &v, sizeof(v));
14211427
return CborNoError;
14221428
}

src/cborparser_dup_string.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@
9696
*/
9797
CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
9898
{
99+
CborError err;
99100
cbor_assert(buffer);
100101
cbor_assert(buflen);
101102
*buflen = SIZE_MAX;
102-
CborError err = _cbor_value_copy_string(value, NULL, buflen, NULL);
103+
err = _cbor_value_copy_string(value, NULL, buflen, NULL);
103104
if (err)
104105
return err;
105106

src/cborpretty.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151

152152
static inline bool convertToUint64(double v, uint64_t *absolute)
153153
{
154+
double supremum;
154155
v = fabs(v);
155156

156157
/* C11 standard section 6.3.1.4 "Real floating and integer" says:
@@ -170,7 +171,7 @@ static inline bool convertToUint64(double v, uint64_t *absolute)
170171
* result is either the nearest higher or nearest lower representable
171172
* value, chosen in an implementation-defined manner.
172173
*/
173-
double supremum = -2.0 * INT64_MIN; /* -2 * (- 2^63) == 2^64 */
174+
supremum = -2.0 * INT64_MIN; /* -2 * (- 2^63) == 2^64 */
174175
if (v >= supremum)
175176
return false;
176177

@@ -209,13 +210,13 @@ static CborError utf8EscapedDump(CborStreamFunction stream, void *out, const voi
209210

210211
if (uc < 0x80) {
211212
/* single-byte UTF-8 */
213+
unsigned char escaped = (unsigned char)uc;
212214
if (uc < 0x7f && uc >= 0x20 && uc != '\\' && uc != '"') {
213215
err = stream(out, "%c", (char)uc);
214216
continue;
215217
}
216218

217219
/* print as an escape sequence */
218-
char escaped = (char)uc;
219220
switch (uc) {
220221
case '"':
221222
case '\\':
@@ -311,13 +312,14 @@ static CborError value_to_pretty(CborStreamFunction stream, void *out, CborValue
311312
static CborError container_to_pretty(CborStreamFunction stream, void *out, CborValue *it, CborType containerType,
312313
int flags, int recursionsLeft)
313314
{
315+
const char *comma = "";
316+
CborError err = CborNoError;
317+
314318
if (!recursionsLeft) {
315319
printRecursionLimit(stream, out);
316-
return CborNoError; /* do allow the dumping to continue */
320+
return err; /* do allow the dumping to continue */
317321
}
318322

319-
const char *comma = "";
320-
CborError err = CborNoError;
321323
while (!cbor_value_at_end(it) && !err) {
322324
err = stream(out, "%s", comma);
323325
comma = ", ";
@@ -490,6 +492,8 @@ static CborError value_to_pretty(CborStreamFunction stream, void *out, CborValue
490492
const char *suffix;
491493
double val;
492494
int r;
495+
uint64_t ival;
496+
493497
if (false) {
494498
float f;
495499
case CborFloatType:
@@ -519,7 +523,6 @@ static CborError value_to_pretty(CborStreamFunction stream, void *out, CborValue
519523
suffix = "";
520524
}
521525

522-
uint64_t ival;
523526
if (convertToUint64(val, &ival)) {
524527
/* this double value fits in a 64-bit integer, so show it as such
525528
* (followed by a floating point suffix, to disambiguate) */

src/cborvalidation.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ static inline CborError validate_number(const CborValue *it, CborType type, uint
294294
{
295295
CborError err = CborNoError;
296296
const uint8_t *ptr = it->ptr;
297+
size_t bytesUsed, bytesNeeded;
297298
uint64_t value;
298299

299300
if ((flags & CborValidateShortestIntegrals) == 0)
@@ -305,8 +306,8 @@ static inline CborError validate_number(const CborValue *it, CborType type, uint
305306
if (err)
306307
return err;
307308

308-
size_t bytesUsed = (size_t)(ptr - it->ptr - 1);
309-
size_t bytesNeeded = 0;
309+
bytesUsed = (size_t)(ptr - it->ptr - 1);
310+
bytesNeeded = 0;
310311
if (value >= Value8Bit)
311312
++bytesNeeded;
312313
if (value > 0xffU)
@@ -376,6 +377,7 @@ static inline CborError validate_tag(CborValue *it, CborTag tag, uint32_t flags,
376377
static inline CborError validate_floating_point(CborValue *it, CborType type, uint32_t flags)
377378
{
378379
CborError err;
380+
int r;
379381
double val;
380382
float valf;
381383
uint16_t valf16;
@@ -398,7 +400,7 @@ static inline CborError validate_floating_point(CborValue *it, CborType type, ui
398400
}
399401
cbor_assert(err == CborNoError); /* can't fail */
400402

401-
int r = fpclassify(val);
403+
r = fpclassify(val);
402404
if (r == FP_NAN || r == FP_INFINITE) {
403405
if (flags & CborValidateFiniteFloatingPoint)
404406
return CborErrorExcludedValue;

src/compilersupport_p.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ static inline unsigned short encode_half(double val)
213213
return _cvtss_sh((float)val, 3);
214214
#else
215215
uint64_t v;
216+
int sign, exp, mant;
216217
memcpy(&v, &val, sizeof(v));
217-
int sign = v >> 63 << 15;
218-
int exp = (v >> 52) & 0x7ff;
219-
int mant = v << 12 >> 12 >> (53-11); /* keep only the 11 most significant bits of the mantissa */
218+
sign = v >> 63 << 15;
219+
exp = (v >> 52) & 0x7ff;
220+
mant = v << 12 >> 12 >> (53-11); /* keep only the 11 most significant bits of the mantissa */
220221
exp -= 1023;
221222
if (exp == 1024) {
222223
/* infinity or NaN */

src/utf8_p.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131

3232
static inline uint32_t get_utf8(const uint8_t **buffer, const uint8_t *end)
3333
{
34-
uint32_t uc;
34+
int charsNeeded;
35+
uint32_t uc, min_uc;
36+
uint8_t b;
3537
ptrdiff_t n = end - *buffer;
3638
if (n == 0)
3739
return ~0U;
@@ -43,8 +45,6 @@ static inline uint32_t get_utf8(const uint8_t **buffer, const uint8_t *end)
4345
}
4446

4547
/* multi-byte UTF-8, decode it */
46-
int charsNeeded;
47-
uint32_t min_uc;
4848
if (unlikely(uc <= 0xC1))
4949
return ~0U;
5050
if (uc < 0xE0) {
@@ -70,7 +70,7 @@ static inline uint32_t get_utf8(const uint8_t **buffer, const uint8_t *end)
7070
return ~0U;
7171

7272
/* first continuation character */
73-
uint8_t b = *(*buffer)++;
73+
b = *(*buffer)++;
7474
if ((b & 0xc0) != 0x80)
7575
return ~0U;
7676
uc <<= 6;
@@ -101,4 +101,4 @@ static inline uint32_t get_utf8(const uint8_t **buffer, const uint8_t *end)
101101
return uc;
102102
}
103103

104-
#endif // CBOR_UTF8_H
104+
#endif /* CBOR_UTF8_H */

0 commit comments

Comments
 (0)