From 425fa7790016d96e11d31407d697540d6893edf9 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 6 Sep 2024 14:23:27 +0900 Subject: [PATCH] Fixes related to compiler warnings Fix integer mismatch usages/comparisons. Use "size_t" instead of "unsigned int" for data structures. --- src/archiver.c | 104 +++++++++++++++++++--------- src/archiver.h | 6 +- src/data_structures/hash_map.c | 31 ++++----- src/data_structures/hash_map.h | 15 ++-- src/data_structures/linked_list.h | 4 +- src/data_structures/priority_heap.c | 8 +-- src/data_structures/test.c | 4 +- src/helpers.c | 12 ++-- src/helpers.h | 6 +- src/parser.c | 43 ++++++------ src/parser_internal.h | 2 +- 11 files changed, 138 insertions(+), 97 deletions(-) diff --git a/src/archiver.c b/src/archiver.c index c35b676..d87a3d1 100644 --- a/src/archiver.c +++ b/src/archiver.c @@ -39,9 +39,9 @@ #include "helpers.h" -#define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%u.tmp" -#define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%uu of %%%uu.\n" -#define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%uu/%%%uu]\n" +#define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%lu.tmp" +#define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%lulu of %%%lulu.\n" +#define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%lulu/%%%lulu]\n" #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ @@ -158,8 +158,8 @@ int write_files_fn(void *data, void *ud) { SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX // Use temp file to store compressed data. char temp_filename[512]; - unsigned int idx = 0; - unsigned int temp_dir_end = strlen(state->parsed->temp_dir); + size_t idx = 0; + size_t temp_dir_end = strlen(state->parsed->temp_dir); snprintf(temp_filename, 512, TEMP_FILENAME_CMP, state->parsed->temp_dir, state->parsed->temp_dir[temp_dir_end - 1] == '/' ? "" : "/", idx); @@ -341,7 +341,7 @@ int write_files_fn(void *data, void *ud) { if (!read_done) { ret = read(pipe_outof_cmd[0], read_buf, 1024); if (ret > 0) { - read_count = fwrite(read_buf, 1, ret, tmp_fd); + read_count = fwrite(read_buf, 1, (size_t)ret, tmp_fd); if (read_count != (size_t)ret) { // Write to tmp_fd error. fprintf(stderr, @@ -384,7 +384,12 @@ int write_files_fn(void *data, void *ud) { uint16_t u16; uint64_t u64; - u16 = strlen(file_info->filename); + size_t temp_size = strlen(file_info->filename); + if (temp_size > 0xFFFF) { + fprintf(stderr, "ERROR: Filename size is too large to store!\n"); + return 1; + } + u16 = (uint16_t)temp_size; // Write filename length. simple_archiver_helper_16_bit_be(&u16); @@ -406,7 +411,7 @@ int write_files_fn(void *data, void *ud) { temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write->buf = malloc(4); temp_to_write->size = 4; - for (unsigned int idx = 0; idx < temp_to_write->size; ++idx) { + for (size_t idx = 0; idx < temp_to_write->size; ++idx) { ((unsigned char *)temp_to_write->buf)[idx] = 0; } @@ -474,7 +479,7 @@ int write_files_fn(void *data, void *ud) { } // Write file length. - u64 = end; + u64 = (uint64_t)end; simple_archiver_helper_64_bit_be(&u64); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write->buf = malloc(8); @@ -511,7 +516,12 @@ int write_files_fn(void *data, void *ud) { uint16_t u16; uint64_t u64; - u16 = strlen(file_info->filename); + size_t temp_size = strlen(file_info->filename); + if (temp_size > 0xFFFF) { + fprintf(stderr, "ERROR: Filename is too large to store!\n"); + return 1; + } + u16 = (uint16_t)temp_size; // Write filename length. simple_archiver_helper_16_bit_be(&u16); @@ -533,7 +543,7 @@ int write_files_fn(void *data, void *ud) { temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write->buf = malloc(4); temp_to_write->size = 4; - for (unsigned int idx = 0; idx < temp_to_write->size; ++idx) { + for (size_t idx = 0; idx < temp_to_write->size; ++idx) { ((unsigned char *)temp_to_write->buf)[idx] = 0; } @@ -606,7 +616,7 @@ int write_files_fn(void *data, void *ud) { // Error. return 1; } - u64 = end; + u64 = (uint64_t)end; simple_archiver_helper_64_bit_be(&u64); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write->buf = malloc(8); @@ -647,7 +657,12 @@ int write_files_fn(void *data, void *ud) { // A symblic link. uint16_t u16; - u16 = strlen(file_info->filename); + size_t temp_size = strlen(file_info->filename); + if (temp_size > 0xFFFF) { + fprintf(stderr, "ERROR: Filename is too large to store!\n"); + return 1; + } + u16 = (uint16_t)temp_size; // Write filename length. simple_archiver_helper_16_bit_be(&u16); @@ -669,7 +684,7 @@ int write_files_fn(void *data, void *ud) { temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write->buf = malloc(4); temp_to_write->size = 4; - for (unsigned int idx = 0; idx < temp_to_write->size; ++idx) { + for (size_t idx = 0; idx < temp_to_write->size; ++idx) { ((unsigned char *)temp_to_write->buf)[idx] = 0; } @@ -746,8 +761,8 @@ int write_files_fn(void *data, void *ud) { // Compare paths to get relative path. // Get first non-common char. - unsigned int idx; - unsigned int last_slash; + size_t idx; + size_t last_slash; for (idx = 0, last_slash = 0; idx < strlen(abs_path) && idx < strlen(link_abs_path); ++idx) { if (((const char *)abs_path)[idx] != @@ -812,7 +827,12 @@ int write_files_fn(void *data, void *ud) { simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write); } else if ((state->parsed->flags & 0x20) == 0) { // Write absolute path length. - u16 = strlen(abs_path); + size_t temp_size = strlen(abs_path); + if (temp_size > 0xFFFF) { + fprintf(stderr, "ERROR: Absolute path name is too large!\n"); + return 1; + } + u16 = (uint16_t)temp_size; simple_archiver_helper_16_bit_be(&u16); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); @@ -841,7 +861,12 @@ int write_files_fn(void *data, void *ud) { if (rel_path) { // Write relative path length. - u16 = strlen(rel_path); + size_t temp_size = strlen(rel_path); + if (temp_size > 0xFFFF) { + fprintf(stderr, "ERROR: Relative path name is too large!\n"); + return 1; + } + u16 = (uint16_t)temp_size; simple_archiver_helper_16_bit_be(&u16); temp_to_write = malloc(sizeof(SDArchiverInternalToWrite)); temp_to_write->buf = malloc(2); @@ -1043,14 +1068,19 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, return SDAS_FAILED_TO_WRITE; } c = 0; - for (unsigned int i = 0; i < 3; ++i) { + for (size_t i = 0; i < 3; ++i) { if (fwrite(&c, 1, 1, out_f) != 1) { return SDAS_FAILED_TO_WRITE; } } // De/compressor bytes. - u16 = strlen(state->parsed->compressor); + size_t temp_size = strlen(state->parsed->compressor); + if (temp_size > 0xFFFF) { + fprintf(stderr, "ERROR: Compressor cmd string is too large!\n"); + return SDAS_NO_COMPRESSOR; + } + u16 = (uint16_t)temp_size; // To big-endian. simple_archiver_helper_16_bit_be(&u16); // Write the size in big-endian. @@ -1065,7 +1095,12 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, return SDAS_FAILED_TO_WRITE; } - u16 = strlen(state->parsed->decompressor); + temp_size = strlen(state->parsed->decompressor); + if (temp_size > 0xFFFF) { + fprintf(stderr, "ERROR: Decompressor cmd string is too large!\n"); + return SDAS_NO_DECOMPRESSOR; + } + u16 = (uint16_t)temp_size; // To big-endian. simple_archiver_helper_16_bit_be(&u16); // Write the size in big-endian. @@ -1082,7 +1117,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, } else { // Write the four flag bytes with first bit NOT set. unsigned char c = 0; - for (unsigned int i = 0; i < 4; ++i) { + for (size_t i = 0; i < 4; ++i) { if (fwrite(&c, 1, 1, out_f) != 1) { return SDAS_FAILED_TO_WRITE; } @@ -1091,7 +1126,11 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, // Write file count. { - uint32_t u32 = filenames->count; + if (filenames->count > 0xFFFFFFFF) { + fprintf(stderr, "ERROR: Filenames count is too large!\n"); + return SDAS_INTERNAL_ERROR; + } + uint32_t u32 = (uint32_t)filenames->count; simple_archiver_helper_32_bit_be(&u32); if (fwrite(&u32, 1, 4, out_f) != 4) { return SDAS_FAILED_TO_WRITE; @@ -1232,7 +1271,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, fprintf(stderr, "File count is %u\n", u32); const uint32_t size = u32; - const unsigned int digits = simple_archiver_helper_num_digits(size); + const size_t digits = simple_archiver_helper_num_digits(size); char format_str[128]; snprintf(format_str, 128, FILE_COUNTS_OUTPUT_FORMAT_STR_0, digits, digits); int skip = 0; @@ -1242,7 +1281,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, state->parsed->working_files[0] != NULL) { hash_map = simple_archiver_hash_map_init(); for (char **iter = state->parsed->working_files; *iter != NULL; ++iter) { - int len = strlen(*iter) + 1; + size_t len = strlen(*iter) + 1; char *key = malloc(len); memcpy(key, *iter, len); key[len - 1] = 0; @@ -1326,7 +1365,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, return SDAS_INVALID_FILE; } - unsigned int permissions = 0; + mode_t permissions = 0; #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX @@ -1537,7 +1576,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, int write_again = 0; int write_pipe_done = 0; int read_pipe_done = 0; - ssize_t fread_ret; + size_t fread_ret; char recv_buf[1024]; size_t amount_to_read; while (!write_pipe_done || !read_pipe_done) { @@ -1565,7 +1604,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, // Send over pipe to decompressor. if (fread_ret > 0) { ssize_t write_ret = write(pipe_into_cmd[1], buf, fread_ret); - if (write_ret == fread_ret) { + if (write_ret > 0 && (size_t)write_ret == fread_ret) { // Successful write. write_again = 0; if (compressed_file_size == 0) { @@ -1596,7 +1635,8 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, if (!read_pipe_done) { ssize_t read_ret = read(pipe_outof_cmd[0], recv_buf, 1024); if (read_ret > 0) { - size_t fwrite_ret = fwrite(recv_buf, 1, read_ret, out_f); + size_t fwrite_ret = + fwrite(recv_buf, 1, (size_t)read_ret, out_f); if (fwrite_ret == (size_t)read_ret) { // Success. } else if (ferror(out_f)) { @@ -1647,7 +1687,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, waitpid(decompressor_pid, NULL, 0); } else { uint64_t compressed_file_size = u64; - ssize_t fread_ret; + size_t fread_ret; while (compressed_file_size != 0) { if (compressed_file_size > 1024) { fread_ret = fread(buf, 1, 1024, in_f); @@ -1688,14 +1728,14 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract, } else { while (u64 != 0) { if (u64 > 1024) { - ssize_t read_ret = fread(buf, 1, 1024, in_f); + size_t read_ret = fread(buf, 1, 1024, in_f); if (read_ret > 0) { u64 -= read_ret; } else if (ferror(in_f)) { return SDAS_INTERNAL_ERROR; } } else { - ssize_t read_ret = fread(buf, 1, u64, in_f); + size_t read_ret = fread(buf, 1, u64, in_f); if (read_ret > 0) { u64 -= read_ret; } else if (ferror(in_f)) { diff --git a/src/archiver.h b/src/archiver.h index b8b035a..cfc2696 100644 --- a/src/archiver.h +++ b/src/archiver.h @@ -32,9 +32,9 @@ typedef struct SDArchiverState { const SDArchiverParsed *parsed; FILE *out_f; SDArchiverHashMap *map; - unsigned int count; - unsigned int max; - unsigned int digits; + size_t count; + size_t max; + size_t digits; } SDArchiverState; enum SDArchiverStateReturns { diff --git a/src/data_structures/hash_map.c b/src/data_structures/hash_map.c index 6fae866..e4b0a19 100644 --- a/src/data_structures/hash_map.c +++ b/src/data_structures/hash_map.c @@ -26,18 +26,18 @@ typedef struct SDArchiverHashMapData { void *value; void *key; - unsigned int key_size; + size_t key_size; void (*value_cleanup_fn)(void *); void (*key_cleanup_fn)(void *); } SDArchiverHashMapData; typedef struct SDArchiverHashMapKeyData { void *key; - unsigned int key_size; + size_t key_size; } SDArchiverHashMapKeyData; typedef struct SDArchiverInternalIterContext { - int (*iter_check_fn)(const void *, unsigned int, const void *, void *); + int (*iter_check_fn)(const void *, size_t, const void *, void *); int ret; void *user_data; } SDArchiverInternalIterContext; @@ -75,11 +75,11 @@ int simple_archiver_hash_map_internal_pick_in_list(void *data, void *ud) { } unsigned long long simple_archiver_hash_map_internal_key_to_hash( - const void *key, unsigned int key_size) { + const void *key, size_t key_size) { unsigned long long seed = 0; unsigned long long temp = 0; - unsigned int count = 0; - for (unsigned int idx = 0; idx < key_size; ++idx) { + size_t count = 0; + for (size_t idx = 0; idx < key_size; ++idx) { temp |= ((unsigned long long)*((unsigned char *)key + idx)) << (8 * count); ++count; if (count >= 8) { @@ -110,13 +110,13 @@ int simple_archiver_hash_map_internal_rehash(SDArchiverHashMap **hash_map) { // Pointers have the same size (at least on the same machine), so // sizeof(void*) should be ok. new_hash_map->buckets = malloc(sizeof(void *) * new_hash_map->buckets_size); - for (unsigned int idx = 0; idx < new_hash_map->buckets_size; ++idx) { + for (size_t idx = 0; idx < new_hash_map->buckets_size; ++idx) { new_hash_map->buckets[idx] = simple_archiver_list_init(); } new_hash_map->count = 0; // Iterate through the old hash map to populate the new hash map. - for (unsigned int bucket_idx = 0; bucket_idx < (*hash_map)->buckets_size; + for (size_t bucket_idx = 0; bucket_idx < (*hash_map)->buckets_size; ++bucket_idx) { SDArchiverLLNode *node = (*hash_map)->buckets[bucket_idx]->head; while (node) { @@ -145,7 +145,7 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) { // Pointers have the same size (at least on the same machine), so // sizeof(void*) should be ok. hash_map->buckets = malloc(sizeof(void *) * hash_map->buckets_size); - for (unsigned int idx = 0; idx < hash_map->buckets_size; ++idx) { + for (size_t idx = 0; idx < hash_map->buckets_size; ++idx) { hash_map->buckets[idx] = simple_archiver_list_init(); } hash_map->count = 0; @@ -155,7 +155,7 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) { void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) { if (hash_map && *hash_map) { - for (unsigned int idx = 0; idx < (*hash_map)->buckets_size; ++idx) { + for (size_t idx = 0; idx < (*hash_map)->buckets_size; ++idx) { SDArchiverLinkedList **linked_list = (*hash_map)->buckets + idx; simple_archiver_list_free(linked_list); } @@ -168,7 +168,7 @@ void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) { } int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value, - void *key, unsigned int key_size, + void *key, size_t key_size, void (*value_cleanup_fn)(void *), void (*key_cleanup_fn)(void *)) { if ((*hash_map)->buckets_size <= (*hash_map)->count) { @@ -214,7 +214,7 @@ int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value, } void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map, - const void *key, unsigned int key_size) { + const void *key, size_t key_size) { unsigned long long hash = simple_archiver_hash_map_internal_key_to_hash(key, key_size) % hash_map->buckets_size; @@ -234,7 +234,7 @@ void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map, } int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key, - unsigned int key_size) { + size_t key_size) { unsigned long long hash = simple_archiver_hash_map_internal_key_to_hash(key, key_size) % hash_map->buckets_size; @@ -269,15 +269,14 @@ int simple_archiver_internal_hash_map_bucket_iter_fn(void *data, void *ud) { } int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map, - int (*iter_check_fn)(const void *, - unsigned int, + int (*iter_check_fn)(const void *, size_t, const void *, void *), void *user_data) { SDArchiverInternalIterContext ctx; ctx.iter_check_fn = iter_check_fn; ctx.ret = 0; ctx.user_data = user_data; - for (unsigned int idx = 0; idx < hash_map->buckets_size; ++idx) { + for (size_t idx = 0; idx < hash_map->buckets_size; ++idx) { if (simple_archiver_list_get( hash_map->buckets[idx], simple_archiver_internal_hash_map_bucket_iter_fn, &ctx) != 0) { diff --git a/src/data_structures/hash_map.h b/src/data_structures/hash_map.h index ae8b8ee..c9e735f 100644 --- a/src/data_structures/hash_map.h +++ b/src/data_structures/hash_map.h @@ -21,12 +21,14 @@ #define SC_SA_DS_HASH_MAP_START_BUCKET_SIZE 32 +#include + #include "linked_list.h" typedef struct SDArchiverHashMap { SDArchiverLinkedList **buckets; - unsigned int buckets_size; - unsigned int count; + size_t buckets_size; + size_t count; } SDArchiverHashMap; SDArchiverHashMap *simple_archiver_hash_map_init(void); @@ -40,18 +42,18 @@ void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map); /// NOTICE: You must not pass NULL to value, otherwise all "get" checks will /// fail for the inserted key. int simple_archiver_hash_map_insert(SDArchiverHashMap **hash_map, void *value, - void *key, unsigned int key_size, + void *key, size_t key_size, void (*value_cleanup_fn)(void *), void (*key_cleanup_fn)(void *)); /// Returns NULL if not found. void *simple_archiver_hash_map_get(const SDArchiverHashMap *hash_map, - const void *key, unsigned int key_size); + const void *key, size_t key_size); /// Returns zero on success. Returns one if more than one entry was removed. /// Otherwise returns non-zero and non-one value on error. int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key, - unsigned int key_size); + size_t key_size); /// Iterates through the hash map with the "iter_check_fn", which is passed the /// key, key-size, value, and user_data. This function will call "iter_check_fn" @@ -60,8 +62,7 @@ int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key, /// "iter_check_fn" returns zero for every call, then this function will return /// zero after having iterated through every key-value pair. int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map, - int (*iter_check_fn)(const void *, - unsigned int, + int (*iter_check_fn)(const void *, size_t, const void *, void *), void *user_data); diff --git a/src/data_structures/linked_list.h b/src/data_structures/linked_list.h index 7471b18..04c25da 100644 --- a/src/data_structures/linked_list.h +++ b/src/data_structures/linked_list.h @@ -19,6 +19,8 @@ #ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_LINKED_LIST_H_ #define SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_LINKED_LIST_H_ +#include + typedef struct SDArchiverLLNode { struct SDArchiverLLNode *next; struct SDArchiverLLNode *prev; @@ -29,7 +31,7 @@ typedef struct SDArchiverLLNode { typedef struct SDArchiverLinkedList { SDArchiverLLNode *head; SDArchiverLLNode *tail; - unsigned int count; + size_t count; } SDArchiverLinkedList; SDArchiverLinkedList *simple_archiver_list_init(void); diff --git a/src/data_structures/priority_heap.c b/src/data_structures/priority_heap.c index c7b179f..af7e400 100644 --- a/src/data_structures/priority_heap.c +++ b/src/data_structures/priority_heap.c @@ -31,7 +31,7 @@ void simple_archiver_priority_heap_internal_realloc( new_priority_heap->nodes = calloc(new_priority_heap->capacity, sizeof(SDArchiverPHNode)); - for (unsigned int idx = 1; idx < (*priority_heap)->size + 1; ++idx) { + for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) { if ((*priority_heap)->nodes[idx].is_valid != 0) { simple_archiver_priority_heap_insert( &new_priority_heap, (*priority_heap)->nodes[idx].priority, @@ -79,7 +79,7 @@ SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn( void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) { if (priority_heap && *priority_heap) { - for (unsigned int idx = 1; idx < (*priority_heap)->size + 1; ++idx) { + for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) { if ((*priority_heap)->nodes[idx].is_valid != 0) { if ((*priority_heap)->nodes[idx].data_cleanup_fn) { (*priority_heap) @@ -109,7 +109,7 @@ void simple_archiver_priority_heap_insert(SDArchiverPHeap **priority_heap, simple_archiver_priority_heap_internal_realloc(priority_heap); } - unsigned int hole = (*priority_heap)->size + 1; + size_t hole = (*priority_heap)->size + 1; while (hole > 1 && (*priority_heap) @@ -146,7 +146,7 @@ void *simple_archiver_priority_heap_pop(SDArchiverPHeap *priority_heap) { SDArchiverPHNode end = priority_heap->nodes[priority_heap->size]; priority_heap->nodes[priority_heap->size].is_valid = 0; - unsigned int hole = 1; + size_t hole = 1; while (hole * 2 + 1 <= priority_heap->size) { if (priority_heap->nodes[hole * 2].is_valid != 0 && priority_heap->nodes[hole * 2 + 1].is_valid != 0) { diff --git a/src/data_structures/test.c b/src/data_structures/test.c index 5b0cbbf..3303191 100644 --- a/src/data_structures/test.c +++ b/src/data_structures/test.c @@ -66,7 +66,7 @@ int get_three_fn(void *data, __attribute__((unused)) void *ud) { int more_fn(long long a, long long b) { return a > b ? 1 : 0; } int hash_map_iter_check_fn(__attribute__((unused)) const void *key, - __attribute__((unused)) unsigned int key_size, + __attribute__((unused)) size_t key_size, const void *value, void *ud) { char *found_buf = ud; const size_t real_value = (const size_t)value; @@ -79,7 +79,7 @@ int hash_map_iter_check_fn(__attribute__((unused)) const void *key, } int hash_map_iter_check_fn2(__attribute__((unused)) const void *key, - __attribute__((unused)) unsigned int key_size, + __attribute__((unused)) size_t key_size, __attribute__((unused)) const void *value, __attribute__((unused)) void *ud) { return 2; diff --git a/src/helpers.c b/src/helpers.c index 828faf0..7bf83b7 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -59,7 +59,7 @@ void simple_archiver_helper_cleanup_chdir_back(char **original) { #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN - chdir(*original); + __attribute__((unused)) int unused_ret = chdir(*original); #endif free(*original); *original = NULL; @@ -221,9 +221,9 @@ int simple_archiver_helper_make_dirs(const char *file_path) { #endif } -char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx, - unsigned int end_idx) { - unsigned int s_len = strlen(s); +char *simple_archiver_helper_cut_substr(const char *s, size_t start_idx, + size_t end_idx) { + size_t s_len = strlen(s); if (start_idx > end_idx || start_idx >= s_len || end_idx > s_len) { return NULL; } else if (end_idx == s_len) { @@ -246,8 +246,8 @@ char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx, } } -unsigned int simple_archiver_helper_num_digits(unsigned int value) { - unsigned int digits = 0; +size_t simple_archiver_helper_num_digits(size_t value) { + size_t digits = 0; do { ++digits; value /= 10; diff --git a/src/helpers.h b/src/helpers.h index 3181370..ce006dd 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -50,10 +50,10 @@ int simple_archiver_helper_make_dirs(const char *file_path); /// Returns non-NULL on success. /// Must be free'd with "free()" if non-NULL. /// start_idx is inclusive and end_idx is exclusive. -char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx, - unsigned int end_idx); +char *simple_archiver_helper_cut_substr(const char *s, size_t start_idx, + size_t end_idx); -unsigned int simple_archiver_helper_num_digits(unsigned int value); +size_t simple_archiver_helper_num_digits(size_t value); void simple_archiver_helper_cleanup_FILE(FILE **fd); void simple_archiver_helper_cleanup_malloced(void **data); diff --git a/src/parser.c b/src/parser.c index 70ec84b..4807751 100644 --- a/src/parser.c +++ b/src/parser.c @@ -39,15 +39,15 @@ #include "parser_internal.h" /// Gets the first non "./"-like character in the filename. -unsigned int simple_archiver_parser_internal_get_first_non_current_idx( +size_t simple_archiver_parser_internal_get_first_non_current_idx( const char *filename) { - unsigned int idx = 0; - unsigned int known_good_idx = 0; - const unsigned int length = strlen(filename); + size_t idx = 0; + size_t known_good_idx = 0; + const size_t length = strlen(filename); // 0b0001 - checked that idx char is '.' // 0b0010 - checked that idx char is '/' - unsigned int flags = 0; + size_t flags = 0; for (; idx < length; ++idx) { if ((flags & 3) == 0) { @@ -90,8 +90,8 @@ unsigned int simple_archiver_parser_internal_get_first_non_current_idx( } void simple_archiver_parser_internal_remove_end_slash(char *filename) { - int len = strlen(filename); - int idx; + size_t len = strlen(filename); + size_t idx; for (idx = len; idx-- > 0;) { if (filename[idx] != '/') { ++idx; @@ -236,7 +236,7 @@ int simple_archiver_parse_args(int argc, const char **argv, out->filename = NULL; } else { out->flags &= 0xFFFFFFEF; - int size = strlen(argv[1]) + 1; + size_t size = strlen(argv[1]) + 1; out->filename = malloc(size); strncpy(out->filename, argv[1], size); } @@ -257,7 +257,7 @@ int simple_archiver_parse_args(int argc, const char **argv, simple_archiver_print_usage(); return 1; } - int size = strlen(argv[1]) + 1; + size_t size = strlen(argv[1]) + 1; out->compressor = malloc(size); strncpy(out->compressor, argv[1], size); --argc; @@ -268,7 +268,7 @@ int simple_archiver_parse_args(int argc, const char **argv, simple_archiver_print_usage(); return 1; } - int size = strlen(argv[1]) + 1; + size_t size = strlen(argv[1]) + 1; out->decompressor = malloc(size); strncpy(out->decompressor, argv[1], size); --argc; @@ -297,15 +297,15 @@ int simple_archiver_parse_args(int argc, const char **argv, } else { if (out->working_files == NULL) { out->working_files = malloc(sizeof(char *) * 2); - unsigned int arg_idx = + size_t arg_idx = simple_archiver_parser_internal_get_first_non_current_idx(argv[0]); - unsigned int arg_length = strlen(argv[0] + arg_idx) + 1; + size_t arg_length = strlen(argv[0] + arg_idx) + 1; out->working_files[0] = malloc(arg_length); strncpy(out->working_files[0], argv[0] + arg_idx, arg_length); simple_archiver_parser_internal_remove_end_slash(out->working_files[0]); out->working_files[1] = NULL; } else { - int working_size = 1; + size_t working_size = 1; char **ptr = out->working_files; while (ptr && *ptr) { ++working_size; @@ -318,9 +318,9 @@ int simple_archiver_parse_args(int argc, const char **argv, // Set new actual last element to NULL. out->working_files[working_size] = NULL; - unsigned int arg_idx = + size_t arg_idx = simple_archiver_parser_internal_get_first_non_current_idx(argv[0]); - int size = strlen(argv[0] + arg_idx) + 1; + size_t size = strlen(argv[0] + arg_idx) + 1; // Set last element to the arg. out->working_files[working_size - 1] = malloc(size); strncpy(out->working_files[working_size - 1], argv[0] + arg_idx, size); @@ -392,7 +392,7 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames( fstatat(AT_FDCWD, file_path, &st, AT_SYMLINK_NOFOLLOW); if ((st.st_mode & S_IFMT) == S_IFREG || (st.st_mode & S_IFMT) == S_IFLNK) { // Is a regular file or a symbolic link. - int len = strlen(file_path) + 1; + size_t len = strlen(file_path) + 1; char *filename = malloc(len); strncpy(filename, file_path, len); if (simple_archiver_hash_map_get(hash_map, filename, len - 1) == NULL) { @@ -472,11 +472,11 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames( } // fprintf(stderr, "dir entry in %s is %s\n", next, // dir_entry->d_name); - int combined_size = strlen(next) + strlen(dir_entry->d_name) + 2; + size_t combined_size = strlen(next) + strlen(dir_entry->d_name) + 2; char *combined_path = malloc(combined_size); snprintf(combined_path, combined_size, "%s/%s", next, dir_entry->d_name); - unsigned int valid_idx = + size_t valid_idx = simple_archiver_parser_internal_get_first_non_current_idx( combined_path); if (valid_idx > 0) { @@ -574,11 +574,10 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames( SDArchiverFileInfo *file_info = iter->data; // Remove leading "./" entries from files_list. - unsigned int idx = - simple_archiver_parser_internal_get_first_non_current_idx( - file_info->filename); + size_t idx = simple_archiver_parser_internal_get_first_non_current_idx( + file_info->filename); if (idx > 0) { - int len = strlen(file_info->filename) + 1 - idx; + size_t len = strlen(file_info->filename) + 1 - idx; char *substr = malloc(len); strncpy(substr, file_info->filename + idx, len); free(file_info->filename); diff --git a/src/parser_internal.h b/src/parser_internal.h index 4741abb..7da9d60 100644 --- a/src/parser_internal.h +++ b/src/parser_internal.h @@ -21,7 +21,7 @@ #include "parser.h" -unsigned int simple_archiver_parser_internal_get_first_non_current_idx( +size_t simple_archiver_parser_internal_get_first_non_current_idx( const char *filename); #endif