From 90c81b177610f4e5b44d10b4dcb42f045d4f0c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 4 May 2018 00:02:23 +0200 Subject: [PATCH] Move to size_t and other unsigned types, over off_t We never expect the offset to be negative, so instead of keeping it around as off_t, and having to cast to size_t to deal with signed vs unsigned comparisons, we just use size_t directly. There's still a few uses of off_t and ssize_t around, but those will need further refactoring to clean up. --- sparsebundlefs.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/sparsebundlefs.cpp b/sparsebundlefs.cpp index 86e9649..d874646 100644 --- a/sparsebundlefs.cpp +++ b/sparsebundlefs.cpp @@ -37,7 +37,6 @@ #include #include #include -#include #include #include #include @@ -55,8 +54,8 @@ static const char image_path[] = "/sparsebundle.dmg"; struct sparsebundle_t { char *path; char *mountpoint; - off_t band_size; - off_t size; + size_t band_size; + size_t size; off_t times_opened; #if FUSE_SUPPORTS_ZERO_COPY map open_files; @@ -140,7 +139,8 @@ static int sparsebundle_iterate_bands(const char *path, size_t length, off_t off sparsebundle_t *sparsebundle = sparsebundle_current(); - if (offset >= sparsebundle->size) + assert(offset >= 0); + if (static_cast(offset) >= sparsebundle->size) return 0; if (offset + length > sparsebundle->size) @@ -150,14 +150,13 @@ static int sparsebundle_iterate_bands(const char *path, size_t length, off_t off size_t bytes_read = 0; while (bytes_read < length) { - off_t band_number = (offset + bytes_read) / sparsebundle->band_size; - off_t band_offset = (offset + bytes_read) % sparsebundle->band_size; + uintmax_t band_number = (offset + bytes_read) / sparsebundle->band_size; + uintmax_t band_offset = (offset + bytes_read) % sparsebundle->band_size; - ssize_t to_read = min(static_cast(length - bytes_read), - sparsebundle->band_size - band_offset); + size_t to_read = min(length - bytes_read, sparsebundle->band_size - band_offset); char *band_path; - if (asprintf(&band_path, "%s/bands/%jx", sparsebundle->path, uintmax_t(band_number)) == -1) { + if (asprintf(&band_path, "%s/bands/%jx", sparsebundle->path, band_number) == -1) { syslog(LOG_ERR, "failed to resolve band name"); return -errno; } @@ -173,7 +172,7 @@ static int sparsebundle_iterate_bands(const char *path, size_t length, off_t off free(band_path); - if (read < to_read) { + if (static_cast(read) < to_read) { to_read = to_read - read; syslog(LOG_DEBUG, "missing %zu bytes from band %jx, padding with zeroes", to_read, uintmax_t(band_number)); @@ -441,10 +440,10 @@ static int sparsebundle_opt_proc(void *data, const char *arg, int key, struct fu return SPARSEBUNDLE_OPT_IGNORED; } -static off_t read_size(const string &str) +static size_t read_size(const string &str) { uintmax_t value = strtoumax(str.c_str(), 0, 10); - if (errno == ERANGE || value > uintmax_t(numeric_limits::max())) + if (errno == ERANGE) sparsebundle_fatal_error("disk image too large (%s bytes)", str.c_str()); return value;