Skip to content

Commit

Permalink
Add logic to handle memory allocation failures.
Browse files Browse the repository at this point in the history
More generically, this patch adds handling of fatal errors.
Memory allocation failures are currently the only type of
fatal error.

- New C API config_set_fatal_error_func() allowing caller
  to provide a fatal error handler.
- A default handler will write a message to stderr and then
  call abort().
- The C++ library installs an internal handler that throws
  std::bad_alloc
- Route all malloc/calloc/realloc requests through new
  wrapper functions libconfig_malloc(), libconfig_calloc(),
  libconfig_realloc(), which call the above and then check
  the return value. If it's NULL, they call the fatal error handler.
- Force the flex- and bison-generated code to call the libconfig
  alloc functions instead of their default ones.
  • Loading branch information
hyperrealm committed Nov 22, 2022
1 parent 020a9ce commit 2c40b58
Show file tree
Hide file tree
Showing 37 changed files with 962 additions and 823 deletions.
98 changes: 81 additions & 17 deletions ac_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H

/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the <minix/config.h> header file. */
#undef HAVE_MINIX_CONFIG_H

/* Define to 1 if you have the `newlocale' function. */
#undef HAVE_NEWLOCALE

/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H

/* Define to 1 if you have the <stdio.h> header file. */
#undef HAVE_STDIO_H

/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H

Expand All @@ -39,6 +42,9 @@
/* Define to 1 if you have the `uselocale' function. */
#undef HAVE_USELOCALE

/* Define to 1 if you have the <wchar.h> header file. */
#undef HAVE_WCHAR_H

/* Define to 1 if you have the <xlocale.h> header file. */
#undef HAVE_XLOCALE_H

Expand Down Expand Up @@ -67,7 +73,9 @@
/* Define to the version of this package. */
#undef PACKAGE_VERSION

/* Define to 1 if you have the ANSI C header files. */
/* Define to 1 if all of the C90 standard headers exist (not just the ones
required in a freestanding environment). This macro is provided for
backward compatibility; new code need not use it. */
#undef STDC_HEADERS

/* Configured target name. */
Expand All @@ -77,21 +85,87 @@
#ifndef _ALL_SOURCE
# undef _ALL_SOURCE
#endif
/* Enable general extensions on macOS. */
#ifndef _DARWIN_C_SOURCE
# undef _DARWIN_C_SOURCE
#endif
/* Enable general extensions on Solaris. */
#ifndef __EXTENSIONS__
# undef __EXTENSIONS__
#endif
/* Enable GNU extensions on systems that have them. */
#ifndef _GNU_SOURCE
# undef _GNU_SOURCE
#endif
/* Enable threading extensions on Solaris. */
/* Enable X/Open compliant socket functions that do not require linking
with -lxnet on HP-UX 11.11. */
#ifndef _HPUX_ALT_XOPEN_SOCKET_API
# undef _HPUX_ALT_XOPEN_SOCKET_API
#endif
/* Identify the host operating system as Minix.
This macro does not affect the system headers' behavior.
A future release of Autoconf may stop defining this macro. */
#ifndef _MINIX
# undef _MINIX
#endif
/* Enable general extensions on NetBSD.
Enable NetBSD compatibility extensions on Minix. */
#ifndef _NETBSD_SOURCE
# undef _NETBSD_SOURCE
#endif
/* Enable OpenBSD compatibility extensions on NetBSD.
Oddly enough, this does nothing on OpenBSD. */
#ifndef _OPENBSD_SOURCE
# undef _OPENBSD_SOURCE
#endif
/* Define to 1 if needed for POSIX-compatible behavior. */
#ifndef _POSIX_SOURCE
# undef _POSIX_SOURCE
#endif
/* Define to 2 if needed for POSIX-compatible behavior. */
#ifndef _POSIX_1_SOURCE
# undef _POSIX_1_SOURCE
#endif
/* Enable POSIX-compatible threading on Solaris. */
#ifndef _POSIX_PTHREAD_SEMANTICS
# undef _POSIX_PTHREAD_SEMANTICS
#endif
/* Enable extensions specified by ISO/IEC TS 18661-5:2014. */
#ifndef __STDC_WANT_IEC_60559_ATTRIBS_EXT__
# undef __STDC_WANT_IEC_60559_ATTRIBS_EXT__
#endif
/* Enable extensions specified by ISO/IEC TS 18661-1:2014. */
#ifndef __STDC_WANT_IEC_60559_BFP_EXT__
# undef __STDC_WANT_IEC_60559_BFP_EXT__
#endif
/* Enable extensions specified by ISO/IEC TS 18661-2:2015. */
#ifndef __STDC_WANT_IEC_60559_DFP_EXT__
# undef __STDC_WANT_IEC_60559_DFP_EXT__
#endif
/* Enable extensions specified by ISO/IEC TS 18661-4:2015. */
#ifndef __STDC_WANT_IEC_60559_FUNCS_EXT__
# undef __STDC_WANT_IEC_60559_FUNCS_EXT__
#endif
/* Enable extensions specified by ISO/IEC TS 18661-3:2015. */
#ifndef __STDC_WANT_IEC_60559_TYPES_EXT__
# undef __STDC_WANT_IEC_60559_TYPES_EXT__
#endif
/* Enable extensions specified by ISO/IEC TR 24731-2:2010. */
#ifndef __STDC_WANT_LIB_EXT2__
# undef __STDC_WANT_LIB_EXT2__
#endif
/* Enable extensions specified by ISO/IEC 24747:2009. */
#ifndef __STDC_WANT_MATH_SPEC_FUNCS__
# undef __STDC_WANT_MATH_SPEC_FUNCS__
#endif
/* Enable extensions on HP NonStop. */
#ifndef _TANDEM_SOURCE
# undef _TANDEM_SOURCE
#endif
/* Enable general extensions on Solaris. */
#ifndef __EXTENSIONS__
# undef __EXTENSIONS__
/* Enable X/Open extensions. Define to 500 only if necessary
to make mbstate_t available. */
#ifndef _XOPEN_SOURCE
# undef _XOPEN_SOURCE
#endif


Expand All @@ -102,15 +176,5 @@
`char[]'. */
#undef YYTEXT_POINTER

/* Define to 1 if on MINIX. */
#undef _MINIX

/* Define to 2 if the system does not provide POSIX.1 features except with
this defined. */
#undef _POSIX_1_SOURCE

/* Define to 1 if you need to in order for `stat' and other things to work. */
#undef _POSIX_SOURCE

/* Define to empty if `const' does not conform to ANSI C. */
#undef const
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dnl Process this file with autoconf to produce a configure script.

AC_INIT(libconfig, 1.7.3, [email protected], libconfig,
AC_INIT(libconfig, 1.7.4, [email protected], libconfig,
[https://hyperrealm.github.io/libconfig/])
AC_CONFIG_AUX_DIR([aux-build])
AC_CONFIG_MACRO_DIR([m4])
Expand Down
2 changes: 1 addition & 1 deletion debian/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Upstream Author: Mark A Lindner
Jose Luis Tallon - Enhancements, bugfixes


Copyright: Copyright (C) 2005-2015 Mark A Lindner
Copyright: Copyright (C) 2005-2023 Mark A Lindner

License: LGPL-2.1

Expand Down
46 changes: 41 additions & 5 deletions doc/libconfig.texi
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@setfilename libconfig.info
@settitle libconfig

@set edition 1.7.3
@set update-date 20 Jun 2021
@set edition 1.7.4
@set update-date 22 Nov 2022
@set subtitle-text A Library For Processing Structured Configuration Files
@set author-text Mark A.@: Lindner

Expand Down Expand Up @@ -38,7 +38,7 @@

@page
@vskip 0pt plus 1filll
Copyright @copyright{} 2004-2021 Mark A Lindner
Copyright @copyright{} 2004-2023 Mark A Lindner

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
Expand Down Expand Up @@ -800,6 +800,42 @@ and a parsing error, respectively.

@end deftypefun

@deftypefun void config_set_fatal_error_func (@w{config_fatal_error_fn_t @var{func}})

@b{Since @i{v1.7.4}}

@cindex fatal error
Specifies the function @var{func} to call when a fatal error is encountered.
If @var{func} is @code{NULL}, the default fatal error handler function will
be reinstated.

@tindex config_fatal_error_fn_t
The type @i{config_fatal_error_fn_t} is a type alias
for a function whose signature is:

@deftypefun @w{void} func (@w{const char *@var{message}})

The function receives an error message @var{message}. The function is
not expected to return to the caller; if it does, the resulting
behavior is undefined.

@end deftypefun

Fatal errors are unrecoverable, and the only reasonable course of
action is to abort the calling process. The default fatal error
handler function writes a message to standard error and then calls
@code{abort()}. One potential alternate implementation would be to
call @code{exit()} with an exit status that indicates to the parent
process (such as a watchdog process) that the current process has
encountered an unrecoverable condition and should be respawned.

In the current implementation, the only condition that will produce a
fatal error is a memory allocation failure---that is, a @code{NULL}
return value from @code{malloc()}, @code{calloc()}, or
@code{realloc()}.

@end deftypefun

@deftypefun void config_set_include_dir (@w{config_t *@var{config}}, @w{const char *@var{include_dir}})
@deftypefunx {const char *} config_get_include_dir (@w{const config_t *@var{config}})

Expand Down Expand Up @@ -879,8 +915,8 @@ implementations of such include functions are not included.

@end deftypefun

@deftypefun {unsigned short} config_get_float_precision(@w{config_t *@var{config}})
@deftypefunx void config_set_float_precision(@w{config_t *@var{config}}, @w{unsigned short @var{digits}})
@deftypefun {unsigned short} config_get_float_precision (@w{config_t *@var{config}})
@deftypefunx void config_set_float_precision (@w{config_t *@var{config}}, @w{unsigned short @var{digits}})

@b{Since @i{v1.6}}

Expand Down
2 changes: 1 addition & 1 deletion examples/c++/example1.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2010 Mark A Lindner
Copyright (C) 2005-2023 Mark A Lindner
This file is part of libconfig.
Expand Down
2 changes: 1 addition & 1 deletion examples/c++/example2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2010 Mark A Lindner
Copyright (C) 2005-2023 Mark A Lindner
This file is part of libconfig.
Expand Down
2 changes: 1 addition & 1 deletion examples/c++/example3.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2010 Mark A Lindner
Copyright (C) 2005-2023 Mark A Lindner
This file is part of libconfig.
Expand Down
2 changes: 1 addition & 1 deletion examples/c++/example4.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2010 Mark A Lindner
Copyright (C) 2005-2023 Mark A Lindner
This file is part of libconfig.
Expand Down
2 changes: 1 addition & 1 deletion examples/c/example1.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2018 Mark A Lindner
Copyright (C) 2005-2023 Mark A Lindner
This file is part of libconfig.
Expand Down
2 changes: 1 addition & 1 deletion examples/c/example2.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2018 Mark A Lindner
Copyright (C) 2005-2023 Mark A Lindner
This file is part of libconfig.
Expand Down
2 changes: 1 addition & 1 deletion examples/c/example3.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2018 Mark A Lindner
Copyright (C) 2005-2023 Mark A Lindner
This file is part of libconfig.
Expand Down
2 changes: 1 addition & 1 deletion examples/c/example4.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2018 Mark A Lindner
Copyright (C) 2005-2023 Mark A Lindner
This file is part of libconfig.
Expand Down
2 changes: 1 addition & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
# For more info see section 6.3 of the GNU Libtool Manual.

VERINFO = -version-info 12:0:1
VERINFO = -version-info 12:1:1

## Flex
PARSER_PREFIX = libconfig_yy
Expand Down
Loading

0 comments on commit 2c40b58

Please sign in to comment.