Fixing multiple unsigned to signed int conversion errors

Signed-off-by: Otavio Pontes <otavio.pontes@intel.com>
This commit is contained in:
Otavio Pontes
2020-05-12 12:27:06 -07:00
parent 235ea872a8
commit e1fd274599
25 changed files with 106 additions and 87 deletions
+1 -1
View File
@@ -106,7 +106,7 @@ static int check_disk_space_availability(struct list *to_install_bundles)
{
char *filepath = NULL;
long fs_free = 0;
long bundle_size = 0;
size_t bundle_size = 0;
if (globals.skip_diskspace_check) {
return 0;
+4 -4
View File
@@ -197,7 +197,7 @@ static void print_bundle_files(struct list *files)
info("\nTotal files: %ld\n", count);
}
static void print_bundle_size(struct manifest *manifest, long size, bool bundle_installed)
static void print_bundle_size(struct manifest *manifest, size_t size, bool bundle_installed)
{
char *pretty_size;
@@ -215,9 +215,9 @@ static void print_bundle_size(struct manifest *manifest, long size, bool bundle_
FREE(pretty_size);
}
static long get_bundle_size(struct manifest *mom, bool bundle_installed)
static size_t get_bundle_size(struct manifest *mom, bool bundle_installed)
{
long bundle_size;
size_t bundle_size;
struct list *bundles_not_installed = NULL;
struct list *iter;
struct manifest *manifest;
@@ -298,7 +298,7 @@ enum swupd_code bundle_info(char *bundle)
struct manifest *latest_manifest = NULL;
struct file *file = NULL;
struct list *subs = NULL;
long bundle_size;
size_t bundle_size;
bool installed = is_installed_bundle(bundle);
bool tracked = is_tracked_bundle(bundle);
+4 -4
View File
@@ -296,9 +296,9 @@ static void get_removable_dependencies(struct manifest *mom, struct list **bundl
list_free_list_and_data(required_bundles, manifest_free_data);
}
static void print_remove_summary(unsigned int requested, unsigned int bad, unsigned int total_removed)
static void print_remove_summary(int requested, int bad, int total_removed)
{
int deps_removed;
long deps_removed;
if (cmdline_option_orphans) {
if (bad > 0) {
@@ -333,8 +333,8 @@ enum swupd_code execute_remove_bundles_extra(struct list *bundles, remove_extra_
{
enum swupd_code ret_code = SWUPD_OK;
enum swupd_code ret = SWUPD_OK;
unsigned int bad = 0;
unsigned int total = 0;
int bad = 0;
int total = 0;
int current_version = CURRENT_OS_VERSION;
struct manifest *current_mom = NULL;
struct list *subs = NULL;
+7 -7
View File
@@ -51,7 +51,7 @@ static struct {
static struct {
int files_removed;
long bytes_removed;
size_t bytes_removed;
} stats;
int clean_get_stats(void)
@@ -175,7 +175,7 @@ static enum swupd_code remove_if(const char *path, bool dry_run, remove_predicat
}
if (ret == 0) {
stats.files_removed++;
stats.bytes_removed += size;
stats.bytes_removed += long_to_ulong(size);
}
}
@@ -280,14 +280,14 @@ static char *read_mom_contents(int version)
int ret;
struct stat stat;
ret = fstat(fd, &stat);
if (ret != 0) {
if (ret != 0 || stat.st_size <= 0) {
goto end;
}
contents = malloc_or_die(stat.st_size + 1);
contents = malloc_or_die(long_to_ulong(stat.st_size) + 1);
ret = fread(contents, stat.st_size, 1, f);
if (ret != 1) {
size_t read = fread(contents, long_to_ulong(stat.st_size), 1, f);
if (read != 1) {
FREE(contents);
contents = NULL;
} else {
@@ -364,7 +364,7 @@ static enum swupd_code clean_staged_manifests(const char *path, bool dry_run, bo
size = 0;
}
if (!rmdir(version_dir) || (dry_run && all)) {
stats.bytes_removed += size;
stats.bytes_removed += long_to_ulong(size);
}
FREE(version_dir);
+1 -1
View File
@@ -81,7 +81,7 @@ static void save_swupd_binary_path()
{
/* we need to resolve the whole path to swupd first, proc/self/exe
* is a symbolic link to the executable that is running the current process */
int path_length;
ssize_t path_length;
path_length = readlink("/proc/self/exe", swupd_binary, sizeof(swupd_binary));
if (path_length <= 0 || path_length >= LINE_MAX) {
// On errors fallback to default location
+5 -3
View File
@@ -28,6 +28,7 @@
#include <sys/errno.h>
#include "archives.h"
#include "int.h"
#include "log.h"
#include "macros.h"
#include "strings.h"
@@ -67,6 +68,7 @@ static int _archive_check_err(struct archive *ar, int ret)
static int copy_data(struct archive *ar, struct archive *aw)
{
int r;
long bytes;
const void *buffer;
size_t size;
off_t offset;
@@ -79,9 +81,9 @@ static int copy_data(struct archive *ar, struct archive *aw)
return r;
}
r = archive_write_data_block(aw, buffer, size, offset);
if (r < ARCHIVE_OK) {
return r;
bytes = archive_write_data_block(aw, buffer, size, offset);
if (bytes < ARCHIVE_OK) {
return long_to_int(bytes);
}
}
return 0;
+4 -3
View File
@@ -37,7 +37,7 @@ size_t hashmap_hash_from_string(const char *key)
size_t hash = 0;
while (*key) {
hash = hash * 29 /* a prime number */ + *key;
hash = hash * 29 /* a prime number */ + (unsigned char)*key;
key++;
}
@@ -46,7 +46,8 @@ size_t hashmap_hash_from_string(const char *key)
static inline struct list **get_hashmap_list(struct hashmap *hashmap, const void *data)
{
return &hashmap->map[hashmap->hash(data) & HASH_MASK(hashmap->mask_bits)];
int mask = HASH_MASK(hashmap->mask_bits);
return &hashmap->map[hashmap->hash(data) & (size_t)mask];
}
static unsigned int calc_bits(size_t capacity)
@@ -70,7 +71,7 @@ struct hashmap *hashmap_new(size_t capacity, hash_equal_fn_t equal, hash_fn_t ha
{
struct hashmap *hashmap;
unsigned int mask_bits = calc_bits(capacity);
size_t real_capacity = HASH_SIZE(mask_bits);
size_t real_capacity = (size_t)HASH_SIZE(mask_bits);
hashmap = malloc_or_die(sizeof(struct hashmap) + real_capacity * sizeof(struct list *));
hashmap->mask_bits = mask_bits;
+2 -1
View File
@@ -26,6 +26,7 @@
#include <string.h>
#include <unistd.h>
#include "int.h"
#include "list.h"
#include "macros.h"
#include "strings.h"
@@ -194,7 +195,7 @@ struct list *list_sort(struct list *list, comparison_fn_t comparison_fn)
{
list = list_head(list);
int len = list_len(list);
return list_merge_sort(list, len, comparison_fn);
return list_merge_sort(list, int_to_uint(len), comparison_fn);
}
bool list_is_sorted(struct list *list, comparison_fn_t comparison_fn)
+7 -5
View File
@@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include "int.h"
#include "macros.h"
#include "strings.h"
@@ -79,7 +80,8 @@ char *str_join(const char *separator, struct list *strings)
{
char *str, *ret;
size_t str_size = 1; // 1 for '\0'
size_t sep_size, printed;
size_t sep_size;
int printed;
struct list *i;
if (!separator) {
@@ -98,11 +100,11 @@ char *str_join(const char *separator, struct list *strings)
for (i = strings; i; i = i->next) {
printed = snprintf(str, str_size, "%s%s",
i == strings ? "" : separator, (char *)i->data);
if (printed >= str_size) {
if (printed < 0 || int_to_uint(printed) >= str_size) {
goto error; //shouldn't happen
}
str_size -= printed;
str += printed;
str_size -= int_to_uint(printed);
str += int_to_uint(printed);
}
return ret;
@@ -209,7 +211,7 @@ char *str_to_lower(const char *str)
char *str_lower = malloc_or_die(str_len(str) + 1);
for (int i = 0; str[i]; i++) {
str_lower[i] = tolower(str[i]);
str_lower[i] = (char)tolower(str[i]);
}
str_lower[str_len(str)] = '\0';
+5 -6
View File
@@ -218,10 +218,10 @@ long get_available_space(const char *path)
struct statvfs stat;
if (statvfs(path, &stat) != 0) {
return -1;
return -ENOENT;
}
return stat.f_bsize * stat.f_bavail;
return ulong_to_long(stat.f_bsize * stat.f_bavail);
}
int copy_all(const char *src, const char *dst)
@@ -346,7 +346,7 @@ long sys_file_hardlink_count(const char *file)
return -errno;
}
return st.st_nlink;
return ulong_to_long(st.st_nlink);
}
void journal_log_error(const char *message)
@@ -429,8 +429,7 @@ char *sys_path_join(const char *fmt, ...)
{
char *path;
va_list ap;
int len;
int i, j;
size_t len, i, j;
/* merge arguments into one path */
va_start(ap, fmt);
@@ -651,7 +650,7 @@ void *sys_mmap_file(const char *file, size_t *file_length)
debug("Failed to stat %s file\n", file);
goto error;
}
*file_length = st.st_size;
*file_length = long_to_ulong(st.st_size);
buffer = mmap(NULL, *file_length, PROT_READ, MAP_PRIVATE, fd, 0);
if (buffer == MAP_FAILED) {
+2 -2
View File
@@ -126,7 +126,7 @@ error:
int tp_task_schedule(struct tp *tp, tp_task_run_t run, void *data)
{
int r = -1;
ssize_t r = -1;
struct task task;
if (tp->num_threads == 0) {
@@ -148,7 +148,7 @@ int tp_task_schedule(struct tp *tp, tp_task_run_t run, void *data)
}
error("Thread pool task scheduling failed: %d - %s\n",
errno, strerror(errno));
return r;
return -errno;
}
}
+2 -2
View File
@@ -251,7 +251,7 @@ extern enum swupd_code compute_hash(struct file *file, char *filename) __attribu
/* manifest.c */
/* Calculate the total contentsize of a manifest list */
extern long get_manifest_list_contentsize(struct list *manifests);
extern unsigned long get_manifest_list_contentsize(struct list *manifests);
extern struct list *recurse_manifest(struct manifest *manifest, struct list *subs, const char *component, bool server, int *err);
extern struct list *consolidate_files(struct list *files);
extern struct list *filter_out_deleted_files(struct list *files);
@@ -296,7 +296,7 @@ extern bool is_url_allowed(const char *url);
extern bool is_url_insecure(const char *url);
extern void remove_trailing_slash(char *url);
extern void print_header(const char *header);
extern void prettify_size(long size_in_bytes, char **pretty_size);
extern void prettify_size(size_t size_in_bytes, char **pretty_size);
extern bool confirm_action(void);
extern bool is_binary(const char *filename);
extern int ensure_root_owned_dir(const char *dirname);
+2 -2
View File
@@ -425,7 +425,7 @@ enum download_status process_curl_error_codes(int curl_ret, CURL *curl_handle)
*/
curl_off_t curl_sz = 0;
if (curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD_T, &curl_sz) == CURLE_OK) {
total_curl_sz += curl_sz;
total_curl_sz += long_to_ulong(curl_sz);
}
if (curl_ret == CURLE_OK || curl_ret == CURLE_HTTP_RETURNED_ERROR) {
@@ -661,7 +661,7 @@ static int retry_download_loop(const char *url, char *filename, struct curl_file
if (sleep_time) {
info("Waiting %d seconds before retrying the download\n", sleep_time);
}
sleep(sleep_time);
sleep(int_to_uint(sleep_time));
sleep_time = (sleep_time * DELAY_MULTIPLIER) > MAX_DELAY ? MAX_DELAY : (sleep_time * DELAY_MULTIPLIER);
info("Retry #%d downloading from %s\n", current_retry, url);
continue;
+5 -6
View File
@@ -101,7 +101,7 @@ struct swupd_curl_parallel_handle {
struct multi_curl_file {
struct curl_file file; /* Curl file information */
enum download_status status; /* status of last download try */
char retries; /* Number of retried performed so far */
int retries; /* Number of retried performed so far */
CURL *curl; /* curl handle if downloading */
char *url; /* The url to be downloaded from */
size_t hash_key; /* hash_key of this file */
@@ -255,7 +255,7 @@ void swupd_curl_parallel_download_set_progress_callback(struct swupd_curl_parall
}
// Try to process at most COUNT messages from the curl multi-stack.
static int perform_curl_io_and_complete(struct swupd_curl_parallel_handle *h, int count)
static int perform_curl_io_and_complete(struct swupd_curl_parallel_handle *h, size_t count)
{
CURLMsg *msg;
CURLcode curl_ret;
@@ -415,8 +415,7 @@ static int poll_fewer_than(struct swupd_curl_parallel_handle *h, size_t xfer_que
// Instead of using "numfds" as a hint for how many transfers
// to process, try to drain the queue to the lower bound.
int remaining = h->mcurl_size - xfer_queue_low;
size_t remaining = h->mcurl_size - xfer_queue_low;
if (perform_curl_io_and_complete(h, remaining) != 0) {
return -1;
}
@@ -562,7 +561,7 @@ int swupd_curl_parallel_download_enqueue(struct swupd_curl_parallel_handle *h, c
file->data = data;
if (hash) {
file->hash = hash;
file->hash_key = HASH_TO_KEY(hash);
file->hash_key = (size_t)HASH_TO_KEY(hash);
} else {
file->hash_key = hashmap_hash_from_string(filename);
}
@@ -644,7 +643,7 @@ int swupd_curl_parallel_download_end(struct swupd_curl_parallel_handle *h, int *
l = l->next;
}
if (retry) {
sleep(h->retry_delay);
sleep(int_to_uint(h->retry_delay));
h->retry_delay = (h->retry_delay * DELAY_MULTIPLIER) > MAX_DELAY ? MAX_DELAY : (h->retry_delay * DELAY_MULTIPLIER);
}
}
+2 -2
View File
@@ -121,7 +121,7 @@ enum swupd_code walk_tree(struct manifest *manifest, const char *start, bool fix
}
goto tidy; /* Already printed out of memory */
}
qsort(F, nF, sizeof(*F), &cmp_filerecord_filename);
qsort(F, long_to_ulong(nF), sizeof(*F), &cmp_filerecord_filename);
/* Interesting question, would it be faster to sort this linked list,
* or convert it to an array of pointers, or just pull them off one
* at a time? Try one at a time first.
@@ -137,7 +137,7 @@ enum swupd_code walk_tree(struct manifest *manifest, const char *start, bool fix
if (file->is_deleted && !file->is_ghosted) {
continue;
}
found = bsearch(&file->filename, F, nF, sizeof(*F), &cmp_string_filerecord_filename);
found = bsearch(&file->filename, F, long_to_ulong(nF), sizeof(*F), &cmp_string_filerecord_filename);
if (found) {
found->in_manifest = true;
}
+3 -3
View File
@@ -517,10 +517,10 @@ void save_cmd(char **argv)
globals.swupd_argv = argv;
}
size_t get_max_xfer(size_t default_max_xfer)
unsigned int get_max_xfer(unsigned int default_max_xfer)
{
if (max_parallel_downloads > 0) {
return max_parallel_downloads;
return int_to_uint(max_parallel_downloads);
}
return default_max_xfer;
@@ -683,7 +683,7 @@ static char *generate_optstring(struct option *opts, unsigned int num_opts)
while (opts->name) {
if (isalpha(opts->val)) {
optstring[i++] = opts->val;
optstring[i++] = (char)opts->val;
if (opts->has_arg) {
optstring[i++] = ':';
}
+1 -1
View File
@@ -90,7 +90,7 @@ bool globals_init(void);
void globals_deinit(void);
void global_print_help(void);
size_t get_max_xfer(size_t default_max_xfer);
unsigned int get_max_xfer(unsigned int default_max_xfer);
void save_cmd(char **argv);
bool set_path_prefix(char *path);
+2 -2
View File
@@ -80,7 +80,7 @@ static void hmac_sha256_for_data(char *hash,
return;
}
if (HMAC(EVP_sha256(), (const void *)key, key_len, data, data_len, digest, &digest_len) == NULL) {
if (HMAC(EVP_sha256(), (const void *)key, ulong_to_int(key_len), data, data_len, digest, &digest_len) == NULL) {
hash_set_zeros(hash);
return;
}
@@ -157,7 +157,7 @@ int compute_hash_lazy(struct file *file, char *filename)
/* TODO: how should we properly handle compute_hash() failures? */
enum swupd_code compute_hash(struct file *file, char *filename)
{
int ret;
ssize_t ret;
char key[SWUPD_HASH_LEN];
size_t key_len;
unsigned char *blob;
+1 -1
View File
@@ -897,7 +897,7 @@ void print_header(const char *header)
info("\n");
}
void prettify_size(long size_in_bytes, char **pretty_size)
void prettify_size(size_t size_in_bytes, char **pretty_size)
{
double size;
+5 -1
View File
@@ -48,6 +48,7 @@ int p_lockfile(void)
}
int ret;
ssize_t bytes;
pid_t pid = getpid();
struct flock fl = {
.l_type = F_WRLCK,
@@ -84,7 +85,10 @@ int p_lockfile(void)
/* speculatively dump our pid in the file,
* that may be useful for debug */
ret = ftruncate(lock_fd, 0);
ret = write(lock_fd, &pid, sizeof(pid));
bytes = write(lock_fd, &pid, sizeof(pid));
if (ret < 0 || bytes < 0) {
debug("Problem writing PID on lock file\n");
}
/* our lock_fd represents the lock */
return lock_fd;
+3 -3
View File
@@ -814,7 +814,7 @@ void populate_file_struct(struct file *file, char *filename)
file->stat.st_uid = stat.st_uid;
file->stat.st_gid = stat.st_gid;
file->stat.st_rdev = stat.st_rdev;
file->stat.st_size = stat.st_size;
file->stat.st_size = long_to_ulong(stat.st_size);
if (S_ISLNK(stat.st_mode)) {
file->is_file = 0;
@@ -978,9 +978,9 @@ void manifest_free_array(struct file **array)
FREE(array);
}
long get_manifest_list_contentsize(struct list *manifests)
unsigned long get_manifest_list_contentsize(struct list *manifests)
{
long total_size = 0;
unsigned long total_size = 0;
struct list *ptr = NULL;
for (ptr = list_head(manifests); ptr; ptr = ptr->next) {
+2 -2
View File
@@ -120,7 +120,7 @@ static long compute_bundle_size(const char *bundle_name)
struct manifest *m;
struct list *include_list = NULL;
struct list *l;
long size;
unsigned long size;
bool is_installed;
m = list_search(manifest_list, bundle_name, manifest_str_cmp);
@@ -145,7 +145,7 @@ static long compute_bundle_size(const char *bundle_name)
list_free_list(include_list);
return size;
return ulong_to_long(size);
}
static long get_bundle_size(const char *bundle)
+13 -2
View File
@@ -186,8 +186,19 @@ bool signature_verify_data(const void *data, size_t data_len, const void *sig_da
BIO *verify_BIO = NULL;
char *errorstr = NULL;
PKCS7 *p7 = NULL;
int sig_data_len_int, data_len_int;
sig_BIO = BIO_new_mem_buf(sig_data, sig_data_len);
if (ulong_to_int_err(sig_data_len, &sig_data_len_int) != 0) {
error("Data to big to be a signature file (size = %ld)\n", sig_data_len);
goto error;
}
if (ulong_to_int_err(data_len, &data_len_int) != 0) {
error("Data to big to verify signature (size = %ld)\n", data_len);
goto error;
}
sig_BIO = BIO_new_mem_buf(sig_data, sig_data_len_int);
if (!sig_BIO) {
string_or_die(&errorstr, "Unable to load signature data into BIO");
goto error;
@@ -200,7 +211,7 @@ bool signature_verify_data(const void *data, size_t data_len, const void *sig_da
goto error;
}
data_BIO = BIO_new_mem_buf(data, data_len);
data_BIO = BIO_new_mem_buf(data, data_len_int);
if (!data_BIO) {
string_or_die(&errorstr, "Unable to load data into BIO");
goto error;
+1 -1
View File
@@ -102,7 +102,7 @@ static int get_sig_inmemory(char *url, struct curl_file_data *tmp_version_sig)
ret = sig_size;
}
tmp_version_sig->capacity = ret;
tmp_version_sig->capacity = int_to_uint(ret);
tmp_version_sig->data = malloc_or_die(int_to_uint(ret) * sizeof(char));
+22 -22
View File
@@ -31,30 +31,30 @@ static void test_int_to_uint()
check(ret == -ERANGE);
}
static void test_ssize_to_size()
static void test_long_to_ulong()
{
int ret;
size_t b;
ret = ssize_to_size_err(0, &b);
ret = long_to_ulong_err(0, &b);
check(ret == 0);
check(b == 0);
ret = ssize_to_size_err(123, &b);
ret = long_to_ulong_err(123, &b);
check(ret == 0);
check(b == 123);
ret = ssize_to_size_err(SSIZE_MAX, &b);
ret = long_to_ulong_err(SSIZE_MAX, &b);
check(ret == 0);
check(b == SSIZE_MAX);
ret = ssize_to_size_err(-1, &b);
ret = long_to_ulong_err(-1, &b);
check(ret == -ERANGE);
ret = ssize_to_size_err(-SSIZE_MAX, &b);
ret = long_to_ulong_err(-SSIZE_MAX, &b);
check(ret == -ERANGE);
ret = ssize_to_size_err(-SSIZE_MAX - 1, &b);
ret = long_to_ulong_err(-SSIZE_MAX - 1, &b);
check(ret == -ERANGE);
}
@@ -83,61 +83,61 @@ static void test_uint_to_int()
}
static void test_size_to_ssize()
static void test_ulong_to_long()
{
int ret;
ssize_t b;
ret = size_to_ssize_err(0, &b);
ret = ulong_to_long_err(0, &b);
check(ret == 0);
check(b == 0);
ret = size_to_ssize_err(123, &b);
ret = ulong_to_long_err(123, &b);
check(ret == 0);
check(b == 123);
ret = size_to_ssize_err(SSIZE_MAX, &b);
ret = ulong_to_long_err(SSIZE_MAX, &b);
check(ret == 0);
check(b == SSIZE_MAX);
ret = size_to_ssize_err((size_t)SSIZE_MAX + 1, &b);
ret = ulong_to_long_err((size_t)SSIZE_MAX + 1, &b);
check(ret == -ERANGE);
ret = size_to_ssize_err(SIZE_MAX, &b);
ret = ulong_to_long_err(SIZE_MAX, &b);
check(ret == -ERANGE);
}
static void test_size_to_int()
static void test_ulong_to_int()
{
int ret;
int b;
ret = size_to_int_err(0, &b);
ret = ulong_to_int_err(0, &b);
check(ret == 0);
check(b == 0);
ret = size_to_int_err(123, &b);
ret = ulong_to_int_err(123, &b);
check(ret == 0);
check(b == 123);
ret = size_to_int_err(INT_MAX, &b);
ret = ulong_to_int_err(INT_MAX, &b);
check(ret == 0);
check(b == INT_MAX);
ret = size_to_int_err((size_t)INT_MAX + 1, &b);
ret = ulong_to_int_err((size_t)INT_MAX + 1, &b);
check(ret == -ERANGE);
ret = size_to_int_err(SIZE_MAX, &b);
ret = ulong_to_int_err(SIZE_MAX, &b);
check(ret == -ERANGE);
}
int main()
{
test_int_to_uint();
test_ssize_to_size();
test_long_to_ulong();
test_uint_to_int();
test_size_to_ssize();
test_size_to_int();
test_ulong_to_long();
test_ulong_to_int();
return 0;
}