From f359ac8a0df9ca7d3858c8c9ed31d5aacc2b2d9b Mon Sep 17 00:00:00 2001 From: "Paolo G. Giarrusso" Date: Thu, 3 May 2018 23:26:04 +0200 Subject: [PATCH 1/6] Use correct printf format specifiers for size_t --- sparsebundlefs.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sparsebundlefs.cpp b/sparsebundlefs.cpp index 749cc96..1b6b874 100644 --- a/sparsebundlefs.cpp +++ b/sparsebundlefs.cpp @@ -305,7 +305,7 @@ static void sparsebundle_read_buf_close_files() { sparsebundle_t *sparsebundle = sparsebundle_current(); - syslog(LOG_DEBUG, "closing %u open file descriptor(s)", sparsebundle->open_files.size()); + syslog(LOG_DEBUG, "closing %zu open file descriptor(s)", sparsebundle->open_files.size()); map::iterator iter; for(iter = sparsebundle->open_files.begin(); iter != sparsebundle->open_files.end(); ++iter) { @@ -360,7 +360,7 @@ static int sparsebundle_read_buf(const char *path, struct fuse_bufvec **bufp, copy(buffers.begin(), buffers.end(), buffer_vector->buf); - syslog(LOG_DEBUG, "returning %d buffers to fuse", buffer_vector->count); + syslog(LOG_DEBUG, "returning %zu buffers to fuse", buffer_vector->count); *bufp = buffer_vector; return ret; From a5f9c89dd9ba57f6789fbdd49c1f24ed51ea6a95 Mon Sep 17 00:00:00 2001 From: Fedor Bezrukov Date: Thu, 3 May 2018 23:29:17 +0200 Subject: [PATCH 2/6] Fix narrowing from ssize_t to size_t in sparsebundle_read_buf_process_band --- sparsebundlefs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sparsebundlefs.cpp b/sparsebundlefs.cpp index 1b6b874..86e9649 100644 --- a/sparsebundlefs.cpp +++ b/sparsebundlefs.cpp @@ -264,7 +264,7 @@ static int sparsebundle_read_buf_prepare_file(const char *path) static int sparsebundle_read_buf_process_band(const char *band_path, size_t length, off_t offset, void *read_data) { - ssize_t read = 0; + size_t read = 0; vector *buffers = static_cast*>(read_data); 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 3/6] 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; From 7414e516c26a247eefb1fd97839c547c2d8803d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 4 May 2018 00:11:34 +0200 Subject: [PATCH 4/6] Track the number of times a sparsebundle has been opened using uintmax_t Instead of off_t, which doesn't make sense, being both unsigned and not a very logical type to use for a counter. --- sparsebundlefs.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sparsebundlefs.cpp b/sparsebundlefs.cpp index d874646..2c0df8a 100644 --- a/sparsebundlefs.cpp +++ b/sparsebundlefs.cpp @@ -56,7 +56,7 @@ struct sparsebundle_t { char *mountpoint; size_t band_size; size_t size; - off_t times_opened; + uintmax_t times_opened; #if FUSE_SUPPORTS_ZERO_COPY map open_files; #endif @@ -370,6 +370,7 @@ static int sparsebundle_release(const char *path, struct fuse_file_info *) { sparsebundle_t *sparsebundle = sparsebundle_current(); + assert(sparsebundle->times_opened); sparsebundle->times_opened--; syslog(LOG_DEBUG, "closed %s%s, now referenced %ju times", sparsebundle->mountpoint, path, uintmax_t(sparsebundle->times_opened)); From 53ca0a682b5c9a794c121b4651a026e8686effe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 4 May 2018 00:22:58 +0200 Subject: [PATCH 5/6] Explicitly enable C++11 mode --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1dcb648..b68f498 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ TARGET = sparsebundlefs PKG_CONFIG = pkg-config -override CFLAGS += -Wall -O2 -g +override CFLAGS += -std=c++11 -Wall -O2 -g GCC_4_2_OR_HIGHER := $(shell expr `$(CXX) -dumpversion | sed 's/\.//g'` \>= 420) ifeq "$(GCC_4_2_OR_HIGHER)" "1" From d500e0c37ba96f1e97edf7ddbcd2a74a366a2201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 4 May 2018 00:23:24 +0200 Subject: [PATCH 6/6] Enable -pedantic and fix use of %p with non-void* pointers --- Makefile | 2 +- sparsebundlefs.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index b68f498..f6ab514 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ TARGET = sparsebundlefs PKG_CONFIG = pkg-config -override CFLAGS += -std=c++11 -Wall -O2 -g +override CFLAGS += -std=c++11 -Wall -pedantic -O2 -g GCC_4_2_OR_HIGHER := $(shell expr `$(CXX) -dumpversion | sed 's/\.//g'` \>= 420) ifeq "$(GCC_4_2_OR_HIGHER)" "1" diff --git a/sparsebundlefs.cpp b/sparsebundlefs.cpp index 2c0df8a..1fa184f 100644 --- a/sparsebundlefs.cpp +++ b/sparsebundlefs.cpp @@ -196,7 +196,7 @@ static int sparsebundle_read_process_band(const char *band_path, size_t length, char** buffer = static_cast(read_data); syslog(LOG_DEBUG, "reading %zu bytes at offset %ju into %p", - length, uintmax_t(offset), *buffer); + length, uintmax_t(offset), static_cast(*buffer)); int band_file = open(band_path, O_RDONLY); if (band_file != -1) { @@ -221,7 +221,8 @@ static int sparsebundle_read_pad_with_zeroes(size_t length, void *read_data) { char** buffer = static_cast(read_data); - syslog(LOG_DEBUG, "padding %zu bytes of zeroes into %p", length, *buffer); + syslog(LOG_DEBUG, "padding %zu bytes of zeroes into %p", + length, static_cast(*buffer)); memset(*buffer, 0, length); *buffer += length;