Skip to content

Commit b689d49

Browse files
Enable sign-compare warnings globally, and fix every occurrence
1 parent 95dc627 commit b689d49

25 files changed

+121
-119
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ if(ENABLE_MAINTAINER_FLAGS)
322322
gnu-like:-Wuninitialized
323323
# Disabled, for now:
324324
gnu-like:-Wno-strict-aliasing
325+
# Sign-comparison-mismatch:
326+
gnu-like:-Wsign-compare
327+
msvc:/we4018
325328
)
326329
endif()
327330

src/common/src/common-b64.c

+31-35
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include <bson/bson.h>
4444
#include <common-b64-private.h>
45+
#include <mlib/loop.h>
4546

4647
#define Assert(Cond) \
4748
if (!(Cond)) \
@@ -118,21 +119,21 @@ mcommon_b64_ntop (uint8_t const *src, size_t srclength, char *target, size_t tar
118119
size_t datalength = 0;
119120
uint8_t input[3];
120121
uint8_t output[4];
121-
size_t i;
122122

123123
if (!target) {
124124
return -1;
125125
}
126126

127-
while (2 < srclength) {
127+
// While we have at least three chars to read:
128+
while (srclength > 2) {
128129
input[0] = *src++;
129130
input[1] = *src++;
130131
input[2] = *src++;
131132
srclength -= 3;
132133

133134
output[0] = input[0] >> 2;
134-
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
135-
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
135+
output[1] = (uint8_t) (((input[0] & 0x03) << 4) + (input[1] >> 4));
136+
output[2] = (uint8_t) (((input[1] & 0x0f) << 2) + (input[2] >> 6));
136137
output[3] = input[2] & 0x3f;
137138
Assert (output[0] < 64);
138139
Assert (output[1] < 64);
@@ -153,12 +154,11 @@ mcommon_b64_ntop (uint8_t const *src, size_t srclength, char *target, size_t tar
153154
/* Get what's left. */
154155
input[0] = input[1] = input[2] = '\0';
155156

156-
for (i = 0; i < srclength; i++) {
157-
input[i] = *src++;
158-
}
157+
memcpy (input, src, srclength);
158+
src += srclength;
159159
output[0] = input[0] >> 2;
160-
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
161-
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
160+
output[1] = (uint8_t) (((input[0] & 0x03) << 4) + (input[1] >> 4));
161+
output[2] = (uint8_t) (((input[1] & 0x0f) << 2) + (input[2] >> 6));
162162
Assert (output[0] < 64);
163163
Assert (output[1] < 64);
164164
Assert (output[2] < 64);
@@ -277,27 +277,24 @@ static const uint8_t mongoc_b64rmap_invalid = 0xff;
277277

278278
static MONGOC_COMMON_ONCE_FUN (bson_b64_initialize_rmap)
279279
{
280-
int i;
281-
unsigned char ch;
282-
283280
/* Null: end of string, stop parsing */
284281
mongoc_b64rmap[0] = mongoc_b64rmap_end;
285282

286-
for (i = 1; i < 256; ++i) {
287-
ch = (unsigned char) i;
283+
mlib_foreach_urange (i, 1, 256) {
284+
const uint8_t ch = (uint8_t) i;
288285
/* Whitespaces */
289286
if (bson_isspace (ch))
290-
mongoc_b64rmap[i] = mongoc_b64rmap_space;
287+
mongoc_b64rmap[ch] = mongoc_b64rmap_space;
291288
/* Padding: stop parsing */
292289
else if (ch == Pad64)
293-
mongoc_b64rmap[i] = mongoc_b64rmap_end;
290+
mongoc_b64rmap[ch] = mongoc_b64rmap_end;
294291
/* Non-base64 char */
295292
else
296-
mongoc_b64rmap[i] = mongoc_b64rmap_invalid;
293+
mongoc_b64rmap[ch] = mongoc_b64rmap_invalid;
297294
}
298295

299296
/* Fill reverse mapping for base64 chars */
300-
for (i = 0; Base64[i] != '\0'; ++i)
297+
for (uint8_t i = 0; Base64[i] != '\0'; ++i)
301298
mongoc_b64rmap[(uint8_t) Base64[i]] = i;
302299

303300
MONGOC_COMMON_ONCE_RETURN;
@@ -313,7 +310,7 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
313310
tarindex = 0;
314311

315312
while (1) {
316-
ch = *src++;
313+
ch = (uint8_t) *src++;
317314
ofs = mongoc_b64rmap[ch];
318315

319316
if (ofs >= mongoc_b64rmap_special) {
@@ -367,22 +364,22 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
367364
* on a byte boundary, and/or with erroneous trailing characters.
368365
*/
369366

370-
if (ch == Pad64) { /* We got a pad char. */
371-
ch = *src++; /* Skip it, get next. */
367+
if (ch == Pad64) { /* We got a pad char. */
368+
ch = (uint8_t) *src++; /* Skip it, get next. */
372369
switch (state) {
373370
case 0: /* Invalid = in first position */
374371
case 1: /* Invalid = in second position */
375372
return (-1);
376373

377374
case 2: /* Valid, means one byte of info */
378375
/* Skip any number of spaces. */
379-
for ((void) NULL; ch != '\0'; ch = *src++)
376+
for ((void) NULL; ch != '\0'; ch = (uint8_t) *src++)
380377
if (mongoc_b64rmap[ch] != mongoc_b64rmap_space)
381378
break;
382379
/* Make sure there is another trailing = sign. */
383380
if (ch != Pad64)
384381
return (-1);
385-
ch = *src++; /* Skip the = */
382+
ch = (uint8_t) *src++; /* Skip the = */
386383
/* Fall through to "single trailing =" case. */
387384
/* FALLTHROUGH */
388385

@@ -391,7 +388,7 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
391388
* We know this char is an =. Is there anything but
392389
* whitespace after it?
393390
*/
394-
for ((void) NULL; ch != '\0'; ch = *src++)
391+
for ((void) NULL; ch != '\0'; ch = (uint8_t) *src++)
395392
if (mongoc_b64rmap[ch] != mongoc_b64rmap_space)
396393
return (-1);
397394

@@ -422,14 +419,13 @@ mongoc_b64_pton_do (char const *src, uint8_t *target, size_t targsize)
422419
static int
423420
mongoc_b64_pton_len (char const *src)
424421
{
425-
int tarindex, state;
426-
uint8_t ch, ofs;
427-
428-
state = 0;
429-
tarindex = 0;
422+
uint8_t ch = 0;
423+
uint8_t ofs = 0;
424+
int state = 0;
425+
int tarindex = 0;
430426

431427
while (1) {
432-
ch = *src++;
428+
ch = (uint8_t) *src++;
433429
ofs = mongoc_b64rmap[ch];
434430

435431
if (ofs >= mongoc_b64rmap_special) {
@@ -469,22 +465,22 @@ mongoc_b64_pton_len (char const *src)
469465
* on a byte boundary, and/or with erroneous trailing characters.
470466
*/
471467

472-
if (ch == Pad64) { /* We got a pad char. */
473-
ch = *src++; /* Skip it, get next. */
468+
if (ch == Pad64) { /* We got a pad char. */
469+
ch = (uint8_t) *src++; /* Skip it, get next. */
474470
switch (state) {
475471
case 0: /* Invalid = in first position */
476472
case 1: /* Invalid = in second position */
477473
return (-1);
478474

479475
case 2: /* Valid, means one byte of info */
480476
/* Skip any number of spaces. */
481-
for ((void) NULL; ch != '\0'; ch = *src++)
477+
for ((void) NULL; ch != '\0'; ch = (uint8_t) *src++)
482478
if (mongoc_b64rmap[ch] != mongoc_b64rmap_space)
483479
break;
484480
/* Make sure there is another trailing = sign. */
485481
if (ch != Pad64)
486482
return (-1);
487-
ch = *src++; /* Skip the = */
483+
ch = (uint8_t) *src++; /* Skip the = */
488484
/* Fall through to "single trailing =" case. */
489485
/* FALLTHROUGH */
490486

@@ -493,7 +489,7 @@ mongoc_b64_pton_len (char const *src)
493489
* We know this char is an =. Is there anything but
494490
* whitespace after it?
495491
*/
496-
for ((void) NULL; ch != '\0'; ch = *src++)
492+
for (; ch != '\0'; ch = (uint8_t) *src++)
497493
if (mongoc_b64rmap[ch] != mongoc_b64rmap_space)
498494
return (-1);
499495

src/common/src/common-md5.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ void
338338
mcommon_md5_append (bson_md5_t *pms, const uint8_t *data, uint32_t nbytes)
339339
{
340340
const uint8_t *p = data;
341-
int left = nbytes;
342-
int offset = (pms->count[0] >> 3) & 63;
341+
uint32_t left = nbytes;
342+
uint8_t offset = (pms->count[0] >> 3) & 63;
343343
uint32_t nbits = (uint32_t) (nbytes << 3);
344344

345345
if (nbytes <= 0)
@@ -353,7 +353,7 @@ mcommon_md5_append (bson_md5_t *pms, const uint8_t *data, uint32_t nbytes)
353353

354354
/* Process an initial partial block. */
355355
if (offset) {
356-
int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
356+
uint32_t copy = (offset + nbytes > 64u ? 64u - offset : nbytes);
357357

358358
memcpy (pms->buf + offset, p, copy);
359359
if (offset + copy < 64)

src/common/src/common-string.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ mcommon_string_append_base64_encode (mcommon_string_append_t *append, const uint
200200
if (encoded_target_len <= (size_t) max_append_len) {
201201
// No truncation needed. Grow the buffer and encode directly.
202202
mcommon_string_grow_to_capacity (string, old_len + encoded_target_len);
203-
BSON_ASSERT (encoded_target_len ==
204-
mcommon_b64_ntop (bytes, (size_t) len, string->str + old_len, encoded_target_len + 1));
203+
const int tgt = mcommon_b64_ntop (bytes, (size_t) len, string->str + old_len, encoded_target_len + 1);
204+
BSON_ASSERT (mlib_cmp (encoded_target_len, ==, tgt));
205205
BSON_ASSERT (mlib_in_range (uint32_t, encoded_target_len));
206206
string->len = old_len + (uint32_t) encoded_target_len;
207207
return true;
@@ -227,18 +227,18 @@ mcommon_string_append_base64_encode (mcommon_string_append_t *append, const uint
227227
uint32_t direct_input_len = mcommon_b64_pton_calculate_target_size ((size_t) direct_encoded_len);
228228
BSON_ASSERT (direct_input_len % 3 == 0);
229229
BSON_ASSERT (direct_input_len < len);
230-
BSON_ASSERT (direct_encoded_len ==
231-
mcommon_b64_ntop (bytes, (size_t) direct_input_len, string->str + old_len, direct_encoded_len + 1));
230+
const int tgt =
231+
mcommon_b64_ntop (bytes, (size_t) direct_input_len, string->str + old_len, direct_encoded_len + 1);
232+
BSON_ASSERT (mlib_cmp (direct_encoded_len, ==, tgt));
232233

233234
char remainder_buffer[5];
234235
uint32_t remainder_input_len = BSON_MIN (3, len - direct_input_len);
235236
BSON_ASSERT (remainder_input_len > 0);
236237
uint32_t remainder_encoded_len = mcommon_b64_ntop_calculate_target_size ((size_t) remainder_input_len) - 1;
237238
BSON_ASSERT (remainder_encoded_len > remainder_truncated_len);
238-
BSON_ASSERT (remainder_encoded_len == mcommon_b64_ntop (bytes + direct_input_len,
239-
(size_t) remainder_input_len,
240-
remainder_buffer,
241-
sizeof remainder_buffer));
239+
const int t2 = mcommon_b64_ntop (
240+
bytes + direct_input_len, (size_t) remainder_input_len, remainder_buffer, sizeof remainder_buffer);
241+
BSON_ASSERT (mlib_cmp (remainder_encoded_len, ==, t2));
242242
memcpy (buffer + old_len + direct_encoded_len, remainder_buffer, remainder_encoded_len);
243243

244244
BSON_ASSERT (old_len + direct_encoded_len + remainder_truncated_len == max_len);

src/libbson/examples/bson-metrics.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ main (int argc, char *argv[])
252252
mark = 0;
253253
while ((b = bson_reader_read (reader, NULL))) {
254254
off_t pos = bson_reader_tell (reader);
255-
state.doc_size_max = BSON_MAX (pos - mark, state.doc_size_max);
255+
state.doc_size_max = BSON_MAX (pos - mark, (off_t) state.doc_size_max);
256256
mark = pos;
257257
bson_metrics (b, NULL, &state);
258258
}

0 commit comments

Comments
 (0)