|
20 | 20 | #include "common/jsonapi.h"
|
21 | 21 | #include "mb/pg_wchar.h"
|
22 | 22 |
|
23 |
| -#ifdef FRONTEND |
24 |
| -#include "common/logging.h" |
25 |
| -#else |
| 23 | +#ifndef FRONTEND |
26 | 24 | #include "miscadmin.h"
|
27 | 25 | #endif
|
28 | 26 |
|
29 |
| -#ifdef FRONTEND |
30 |
| -#define check_stack_depth() |
31 |
| -#define json_log_and_abort(...) \ |
32 |
| - do { pg_log_fatal(__VA_ARGS__); exit(1); } while(0) |
33 |
| -#else |
34 |
| -#define json_log_and_abort(...) elog(ERROR, __VA_ARGS__) |
35 |
| -#endif |
36 |
| - |
37 | 27 | /*
|
38 | 28 | * The context of the parser is maintained by the recursive descent
|
39 | 29 | * mechanism, but is passed explicitly to the error reporting routine
|
@@ -61,7 +51,6 @@ static JsonParseErrorType parse_object(JsonLexContext *lex, JsonSemAction *sem);
|
61 | 51 | static JsonParseErrorType parse_array_element(JsonLexContext *lex, JsonSemAction *sem);
|
62 | 52 | static JsonParseErrorType parse_array(JsonLexContext *lex, JsonSemAction *sem);
|
63 | 53 | static JsonParseErrorType report_parse_error(JsonParseContext ctx, JsonLexContext *lex);
|
64 |
| -static char *extract_token(JsonLexContext *lex); |
65 | 54 |
|
66 | 55 | /* the null action object used for pure validation */
|
67 | 56 | JsonSemAction nullSemAction =
|
@@ -378,7 +367,9 @@ parse_object(JsonLexContext *lex, JsonSemAction *sem)
|
378 | 367 | JsonTokenType tok;
|
379 | 368 | JsonParseErrorType result;
|
380 | 369 |
|
| 370 | +#ifndef FRONTEND |
381 | 371 | check_stack_depth();
|
| 372 | +#endif |
382 | 373 |
|
383 | 374 | if (ostart != NULL)
|
384 | 375 | (*ostart) (sem->semstate);
|
@@ -478,7 +469,9 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem)
|
478 | 469 | json_struct_action aend = sem->array_end;
|
479 | 470 | JsonParseErrorType result;
|
480 | 471 |
|
| 472 | +#ifndef FRONTEND |
481 | 473 | check_stack_depth();
|
| 474 | +#endif |
482 | 475 |
|
483 | 476 | if (astart != NULL)
|
484 | 477 | (*astart) (sem->semstate);
|
@@ -1044,15 +1037,34 @@ report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
|
1044 | 1037 |
|
1045 | 1038 | /*
|
1046 | 1039 | * We don't use a default: case, so that the compiler will warn about
|
1047 |
| - * unhandled enum values. But this needs to be here anyway to cover the |
1048 |
| - * possibility of an incorrect input. |
| 1040 | + * unhandled enum values. |
1049 | 1041 | */
|
1050 |
| - json_log_and_abort("unexpected json parse state: %d", (int) ctx); |
| 1042 | + Assert(false); |
1051 | 1043 | return JSON_SUCCESS; /* silence stupider compilers */
|
1052 | 1044 | }
|
1053 | 1045 |
|
| 1046 | + |
| 1047 | +#ifndef FRONTEND |
| 1048 | +/* |
| 1049 | + * Extract the current token from a lexing context, for error reporting. |
| 1050 | + */ |
| 1051 | +static char * |
| 1052 | +extract_token(JsonLexContext *lex) |
| 1053 | +{ |
| 1054 | + int toklen = lex->token_terminator - lex->token_start; |
| 1055 | + char *token = palloc(toklen + 1); |
| 1056 | + |
| 1057 | + memcpy(token, lex->token_start, toklen); |
| 1058 | + token[toklen] = '\0'; |
| 1059 | + return token; |
| 1060 | +} |
| 1061 | + |
1054 | 1062 | /*
|
1055 | 1063 | * Construct a detail message for a JSON error.
|
| 1064 | + * |
| 1065 | + * Note that the error message generated by this routine may not be |
| 1066 | + * palloc'd, making it unsafe for frontend code as there is no way to |
| 1067 | + * know if this can be safery pfree'd or not. |
1056 | 1068 | */
|
1057 | 1069 | char *
|
1058 | 1070 | json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
|
@@ -1115,20 +1127,7 @@ json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
|
1115 | 1127 | * unhandled enum values. But this needs to be here anyway to cover the
|
1116 | 1128 | * possibility of an incorrect input.
|
1117 | 1129 | */
|
1118 |
| - json_log_and_abort("unexpected json parse error type: %d", (int) error); |
1119 |
| - return NULL; /* silence stupider compilers */ |
1120 |
| -} |
1121 |
| - |
1122 |
| -/* |
1123 |
| - * Extract the current token from a lexing context, for error reporting. |
1124 |
| - */ |
1125 |
| -static char * |
1126 |
| -extract_token(JsonLexContext *lex) |
1127 |
| -{ |
1128 |
| - int toklen = lex->token_terminator - lex->token_start; |
1129 |
| - char *token = palloc(toklen + 1); |
1130 |
| - |
1131 |
| - memcpy(token, lex->token_start, toklen); |
1132 |
| - token[toklen] = '\0'; |
1133 |
| - return token; |
| 1130 | + elog(ERROR, "unexpected json parse error type: %d", (int) error); |
| 1131 | + return NULL; |
1134 | 1132 | }
|
| 1133 | +#endif |
0 commit comments