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

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",

libenv/sysinfo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ static int Linux_Redhat_Version(EvalContext *ctx)
19281928
Scientific Linux don't fall through the cracks.
19291929
*/
19301930

1931-
for (int i = 0; i < strlen(relstring); i++)
1931+
for (size_t i = 0; i < strlen(relstring); i++)
19321932
{
19331933
relstring[i] = tolower(relstring[i]);
19341934
}
@@ -2079,7 +2079,7 @@ static int Linux_Suse_Version(EvalContext *ctx)
20792079
* SUSE with SUSE 10.0.
20802080
*/
20812081

2082-
for (int i = 0; i < strlen(relstring); i++)
2082+
for (size_t i = 0; i < strlen(relstring); i++)
20832083
{
20842084
relstring[i] = tolower(relstring[i]);
20852085
}

libntech

libpromises/crypto.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,12 @@ bool SavePublicKey(const char *user, const char *digest, const RSA *key)
433433
int ret;
434434

435435
ret = snprintf(keyname, sizeof(keyname), "%s-%s", user, digest);
436-
if (ret >= sizeof(keyname))
436+
if (ret < 0)
437+
{
438+
Log(LOG_LEVEL_ERR, "snprintf failed: %s", GetErrorStr());
439+
return false;
440+
}
441+
else if ((unsigned long) ret >= sizeof(keyname))
437442
{
438443
Log(LOG_LEVEL_ERR, "USERNAME-KEY (%s-%s) string too long!",
439444
user, digest);
@@ -442,7 +447,12 @@ bool SavePublicKey(const char *user, const char *digest, const RSA *key)
442447

443448
ret = snprintf(filename, sizeof(filename), "%s/ppkeys/%s.pub",
444449
GetWorkDir(), keyname);
445-
if (ret >= sizeof(filename))
450+
if (ret < 0)
451+
{
452+
Log(LOG_LEVEL_ERR, "snprintf failed: %s", GetErrorStr());
453+
return false;
454+
}
455+
else if ((unsigned long) ret >= sizeof(filename))
446456
{
447457
Log(LOG_LEVEL_ERR, "Filename too long!");
448458
return false;
@@ -625,7 +635,11 @@ int EncryptString(char *out, size_t out_size, const char *in, int plainlen,
625635

626636
cipherlen += tmplen;
627637

628-
if(cipherlen > max_ciphertext_size)
638+
if (cipherlen < 0)
639+
{
640+
ProgrammingError("EncryptString: chipherlen (%d) < 0", cipherlen);
641+
}
642+
else if ((size_t) cipherlen > max_ciphertext_size)
629643
{
630644
ProgrammingError("EncryptString: too large ciphertext written: cipherlen (%d) > max_ciphertext_size (%zd)",
631645
cipherlen, max_ciphertext_size);
@@ -711,7 +725,11 @@ int DecryptString(char *out, size_t out_size, const char *in, int cipherlen,
711725

712726
plainlen += tmplen;
713727

714-
if(plainlen > max_plaintext_size)
728+
if (plainlen < 0)
729+
{
730+
ProgrammingError("DecryptString: plainlen (%d) < 0", plainlen);
731+
}
732+
if ((size_t) plainlen > max_plaintext_size)
715733
{
716734
ProgrammingError("DecryptString: too large plaintext written: plainlen (%d) > max_plaintext_size (%zd)",
717735
plainlen, max_plaintext_size);
@@ -729,7 +747,11 @@ void DebugBinOut(char *buffer, int len, char *comment)
729747
char buf[CF_BUFSIZE];
730748
char hexStr[3]; // one byte as hex
731749

732-
if (len >= (sizeof(buf) / 2)) // hex uses two chars per byte
750+
if (len < 0)
751+
{
752+
Log(LOG_LEVEL_ERR, "Debug binary print negative len param (len = %d)", len);
753+
}
754+
else if ((unsigned long) len >= (sizeof(buf) / 2)) // hex uses two chars per byte
733755
{
734756
Log(LOG_LEVEL_DEBUG, "Debug binary print is too large (len = %d)", len);
735757
return;

libpromises/evalfunction.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8068,7 +8068,7 @@ static char *StripPatterns(char *file_buffer, const char *pattern, const char *f
80688068
return file_buffer;
80698069
}
80708070

8071-
int start, end, count = 0;
8071+
size_t start, end, count = 0;
80728072
const size_t original_length = strlen(file_buffer);
80738073
while (StringMatchWithPrecompiledRegex(rx, file_buffer, &start, &end))
80748074
{

libpromises/ornaments.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@
3939
*/
4040
static bool StringAppendPromise(char *dst, const char *src, size_t n)
4141
{
42-
int i, j;
42+
size_t i, j;
43+
44+
if (n == 0)
45+
{
46+
return false;
47+
}
48+
4349
n--;
4450
for (i = 0; i < n && dst[i]; i++)
4551
{

libpromises/pipes.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,14 @@ int PipeWriteData(const char *base_cmd, const char *args, const char *data)
176176
io.read_fd, io.write_fd, args);
177177

178178
int res = 0;
179-
if (PipeWrite(&io, data) != strlen(data))
179+
int written = PipeWrite(&io, data);
180+
if (written < 0)
181+
{
182+
Log(LOG_LEVEL_ERR, "Failed to write to pipe (fd %d): %s",
183+
io.write_fd, GetErrorStr());
184+
res = -1;
185+
}
186+
else if ((size_t) written != strlen(data))
180187
{
181188
Log(LOG_LEVEL_VERBOSE,
182189
"Was not able to send whole data to application '%s'.",
@@ -217,7 +224,13 @@ int PipeReadWriteData(const char *base_cmd, const char *args, const char *reques
217224
Log(LOG_LEVEL_DEBUG, "Opened fds %d and %d for command '%s'.",
218225
io.read_fd, io.write_fd, command);
219226

220-
if (PipeWrite(&io, request) != strlen(request))
227+
int written = PipeWrite(&io, request);
228+
if (written < 0) {
229+
Log(LOG_LEVEL_ERR, "Failed to write to pipe (fd %d): %s",
230+
io.write_fd, GetErrorStr());
231+
return -1;
232+
}
233+
else if ((size_t) written != strlen(request))
221234
{
222235
Log(LOG_LEVEL_VERBOSE, "Couldn't send whole data to application '%s'.",
223236
base_cmd);

libpromises/processes_select.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ static bool SelectProcRegexMatch(const char *name1, const char *name2,
594594
}
595595
else
596596
{
597-
int s, e;
597+
size_t s, e;
598598
return StringMatch(regex, line[i], &s, &e);
599599
}
600600
}

libpromises/rlist.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,8 @@ Rlist *RlistFromSplitRegex(const char *string, const char *regex, size_t max_ent
11531153

11541154
const char *sp = string;
11551155
size_t entry_count = 0;
1156-
int start = 0;
1157-
int end = 0;
1156+
size_t start = 0;
1157+
size_t end = 0;
11581158
Rlist *result = NULL;
11591159
Buffer *buffer = BufferNewWithCapacity(CF_MAXVARSIZE);
11601160

@@ -1213,7 +1213,7 @@ Rlist *RlistFromRegexSplitNoOverflow(const char *string, const char *regex, int
12131213
{
12141214
Rlist *liststart = NULL;
12151215
char node[CF_MAXVARSIZE];
1216-
int start, end;
1216+
size_t start, end;
12171217
int count = 0;
12181218

12191219
assert(max > 0); // ensured by FnCallStringSplit() before calling us
@@ -1237,7 +1237,7 @@ Rlist *RlistFromRegexSplitNoOverflow(const char *string, const char *regex, int
12371237
{
12381238
len = CF_MAXVARSIZE - 1;
12391239
Log(LOG_LEVEL_WARNING,
1240-
"Segment in string_split() is %d bytes and will be truncated to %zu bytes",
1240+
"Segment in string_split() is %zu bytes and will be truncated to %zu bytes",
12411241
start,
12421242
len);
12431243
}

libpromises/syntax.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ static JsonElement *BundleTypesToJson(void)
10931093
while ((bundle_type = JsonIteratorNextKey(&it)))
10941094
{
10951095
JsonElement *promise_types = JsonObjectGetAsArray(JsonObjectGetAsObject(bundle_types, bundle_type), "promiseTypes");
1096-
for (int i = 0; i < SeqLength(common_promise_types); i++)
1096+
for (size_t i = 0; i < SeqLength(common_promise_types); i++)
10971097
{
10981098
const char *common_promise_type = SeqAt(common_promise_types, i);
10991099
JsonArrayAppendString(promise_types, common_promise_type);

libpromises/var_expressions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void VarRefDestroy(VarRef *ref)
415415
free(ref->lval);
416416
if (ref->num_indices > 0)
417417
{
418-
for (int i = 0; i < ref->num_indices; ++i)
418+
for (size_t i = 0; i < ref->num_indices; ++i)
419419
{
420420
free(ref->indices[i]);
421421
}

libpromises/vars.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ bool ExtractScalarReference(Buffer *out, const char *str, size_t len, bool extra
376376
}
377377

378378
const char *dollar_point = memchr(str, '$', len);
379-
if (!dollar_point || (dollar_point - str) == len)
379+
if (!dollar_point || (size_t) (dollar_point - str) == len)
380380
{
381381
return false;
382382
}

libpromises/verify_classes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ static bool ValidClassName(const char *str)
4848
FreeExpression(res.result);
4949
}
5050

51-
return res.result && res.position == strlen(str);
51+
assert(res.position >= 0);
52+
return res.result && (size_t) res.position == strlen(str);
5253
}
5354

5455
PromiseResult VerifyClassPromise(EvalContext *ctx, const Promise *pp, ARG_UNUSED void *param)

0 commit comments

Comments
 (0)