Skip to content

Commit f6e841c

Browse files
authored
Merge pull request #4448 from larsewi/CFE-3415
CFE-3415: Fixed some sign-compare warnings
2 parents fca303e + d926a2c commit f6e841c

File tree

22 files changed

+102
-39
lines changed

22 files changed

+102
-39
lines changed

cf-agent/verify_files.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ static bool SaveBufferCallback(const char *dest_filename, void *param, NewLineMo
737737
if (bytes_written != BufferSize(output_buffer))
738738
{
739739
Log(LOG_LEVEL_ERR,
740-
"Error writing to output file '%s' when writing. %zu bytes written but expected %u. (fclose: %s)",
740+
"Error writing to output file '%s' when writing. %zu bytes written but expected %zu. (fclose: %s)",
741741
dest_filename, bytes_written, BufferSize(output_buffer), GetErrorStr());
742742
fclose(fp);
743743
return false;

cf-check/backup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int backup_files_copy(Seq *filenames)
106106
Log(LOG_LEVEL_INFO, "Backing up to '%s'", backup_dir);
107107

108108
int ret = 0;
109-
for (int i = 0; i < length; ++i)
109+
for (size_t i = 0; i < length; ++i)
110110
{
111111
const char *file = SeqAt(filenames, i);
112112
if (!File_CopyToDir(file, backup_dir))
@@ -137,7 +137,7 @@ static int backup_files_replicate(const Seq *files)
137137
Log(LOG_LEVEL_INFO, "Backing up to '%s' using data replication", backup_dir);
138138

139139
size_t corrupted = 0;
140-
for (int i = 0; i < length; ++i)
140+
for (size_t i = 0; i < length; ++i)
141141
{
142142
const char *file = SeqAt(files, i);
143143
assert(StringEndsWith(backup_dir, "/"));

cf-check/diagnose.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ static char *follow_symlink(const char *path)
515515
{
516516
return NULL;
517517
}
518-
if (r >= sizeof(target_buf))
518+
if ((size_t) r >= sizeof(target_buf))
519519
{
520520
Log(LOG_LEVEL_ERR, "Symlink target path too long: %s", path);
521521
return NULL;
@@ -548,7 +548,7 @@ size_t diagnose_files(
548548
*corrupt = SeqNew(length, free);
549549
}
550550

551-
for (int i = 0; i < length; ++i)
551+
for (size_t i = 0; i < length; ++i)
552552
{
553553
const char *filename = SeqAt(filenames, i);
554554
const char *symlink = NULL; // Only initialized because of gcc warning

cf-check/dump.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ int dump_main(int argc, const char *const *const argv)
458458
dump_mode mode = DUMP_NICE;
459459
size_t offset = 1;
460460

461-
if (argc > offset && argv[offset] != NULL && argv[offset][0] == '-')
461+
if ((size_t) argc > offset && argv[offset] != NULL && argv[offset][0] == '-')
462462
{
463463
const char *const option = argv[offset];
464464
offset += 1;
@@ -491,7 +491,7 @@ int dump_main(int argc, const char *const *const argv)
491491
}
492492
}
493493

494-
if (argc > offset && argv[offset] != NULL && argv[offset][0] == '-')
494+
if ((size_t) argc > offset && argv[offset] != NULL && argv[offset][0] == '-')
495495
{
496496
print_usage();
497497
printf("Only one option supported!\n");

cf-check/lmdump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
static void lmdump_print_hex(const char *s, size_t len)
1212
{
13-
for (int i = 0; i < len; i++)
13+
for (size_t i = 0; i < len; i++)
1414
{
1515
printf("%02x", s[i]);
1616
}

cf-check/repair.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ int repair_lmdb_files(Seq *files, bool force)
249249
const size_t length = SeqLength(corrupt);
250250
assert(length > 0);
251251
backup_files_copy(corrupt);
252-
for (int i = 0; i < length; ++i)
252+
for (size_t i = 0; i < length; ++i)
253253
{
254254
const char *file = SeqAt(corrupt, i);
255255
if (repair_lmdb_file(file, -1) == -1)

cf-check/utilities.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Seq *default_lmdb_files()
2222
Seq *argv_to_lmdb_files(
2323
const int argc, const char *const *const argv, const size_t offset)
2424
{
25-
if (offset >= argc)
25+
assert(argc >= 0);
26+
if (offset >= (size_t) argc)
2627
{
2728
Log(LOG_LEVEL_INFO,
2829
"No filenames specified, defaulting to .lmdb files in %s",

libcfnet/client_code.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,11 @@ Item *RemoteDirList(const char *dirname, bool encrypt, AgentConnection *conn)
359359

360360
tosend = cipherlen + CF_PROTO_OFFSET;
361361

362-
if(tosend > sizeof(sendbuffer))
362+
if (tosend < 0)
363+
{
364+
ProgrammingError("RemoteDirList: tosend (%d) < 0", tosend);
365+
}
366+
else if ((unsigned long) tosend > sizeof(sendbuffer))
363367
{
364368
ProgrammingError("RemoteDirList: tosend (%d) > sendbuffer (%zd)",
365369
tosend, sizeof(sendbuffer));
@@ -495,7 +499,11 @@ bool CompareHashNet(const char *file1, const char *file2, bool encrypt, AgentCon
495499

496500
tosend = cipherlen + CF_PROTO_OFFSET;
497501

498-
if(tosend > sizeof(sendbuffer))
502+
if (tosend < 0)
503+
{
504+
ProgrammingError("CompareHashNet: tosend (%d) < 0", tosend);
505+
}
506+
else if ((unsigned long) tosend > sizeof(sendbuffer))
499507
{
500508
ProgrammingError("CompareHashNet: tosend (%d) > sendbuffer (%zd)",
501509
tosend, sizeof(sendbuffer));
@@ -589,7 +597,11 @@ static bool EncryptCopyRegularFileNet(const char *source, const char *dest, off_
589597

590598
tosend = cipherlen + CF_PROTO_OFFSET;
591599

592-
if(tosend > sizeof(workbuf))
600+
if (tosend < 0)
601+
{
602+
ProgrammingError("EncryptCopyRegularFileNet: tosend (%d) < 0", tosend);
603+
}
604+
else if ((unsigned long) tosend > sizeof(workbuf))
593605
{
594606
ProgrammingError("EncryptCopyRegularFileNet: tosend (%d) > workbuf (%zd)",
595607
tosend, sizeof(workbuf));

libcfnet/stat_cache.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ int cf_remote_stat(AgentConnection *conn, bool encrypt, const char *file,
165165

166166
tosend = cipherlen + CF_PROTO_OFFSET;
167167

168-
if(tosend > sizeof(sendbuffer))
168+
if (tosend < 0)
169+
{
170+
ProgrammingError("cf_remote_stat: tosend (%d) < 0", tosend);
171+
}
172+
else if((unsigned int) tosend > sizeof(sendbuffer))
169173
{
170174
ProgrammingError("cf_remote_stat: tosend (%d) > sendbuffer (%zd)",
171175
tosend, sizeof(sendbuffer));

libcfnet/tls_client.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,12 @@ int TLSClientIdentificationDialog(ConnectionInfo *conn_info,
250250
{
251251
ret = snprintf(&line[line_len], sizeof(line) - line_len,
252252
" USERNAME=%s", username);
253-
if (ret >= sizeof(line) - line_len)
253+
if (ret < 0)
254+
{
255+
Log(LOG_LEVEL_ERR, "snprintf failed: %s", GetErrorStr());
256+
return -1;
257+
}
258+
else if ((unsigned int) ret >= sizeof(line) - line_len)
254259
{
255260
Log(LOG_LEVEL_ERR, "Sending IDENTITY truncated: %s", line);
256261
return -1;
@@ -275,14 +280,13 @@ int TLSClientIdentificationDialog(ConnectionInfo *conn_info,
275280
static const char OK[] = "OK WELCOME";
276281
size_t OK_len = sizeof(OK) - 1;
277282
ret = TLSRecvLines(conn_info->ssl, line, sizeof(line));
278-
if (ret == -1)
283+
if (ret < 0)
279284
{
280285
Log(LOG_LEVEL_ERR,
281286
"Connection was hung up during identification! (3)");
282287
return -1;
283288
}
284-
285-
if (ret < OK_len || strncmp(line, OK, OK_len) != 0)
289+
else if ((size_t) ret < OK_len || strncmp(line, OK, OK_len) != 0)
286290
{
287291
Log(LOG_LEVEL_ERR,
288292
"Peer did not accept our identity! Responded: %s",

0 commit comments

Comments
 (0)