From 969cb5ca31b9917e838b89500550e3f540f84b87 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 3 Oct 2026 16:50:31 +0200 Subject: [PATCH] Make sgetbs() return the number of characters read on success --- cmdline/mkstream.c | 2 +- cmdline/selftest.c | 24 ++++++++++++++++++++++++ cmdline/state.c | 2 +- cmdline/stream.c | 10 ++++++++-- cmdline/stream.h | 2 +- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/cmdline/mkstream.c b/cmdline/mkstream.c index 224e8380..146110fe 100644 --- a/cmdline/mkstream.c +++ b/cmdline/mkstream.c @@ -144,7 +144,7 @@ void test(int advise_mode) char copy[STR_MAX]; memset(str, ' ' + j, j - 1); str[j - 1] = 0; - if (sgetbs(s, copy, sizeof(copy)) != 0 || strcmp(copy, str) != 0) { + if (sgetbs(s, copy, sizeof(copy)) != (ssize_t)(j - 1) || strcmp(copy, str) != 0) { /* LCOV_EXCL_START */ exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ diff --git a/cmdline/selftest.c b/cmdline/selftest.c index a88f61e5..0aae3f3b 100644 --- a/cmdline/selftest.c +++ b/cmdline/selftest.c @@ -2183,6 +2183,30 @@ static void test_stream(void) exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } + + /* sgetbs test */ + unsigned char buf_bs[] = { 0x85, 'h', 'e', 'l', 'l', 'o' }; + char str_bs[16]; + memset(&f, 0, sizeof(f)); + f.pos = buf_bs; + f.end = buf_bs + sizeof(buf_bs); + if (sgetbs(&f, str_bs, sizeof(str_bs)) != 5 || strcmp(str_bs, "hello") != 0) { + /* LCOV_EXCL_START */ + log_fatal(EINTERNAL, "test_stream: sgetbs valid string failed\n"); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + /* sgetbs buffer too small test */ + memset(&f, 0, sizeof(f)); + f.pos = buf_bs; + f.end = buf_bs + sizeof(buf_bs); + if (sgetbs(&f, str_bs, 5) >= 0) { + /* LCOV_EXCL_START */ + log_fatal(EINTERNAL, "test_stream: sgetbs small buffer accepted\n"); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } } static void test_extent_run(void) diff --git a/cmdline/state.c b/cmdline/state.c index 83144634..a233b628 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -2416,7 +2416,7 @@ static void state_read_content(struct snapraid_state* state, const char* path, S int has_info; int has_invalid_parity; char buffer[PATH_MAX]; - int ret; + ssize_t ret; tommy_array disk_mapping; uint32_t mapping_max; tommy_hashdyn bucket_hash; diff --git a/cmdline/stream.c b/cmdline/stream.c index a7c49e33..dd978473 100644 --- a/cmdline/stream.c +++ b/cmdline/stream.c @@ -691,7 +691,7 @@ int sgetble32(STREAM* f, uint32_t* value) return 0; } -int sgetbs(STREAM* f, char* str, size_t size) +ssize_t sgetbs(STREAM* f, char* str, size_t size) { uint64_t len; @@ -709,7 +709,13 @@ int sgetbs(STREAM* f, char* str, size_t size) str[len] = 0; - return sread(f, str, len); + if (sread(f, str, len) != 0) { + /* LCOV_EXCL_START */ + return -1; + /* LCOV_EXCL_STOP */ + } + + return len; } int swrite_uncached(const void* void_data, size_t size, STREAM* f) diff --git a/cmdline/stream.h b/cmdline/stream.h index ea9d52f6..08377adc 100644 --- a/cmdline/stream.h +++ b/cmdline/stream.h @@ -329,7 +329,7 @@ int sgetble32(STREAM* f, uint32_t* value); * Read a binary string. * Return -1 on error or if the buffer is too small, or the number of chars read. */ -int sgetbs(STREAM* f, char* str, size_t size); +ssize_t sgetbs(STREAM* f, char* str, size_t size); /****************************************************************************/ /* put */