Skip to content

Commit 1145943

Browse files
committed
Added parser depth counter and removed invalid noexcept
1 parent 94c082c commit 1145943

File tree

5 files changed

+143
-46
lines changed

5 files changed

+143
-46
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ set(JSONEXPR_USE_SYSTEM_EXPECTED OFF CACHE BOOL "Use a pre-installed version of
2828
set(JSONEXPR_USE_STD_EXPECTED OFF CACHE BOOL "Use std::expected (requires C++23). Else, use tl::expected.")
2929
set(JSONEXPR_USE_STD_FROM_CHARS ON CACHE BOOL "Use std::from_chars (requires C++17). Else, use streams.")
3030

31+
# Configurable parameters
32+
set(JSONEXPR_MAX_AST_DEPTH 32 CACHE STRING "Maximum depth of the parsed AST (0=infinite)")
33+
3134
# Development options.
3235
set(JSONEXPR_DEV OFF CACHE BOOL "Enable warnings in compilation.")
3336

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [Error handling](#error-handling-1)
2121
- [Overloading](#overloading)
2222
- [AST functions \(advanced\)](#ast-functions-advanced)
23+
- [Security](#security)
2324
- [Acknowledgments](#acknowledgments)
2425

2526
<!-- /MarkdownTOC -->
@@ -327,6 +328,19 @@ first_non_null(1, 1+'abc') -> 1 (second argument was invalid, but no error si
327328
```
328329

329330

331+
# Security
332+
333+
All operations allowed in the language are meant to be safe, in the sense that they should not make the host process abort or behave in an unspecified manner (e.g., through out-of-bounds read or writes, use-after-free, incorrect type accesses, read of uninitialized memory, etc.). This is tested by running the test suite with sanitizers, and by fuzzing.
334+
335+
Furthermore, the parser has a fixed maximum recursion depth to prevent stack overflows. This depth is set to 32 by default, and can be changed with the CMake option `JSONEXPR_MAX_AST_DEPTH`.
336+
337+
Despite the above, the library is not 100% risk-free. In particular, the following is currently unsafe:
338+
- integer overflow and underflow in evaluated expression
339+
340+
The following would trigger an exception (or abort the process if exceptions are disabled):
341+
- running out of heap memory while parsing or evaluating an expression
342+
343+
330344
# Acknowledgments
331345

332346
This library was written partly on my spare time, and partly during the course of my employment at [IBEX Innovations Ltd.](https://ibexinnovations.co.uk/). I would like to thank my employer for allowing me to open-source this library, with the hope that it is useful to others.

libjsonexpr/include/jsonexpr/config.hpp.config

+4
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@
4141
# define JSONEXPR_EXPORT
4242
#endif
4343

44+
#cmakedefine01 JSONEXPR_FUZZ
45+
46+
#define JSONEXPR_MAX_AST_DEPTH ${JSONEXPR_MAX_AST_DEPTH}
47+
4448
#endif

libjsonexpr/include/jsonexpr/parse.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "jsonexpr/expected.hpp"
66

77
namespace jsonexpr {
8-
JSONEXPR_EXPORT expected<ast::node, error> parse(std::string_view expression) noexcept;
8+
JSONEXPR_EXPORT expected<ast::node, error> parse(std::string_view expression);
99
} // namespace jsonexpr
1010

1111
#endif

0 commit comments

Comments
 (0)