Skip to content

Commit

Permalink
libcomposefs/lcfs-writer.c: fix build without reallocarray
Browse files Browse the repository at this point in the history
If reallocarray is not available (e.g. on uclibc), use realloc to avoid
the following build failure with uclibc:

composefs/libcomposefs/lcfs-writer.c: In function 'lcfs_node_add_child':
composefs/libcomposefs/lcfs-writer.c:886:24: error: implicit declaration of function 'reallocarray' [-Werror=implicit-function-declaration]
  886 |         new_children = reallocarray(parent->children, sizeof(*parent->children),
      |                        ^~~~~~~~~~~~
composefs/libcomposefs/lcfs-writer.c:886:22: error: assignment to 'struct lcfs_node_s **' from 'int' makes pointer from integer without a cast [-Werror=int-conversion]
  886 |         new_children = reallocarray(parent->children, sizeof(*parent->children),
      |                      ^

Fixes:
 - http://autobuild.buildroot.org/results/38470213c86091d14c59716cddcccd555f875705

Signed-off-by: Fabrice Fontaine <[email protected]>
  • Loading branch information
ffontaine authored and alexlarsson committed Dec 11, 2023
1 parent 9a9cd59 commit 3358d54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ AC_FUNC_ERROR_AT_LINE
AC_FUNC_FSEEKO
AC_HEADER_MAJOR
AC_FUNC_MMAP
AC_CHECK_FUNCS([getcwd memset munmap strdup])
AC_CHECK_FUNCS([getcwd memset munmap reallocarray strdup])

AC_SUBST(PKGCONFIG_REQUIRES)
AC_SUBST(PKGCONFIG_REQUIRES_PRIVATELY)
Expand Down
15 changes: 15 additions & 0 deletions libcomposefs/lcfs-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ static inline char *memdup(const char *s, size_t len)
return s2;
}

static inline bool size_multiply_overflow(size_t size, size_t nmemb)
{
return nmemb != 0 && size > (SIZE_MAX / nmemb);
}

#ifndef HAVE_REALLOCARRAY
static inline void *reallocarray(void *ptr, size_t nmemb, size_t size)
{
if (size_multiply_overflow(size, nmemb))
return NULL;

return realloc(ptr, size * nmemb ?: 1);
}
#endif

static inline int hexdigit(char c)
{
if (c >= '0' && c <= '9')
Expand Down

0 comments on commit 3358d54

Please sign in to comment.