Skip to content

Commit a474017

Browse files
committed
Support a max depth to protect against malicious payloads
1 parent 92ad483 commit a474017

File tree

4 files changed

+312
-269
lines changed

4 files changed

+312
-269
lines changed

config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ errors:
185185
- MODULE_TERM
186186
- MULTI_ASSIGN_MULTI_SPLATS
187187
- MULTI_ASSIGN_UNEXPECTED_REST
188+
- NESTING_TOO_DEEP
188189
- NO_LOCAL_VARIABLE
189190
- NOT_EXPRESSION
190191
- NUMBER_LITERAL_UNDERSCORE

include/prism/defines.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
#define __STDC_FORMAT_MACROS
2626
#include <inttypes.h>
2727

28+
/**
29+
* When we are parsing using recursive descent, we want to protect against
30+
* malicious payloads that could attempt to crash our parser. We do this by
31+
* specifying a maximum depth to which we are allowed to recurse.
32+
*/
33+
#ifndef PRISM_DEPTH_MAXIMUM
34+
#define PRISM_DEPTH_MAXIMUM 1000
35+
#endif
36+
2837
/**
2938
* By default, we compile with -fvisibility=hidden. When this is enabled, we
3039
* need to mark certain functions as being publically-visible. This macro does
@@ -212,4 +221,28 @@
212221
#define PRISM_ENCODING_EXCLUDE_FULL
213222
#endif
214223

224+
/**
225+
* Support PRISM_LIKELY and PRISM_UNLIKELY to help the compiler optimize its
226+
* branch predication.
227+
*/
228+
#if defined(__GNUC__) || defined(__clang__)
229+
/** The compiler should predicate that this branch will be taken. */
230+
#define PRISM_LIKELY(x) __builtin_expect(!!(x), 1)
231+
232+
/** The compiler should predicate that this branch will not be taken. */
233+
#define PRISM_UNLIKELY(x) __builtin_expect(!!(x), 0)
234+
#elif defined(_MSC_VER) && (_MSC_VER >= 1400)
235+
/** The compiler should predicate that this branch will be taken. */
236+
#define PRISM_LIKELY(x) __assume((x))
237+
238+
/** The compiler should predicate that this branch will not be taken. */
239+
#define PRISM_UNLIKELY(x) __assume(!(x))
240+
#else
241+
/** Void because this platform does not support branch prediction hints. */
242+
#define PRISM_LIKELY(x) (x)
243+
244+
/** Void because this platform does not support branch prediction hints. */
245+
#define PRISM_UNLIKELY(x) (x)
246+
#endif
247+
215248
#endif

0 commit comments

Comments
 (0)