Skip to content

Commit

Permalink
Fixed some strict compiler warnings
Browse files Browse the repository at this point in the history
Ticket: None
Changelog: None
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed May 7, 2024
1 parent 38dbb44 commit a2314fc
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AM_CFLAGS = -Wall -Wextra -Werror
AM_CFLAGS = -Wall -Wextra -Werror -Wconversion -Wformat -Wc++-compat
AM_CPPFLAGS = -include config.h

include_HEADERS = $(top_builddir)/lib/leech.h
Expand Down
2 changes: 1 addition & 1 deletion lib/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bool LCH_BlockStore(const LCH_Instance *const instance,
}

const size_t length = LCH_BufferLength(json);
const unsigned char *const data = (unsigned char *)LCH_BufferData(json);
const unsigned char *const data = (const unsigned char *)LCH_BufferData(json);
if (!LCH_MessageDigest(data, length, digest)) {
LCH_BufferDestroy(digest);
LCH_BufferDestroy(json);
Expand Down
2 changes: 1 addition & 1 deletion lib/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ bool LCH_BufferWriteFile(const LCH_Buffer *buffer, const char *filename) {
return false;
}

tot_written += n_written;
tot_written += (size_t)n_written;
}

close(fd);
Expand Down
8 changes: 4 additions & 4 deletions lib/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static size_t HashKey(const LCH_Buffer *const key) {

size_t hash = 5381;
for (size_t i = 0; i < length; i++) {
hash = ((hash << 5) + hash) + buffer[i];
hash = ((hash << 5) + hash) + (unsigned char)buffer[i];
}
return hash;
}
Expand All @@ -78,15 +78,15 @@ static size_t ComputeIndex(const LCH_Dict *const dict,
}

static bool EnsureCapacity(LCH_Dict *const dict) {
if (dict->in_use < (dict->capacity * LCH_DICT_LOAD_FACTOR)) {
if (dict->in_use < ((float)dict->capacity * LCH_DICT_LOAD_FACTOR)) {
return true;
}

/* If we can free half of the capacity by removing invalidated items, there is
* no need to expand the buffer. */
assert(dict->in_use >= dict->length);
const bool expand =
((dict->capacity / 100.f) * (dict->in_use - dict->length)) < 0.5f;
const bool expand = (((float)dict->capacity / 100.f) *
(float)(dict->in_use - dict->length)) < 0.5f;

const size_t new_capacity = (expand) ? dict->capacity * 2 : dict->capacity;
DictElement **const new_buffer =
Expand Down
25 changes: 15 additions & 10 deletions lib/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1256,8 +1256,9 @@ static bool ParseToken(LCH_JsonParser *const parser, const char *const token) {

for (size_t i = 0; i < length; i++) {
if (parser->cursor[i] != token[i]) {
char *const truncated =
LCH_StringTruncate(parser->cursor, parser->end - parser->cursor, 64);
assert(parser->end >= parser->cursor);
char *const truncated = LCH_StringTruncate(
parser->cursor, (size_t)(parser->end - parser->cursor), 64);
LCH_LOG_ERROR("Failed to parse JSON: Expected '%s', but found '%s'",
token, truncated);
free(truncated);
Expand Down Expand Up @@ -1576,7 +1577,8 @@ static LCH_Json *ParseNumber(LCH_JsonParser *const parser) {

/* We make a null-byte terminated copy in order to make sure we don't scan
* beyond the buffer. */
const size_t max = parser->end - parser->cursor;
assert(parser->end >= parser->cursor);
const size_t max = (size_t)(parser->end - parser->cursor);
char nt_copy[max + 1];
strncpy(nt_copy, parser->cursor, max);
nt_copy[max] = '\0';
Expand All @@ -1585,8 +1587,9 @@ static LCH_Json *ParseNumber(LCH_JsonParser *const parser) {
double number;
int ret = sscanf(nt_copy, "%le%n", &number, &n_chars);
if (ret != 1) {
char *const truncated =
LCH_StringTruncate(parser->cursor, parser->end - parser->cursor, 64);
assert(parser->end >= parser->cursor);
char *const truncated = LCH_StringTruncate(
parser->cursor, (size_t)(parser->end - parser->cursor), 64);
LCH_LOG_ERROR("Failed to parse JSON string: Expected NUMBER, found %s",
truncated);
return NULL;
Expand Down Expand Up @@ -1638,12 +1641,13 @@ static LCH_Json *Parse(LCH_JsonParser *const parser) {
return ParseArray(parser);
}

if ((isdigit(parser->cursor[0]) != 0) || (parser->cursor[0] == '-')) {
if ((isdigit((int)parser->cursor[0]) != 0) || (parser->cursor[0] == '-')) {
return ParseNumber(parser);
}

char *const truncated =
LCH_StringTruncate(parser->cursor, parser->end - parser->cursor, 64);
assert(parser->end >= parser->cursor);
char *const truncated = LCH_StringTruncate(
parser->cursor, (size_t)(parser->end - parser->cursor), 64);
LCH_LOG_ERROR(
"Failed to parse JSON: Expected 'null', 'true', 'false', NUMBER, STRING,"
"OBJECT, ARRAY; but found '%s'",
Expand All @@ -1668,8 +1672,9 @@ LCH_Json *LCH_JsonParse(const char *const str, const size_t len) {
TrimLeadingWhitespace(&parser);

if (parser.cursor < parser.end) {
char *const truncated =
LCH_StringTruncate(parser.cursor, parser.end - parser.cursor, 64);
assert(parser.end >= parser.cursor);
char *const truncated = LCH_StringTruncate(
parser.cursor, (size_t)(parser.end - parser.cursor), 64);
LCH_LOG_ERROR("Failed to parse JSON: Expected End-of-File; but found '%s'",
truncated);
free(truncated);
Expand Down
20 changes: 13 additions & 7 deletions lib/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <sys/types.h>

Expand Down Expand Up @@ -148,29 +149,34 @@ static void Swap(LCH_List *const list, const ssize_t a, const ssize_t b) {

static size_t Partition(LCH_List *const list, const ssize_t low,
const ssize_t high, LCH_CompareFn compare) {
void *pivot = LCH_ListGet(list, high);
assert(high >= 0);
void *pivot = LCH_ListGet(list, (size_t)high);
ssize_t i = low;
for (ssize_t j = low; j < high; j++) {
if (compare(LCH_ListGet(list, j), pivot) <= 0) {
assert(j >= 0);
if (compare(LCH_ListGet(list, (size_t)j), pivot) <= 0) {
Swap(list, i++, j);
}
}
Swap(list, i, high);
return i;
assert(i >= 0);
return (size_t)i;
}

static void QuickSort(LCH_List *const list, const ssize_t low,
const ssize_t high, LCH_CompareFn compare) {
if (low < high) {
const ssize_t pivot = Partition(list, low, high, compare);
QuickSort(list, low, pivot - 1, compare);
QuickSort(list, pivot + 1, high, compare);
const size_t pivot = Partition(list, low, high, compare);
assert(pivot + 1 <= SSIZE_MAX);
QuickSort(list, low, (ssize_t)pivot - 1, compare);
QuickSort(list, (ssize_t)pivot + 1, high, compare);
}
}

void LCH_ListSort(LCH_List *const self, LCH_CompareFn compare) {
assert(self != NULL);
QuickSort(self, 0, self->length - 1, compare);
assert(self->length <= SSIZE_MAX);
QuickSort(self, 0, (ssize_t)self->length - 1, compare);
}

void LCH_ListDestroy(void *const self) {
Expand Down
18 changes: 9 additions & 9 deletions lib/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ int SHA1Result(SHA1Context *context, uint8_t Message_Digest[SHA1HashSize]) {

for (i = 0; i < SHA1HashSize; ++i) {
Message_Digest[i] =
context->Intermediate_Hash[i >> 2] >> 8 * (3 - (i & 0x03));
(uint8_t)(context->Intermediate_Hash[i >> 2] >> 8 * (3 - (i & 0x03)));
}

return shaSuccess;
Expand Down Expand Up @@ -358,14 +358,14 @@ void SHA1PadMessage(SHA1Context *context) {
/*
* Store the message length as the last 8 octets
*/
context->Message_Block[56] = context->Length_High >> 24;
context->Message_Block[57] = context->Length_High >> 16;
context->Message_Block[58] = context->Length_High >> 8;
context->Message_Block[59] = context->Length_High;
context->Message_Block[60] = context->Length_Low >> 24;
context->Message_Block[61] = context->Length_Low >> 16;
context->Message_Block[62] = context->Length_Low >> 8;
context->Message_Block[63] = context->Length_Low;
context->Message_Block[56] = (uint8_t)(context->Length_High >> 24);
context->Message_Block[57] = (uint8_t)(context->Length_High >> 16);
context->Message_Block[58] = (uint8_t)(context->Length_High >> 8);
context->Message_Block[59] = (uint8_t)(context->Length_High);
context->Message_Block[60] = (uint8_t)(context->Length_Low >> 24);
context->Message_Block[61] = (uint8_t)(context->Length_Low >> 16);
context->Message_Block[62] = (uint8_t)(context->Length_Low >> 8);
context->Message_Block[63] = (uint8_t)(context->Length_Low);

SHA1ProcessMessageBlock(context);
}
3 changes: 2 additions & 1 deletion lib/string_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ LCH_List *LCH_StringSplit(const char *str, const char *del) {
const char *end = strpbrk(str, del);

while (end != NULL) {
char *tmp = LCH_StringNDuplicate(start, end - start);
assert(end >= start);
char *tmp = LCH_StringNDuplicate(start, (size_t)(end - start));
if (tmp == NULL) {
return NULL;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <math.h>

#include "buffer.h"
Expand Down Expand Up @@ -160,7 +161,8 @@ bool LCH_MessageDigest(const unsigned char *const message, const size_t length,
return false;
}

ret = SHA1Input(&ctx, message, length);
assert(length < UINT_MAX);
ret = SHA1Input(&ctx, message, (unsigned int)length);
if (ret != shaSuccess) {
return false;
}
Expand Down Expand Up @@ -239,6 +241,6 @@ bool LCH_DoubleToSize(const double number, size_t *const size) {
LCH_LOG_ERROR("%s: Out of bound for size_t (%g < 0)", msg, number);
return false;
}
*size = number;
*size = (size_t)number;
return true;
}

0 comments on commit a2314fc

Please sign in to comment.