Check for failed malloc

...thereby silencing

> In file included from /usr/include/features.h:490,
>                  from /usr/include/bits/libc-header-start.h:33,
>                  from /usr/include/stdio.h:27,
>                  from soltools/mkdepend/def.h:40,
>                  from soltools/mkdepend/main.c:58:
> In function ‘read’,
>     inlined from ‘main’ at soltools/mkdepend/main.c:197:28:
> /usr/include/bits/unistd.h:38:10: error: ‘__read_alias’ specified size 18446744073709551614 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,
>       |                ^~~~~~~~~~

seen at least with -Wp,-D_FORTIFY_SOURCE=3 manually added to gb_COMPILEROPTFLAGS
in an --enable-optimized build against recent GCC 13 trunk

Change-Id: I9ca3c0ea8c579fffbdad52d7d39a4ce82085ddcd
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143760
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
This commit is contained in:
Stephan Bergmann 2022-12-07 07:43:23 +01:00
parent e0bf2712aa
commit cb26710b6b

View file

@ -77,6 +77,7 @@ typedef _W64 int ssize_t;
#endif #endif
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h>
#ifdef MINIX #ifdef MINIX
#define USE_CHMOD 1 #define USE_CHMOD 1
@ -194,6 +195,9 @@ int main(int argc, char **argv)
fatalerr("cannot open \"%s\"\n", argv[1]+1); fatalerr("cannot open \"%s\"\n", argv[1]+1);
(void)fstat(afd, &ast); (void)fstat(afd, &ast);
args = (char *)malloc(ast.st_size + 1); args = (char *)malloc(ast.st_size + 1);
if (args == NULL) {
abort();
}
if ((ast.st_size = read(afd, args, (size_t) ast.st_size)) < 0) if ((ast.st_size = read(afd, args, (size_t) ast.st_size)) < 0)
fatalerr("failed to read %s\n", argv[1]+1); fatalerr("failed to read %s\n", argv[1]+1);
args[ast.st_size] = '\0'; args[ast.st_size] = '\0';