9 Commits

Author SHA1 Message Date
Rosen Penev
66f7869219 rename WIN32 to _WIN32
The latter is the proper macro defined by Windows headers.

Fixes compilation under at least clang-cl which mandates function
declarations.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2024-02-07 18:06:55 -08:00
Tobias Stoeckmann
5c72257220 vasprintf(): avoid out of memory accesses
Systems without vasprintf fall back to implementation in header file
vasprintf_compat.h. This version could run into heap overflow issues
with very long arguments or formats provoking a lot of output.

The vsnprintf function returns a negative value if more than INT_MAX
characters would be written since its int return type could not
handle this (and %n couldn't handle it either).

Before testing for a possible error value the additional char for
\0 is already added. A -1 error code would not be detected.

Increment only after implicitly casting to an unsigned value to avoid
signed integer overflow if INT_MAX has been returned.

Use va_copy to duplicate the original ap argument for multiple uses
on non-WIN32 systems. At least with glibc the test suite would fail
because the arguments are not reset after leaving the vsnprintf call.

Removed support for apparently very old glibc versions which do not
comply with vsnprintf standard descriptions. It breaks support for
modern ones which are not forced to return -1 in case of error. The
standard specifies merely "a negative value".

How to reproduce:

- Use a system without vasprintf
- Alternatively remove -D_GNU_SOURCE from CMakeLists.txt
- Compile and run:

  #include "json.h"
  int main(void) {
    struct printbuf *pb = printbuf_new();
    sprintbuf(pb, "prefix %2147483647s", "*");
    printbuf_free(pb);
    return 0;
  }
2022-02-24 23:06:57 +01:00
Eric Haszlakiewicz
f357081b57 Not all systems (e.g. osx) have malloc.h, include stdlib.h instead. 2020-04-08 02:53:48 +00:00
Eric Haszlakiewicz
19bbf2c069 Add includes and split off json_types.h to help ensure that headers can be included in any order. 2020-04-06 13:55:27 +00:00
dota17
8b162c4b89 clang-format the files 2020-04-03 11:39:30 +08:00
dota17
c117d8a8a8 add the disabling formatting coments and adjust the partial code manuly 2020-04-03 11:28:04 +08:00
Eric Haszlakiewicz
f2f103b986 Add a brief overview of each file to the docs. 2017-12-06 00:20:59 -05:00
Eric Haszlakiewicz
81f6edbfd5 PR#331: for Visual Studio, use a snprintf/vsnprintf wrapper that ensures the string is terminated. 2017-08-25 01:15:39 -04:00
Alexandru Ardelean
47f32a76ef vasprintf_compat.h: spin-off this compat header ; use math_compat.h as template
Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2016-11-22 16:37:30 +02:00