From 72860bfdca5286399837080d53ba297bf72c56b3 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Sun, 16 Oct 2022 18:02:46 +0200 Subject: [PATCH] tests: Check lseek, read and malloc results with correct types in test. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When compiling dwfl-report-offline-memory.c on some systems (latest gcc/glibc and --enable-sanitize-undefined) we might get: In file included from /usr/include/features.h:490, from /usr/include/assert.h:35, from dwfl-report-offline-memory.c:18: In function ‘read’, inlined from ‘main’ at dwfl-report-offline-memory.c:68:23: /usr/include/bits/unistd.h:38:10: error: ‘__read_alias’ specified size 18446744073709551615 exceeds maximum object size 9223372036854775807 [-Werror=stringop-overflow=] 38 | return __glibc_fortify (read, __nbytes, sizeof (char), | ^~~~~~~~~~~~~~~ /usr/include/bits/unistd.h: In function ‘main’: /usr/include/bits/unistd.h:26:16: note: in a call to function ‘__read_alias’ declared with attribute ‘access (write_only, 2, 3)’ 26 | extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf, | ^~~~~~~~~~ cc1: all warnings being treated as errors make[2]: *** [Makefile:2461: dwfl-report-offline-memory.o] Error 1 Fix by using the correct types and checking all return values. Signed-off-by: Mark Wielaard --- tests/ChangeLog | 5 +++++ tests/dwfl-report-offline-memory.c | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/ChangeLog b/tests/ChangeLog index 6ac2c1e8..d07a910e 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,8 @@ +2022-10-16 Mark Wielaard + + * dwfl-report-offline-memory.c (main): Check lseek, read and malloc + results with correct types. + 2022-09-13 Aleksei Vetrov * Makefile.am (check_PROGRAMS): Add dwfl-report-offline-memory. diff --git a/tests/dwfl-report-offline-memory.c b/tests/dwfl-report-offline-memory.c index 837aca5e..81fa136f 100644 --- a/tests/dwfl-report-offline-memory.c +++ b/tests/dwfl-report-offline-memory.c @@ -62,10 +62,14 @@ main (int argc, char **argv) int fd = open (fname, O_RDONLY); if (fd < 0) error (-1, 0, "can't open file %s: %s", fname, strerror (errno)); - size_t size = lseek (fd, 0, SEEK_END); + off_t size = lseek (fd, 0, SEEK_END); + if (size < 0) + error (-1, 0, "can't lseek file %s: %s", fname, strerror (errno)); lseek (fd, 0, SEEK_SET); char *data = malloc (size); - size_t bytes_read = read (fd, data, size); + if (data == NULL) + error (-1, 0, "can't malloc: %s", strerror (errno)); + ssize_t bytes_read = read (fd, data, size); assert (bytes_read == size); close (fd); -- 2.30.2