Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 15cd9a3

Browse filesBrowse files
committed
jsonapi: Use const char *
Apply const qualifiers to char * arguments and fields throughout the jsonapi. This allows the top-level APIs such as pg_parse_json_incremental() to declare their input argument as const. It also reduces the number of unconstify() calls. Reviewed-by: Andrew Dunstan <andrew@dunslane.net> Discussion: https://www.postgresql.org/message-id/flat/f732b014-f614-4600-a437-dba5a2c3738b%40eisentraut.org
1 parent 0b06bf9 commit 15cd9a3
Copy full SHA for 15cd9a3

File tree

Expand file treeCollapse file tree

3 files changed

+41
-38
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+41
-38
lines changed

‎src/backend/utils/adt/jsonfuncs.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/jsonfuncs.c
+21-18Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typedef struct GetState
8686
{
8787
JsonLexContext *lex;
8888
text *tresult;
89-
char *result_start;
89+
const char *result_start;
9090
bool normalize_results;
9191
bool next_scalar;
9292
int npath; /* length of each path-related array */
@@ -111,7 +111,7 @@ typedef struct EachState
111111
Tuplestorestate *tuple_store;
112112
TupleDesc ret_tdesc;
113113
MemoryContext tmp_cxt;
114-
char *result_start;
114+
const char *result_start;
115115
bool normalize_results;
116116
bool next_scalar;
117117
char *normalized_scalar;
@@ -125,7 +125,7 @@ typedef struct ElementsState
125125
Tuplestorestate *tuple_store;
126126
TupleDesc ret_tdesc;
127127
MemoryContext tmp_cxt;
128-
char *result_start;
128+
const char *result_start;
129129
bool normalize_results;
130130
bool next_scalar;
131131
char *normalized_scalar;
@@ -138,7 +138,7 @@ typedef struct JHashState
138138
const char *function_name;
139139
HTAB *hash;
140140
char *saved_scalar;
141-
char *save_json_start;
141+
const char *save_json_start;
142142
JsonTokenType saved_token_type;
143143
} JHashState;
144144

@@ -247,7 +247,7 @@ typedef struct PopulateRecordsetState
247247
const char *function_name;
248248
HTAB *json_hash;
249249
char *saved_scalar;
250-
char *save_json_start;
250+
const char *save_json_start;
251251
JsonTokenType saved_token_type;
252252
Tuplestorestate *tuple_store;
253253
HeapTupleHeader rec;
@@ -273,7 +273,7 @@ typedef struct PopulateArrayState
273273
{
274274
JsonLexContext *lex; /* json lexer */
275275
PopulateArrayContext *ctx; /* context */
276-
char *element_start; /* start of the current array element */
276+
const char *element_start; /* start of the current array element */
277277
char *element_scalar; /* current array element token if it is a
278278
* scalar */
279279
JsonTokenType element_type; /* current array element type */
@@ -295,7 +295,7 @@ typedef struct JsValue
295295
{
296296
struct
297297
{
298-
char *str; /* json string */
298+
const char *str; /* json string */
299299
int len; /* json string length or -1 if null-terminated */
300300
JsonTokenType type; /* json type */
301301
} json; /* json value */
@@ -390,7 +390,7 @@ static JsonParseErrorType elements_array_element_end(void *state, bool isnull);
390390
static JsonParseErrorType elements_scalar(void *state, char *token, JsonTokenType tokentype);
391391

392392
/* turn a json object into a hash table */
393-
static HTAB *get_json_object_as_hash(char *json, int len, const char *funcname,
393+
static HTAB *get_json_object_as_hash(const char *json, int len, const char *funcname,
394394
Node *escontext);
395395

396396
/* semantic actions for populate_array_json */
@@ -456,7 +456,7 @@ static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod,
456456
static RecordIOData *allocate_record_info(MemoryContext mcxt, int ncolumns);
457457
static bool JsObjectGetField(JsObject *obj, char *field, JsValue *jsv);
458458
static void populate_recordset_record(PopulateRecordsetState *state, JsObject *obj);
459-
static bool populate_array_json(PopulateArrayContext *ctx, char *json, int len);
459+
static bool populate_array_json(PopulateArrayContext *ctx, const char *json, int len);
460460
static bool populate_array_dim_jsonb(PopulateArrayContext *ctx, JsonbValue *jbv,
461461
int ndim);
462462
static void populate_array_report_expected_array(PopulateArrayContext *ctx, int ndim);
@@ -1181,7 +1181,7 @@ get_object_end(void *state)
11811181
if (lex_level == 0 && _state->npath == 0)
11821182
{
11831183
/* Special case: return the entire object */
1184-
char *start = _state->result_start;
1184+
const char *start = _state->result_start;
11851185
int len = _state->lex->prev_token_terminator - start;
11861186

11871187
_state->tresult = cstring_to_text_with_len(start, len);
@@ -1275,7 +1275,7 @@ get_object_field_end(void *state, char *fname, bool isnull)
12751275
_state->tresult = (text *) NULL;
12761276
else
12771277
{
1278-
char *start = _state->result_start;
1278+
const char *start = _state->result_start;
12791279
int len = _state->lex->prev_token_terminator - start;
12801280

12811281
_state->tresult = cstring_to_text_with_len(start, len);
@@ -1337,7 +1337,7 @@ get_array_end(void *state)
13371337
if (lex_level == 0 && _state->npath == 0)
13381338
{
13391339
/* Special case: return the entire array */
1340-
char *start = _state->result_start;
1340+
const char *start = _state->result_start;
13411341
int len = _state->lex->prev_token_terminator - start;
13421342

13431343
_state->tresult = cstring_to_text_with_len(start, len);
@@ -1426,7 +1426,7 @@ get_array_element_end(void *state, bool isnull)
14261426
_state->tresult = (text *) NULL;
14271427
else
14281428
{
1429-
char *start = _state->result_start;
1429+
const char *start = _state->result_start;
14301430
int len = _state->lex->prev_token_terminator - start;
14311431

14321432
_state->tresult = cstring_to_text_with_len(start, len);
@@ -1463,7 +1463,7 @@ get_scalar(void *state, char *token, JsonTokenType tokentype)
14631463
* scalar token, but not whitespace before it. Probably not worth
14641464
* doing our own space-skipping to avoid that.
14651465
*/
1466-
char *start = _state->lex->input;
1466+
const char *start = _state->lex->input;
14671467
int len = _state->lex->prev_token_terminator - start;
14681468

14691469
_state->tresult = cstring_to_text_with_len(start, len);
@@ -2782,7 +2782,7 @@ populate_array_scalar(void *_state, char *token, JsonTokenType tokentype)
27822782
* Returns false if an error occurs when parsing.
27832783
*/
27842784
static bool
2785-
populate_array_json(PopulateArrayContext *ctx, char *json, int len)
2785+
populate_array_json(PopulateArrayContext *ctx, const char *json, int len)
27862786
{
27872787
PopulateArrayState state;
27882788
JsonSemAction sem;
@@ -3123,7 +3123,7 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv,
31233123
{
31243124
Datum res;
31253125
char *str = NULL;
3126-
char *json = NULL;
3126+
const char *json = NULL;
31273127

31283128
if (jsv->is_json)
31293129
{
@@ -3139,7 +3139,10 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv,
31393139
str[len] = '\0';
31403140
}
31413141
else
3142-
str = json; /* string is already null-terminated */
3142+
{
3143+
/* string is already null-terminated */
3144+
str = unconstify(char *, json);
3145+
}
31433146

31443147
/* If converting to json/jsonb, make string into valid JSON literal */
31453148
if ((typid == JSONOID || typid == JSONBOID) &&
@@ -3784,7 +3787,7 @@ populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
37843787
* Returns the hash table if the json is parsed successfully, NULL otherwise.
37853788
*/
37863789
static HTAB *
3787-
get_json_object_as_hash(char *json, int len, const char *funcname,
3790+
get_json_object_as_hash(const char *json, int len, const char *funcname,
37883791
Node *escontext)
37893792
{
37903793
HASHCTL ctl;

‎src/common/jsonapi.c

Copy file name to clipboardExpand all lines: src/common/jsonapi.c
+13-13Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static td_entry td_parser_table[JSON_NUM_NONTERMINALS][JSON_NUM_TERMINALS] =
211211
static char JSON_PROD_GOAL[] = {JSON_TOKEN_END, JSON_NT_JSON, 0};
212212

213213
static inline JsonParseErrorType json_lex_string(JsonLexContext *lex);
214-
static inline JsonParseErrorType json_lex_number(JsonLexContext *lex, char *s,
214+
static inline JsonParseErrorType json_lex_number(JsonLexContext *lex, const char *s,
215215
bool *num_err, size_t *total_len);
216216
static inline JsonParseErrorType parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
217217
static JsonParseErrorType parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
@@ -290,12 +290,12 @@ IsValidJsonNumber(const char *str, size_t len)
290290
*/
291291
if (*str == '-')
292292
{
293-
dummy_lex.input = unconstify(char *, str) + 1;
293+
dummy_lex.input = str + 1;
294294
dummy_lex.input_length = len - 1;
295295
}
296296
else
297297
{
298-
dummy_lex.input = unconstify(char *, str);
298+
dummy_lex.input = str;
299299
dummy_lex.input_length = len;
300300
}
301301

@@ -323,7 +323,7 @@ IsValidJsonNumber(const char *str, size_t len)
323323
* cleanup.
324324
*/
325325
JsonLexContext *
326-
makeJsonLexContextCstringLen(JsonLexContext *lex, char *json,
326+
makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json,
327327
size_t len, int encoding, bool need_escapes)
328328
{
329329
if (lex == NULL)
@@ -649,7 +649,7 @@ json_count_array_elements(JsonLexContext *lex, int *elements)
649649
JsonParseErrorType
650650
pg_parse_json_incremental(JsonLexContext *lex,
651651
JsonSemAction *sem,
652-
char *json,
652+
const char *json,
653653
size_t len,
654654
bool is_last)
655655
{
@@ -1308,8 +1308,8 @@ parse_array(JsonLexContext *lex, JsonSemAction *sem)
13081308
JsonParseErrorType
13091309
json_lex(JsonLexContext *lex)
13101310
{
1311-
char *s;
1312-
char *const end = lex->input + lex->input_length;
1311+
const char *s;
1312+
const char *const end = lex->input + lex->input_length;
13131313
JsonParseErrorType result;
13141314

13151315
if (lex->incremental && lex->inc_state->partial_completed)
@@ -1593,7 +1593,7 @@ json_lex(JsonLexContext *lex)
15931593
break;
15941594
default:
15951595
{
1596-
char *p;
1596+
const char *p;
15971597

15981598
/*
15991599
* We're not dealing with a string, number, legal
@@ -1671,8 +1671,8 @@ json_lex(JsonLexContext *lex)
16711671
static inline JsonParseErrorType
16721672
json_lex_string(JsonLexContext *lex)
16731673
{
1674-
char *s;
1675-
char *const end = lex->input + lex->input_length;
1674+
const char *s;
1675+
const char *const end = lex->input + lex->input_length;
16761676
int hi_surrogate = -1;
16771677

16781678
/* Convenience macros for error exits */
@@ -1689,7 +1689,7 @@ json_lex_string(JsonLexContext *lex)
16891689
} while (0)
16901690
#define FAIL_AT_CHAR_END(code) \
16911691
do { \
1692-
char *term = s + pg_encoding_mblen(lex->input_encoding, s); \
1692+
const char *term = s + pg_encoding_mblen(lex->input_encoding, s); \
16931693
lex->token_terminator = (term <= end) ? term : end; \
16941694
return code; \
16951695
} while (0)
@@ -1854,7 +1854,7 @@ json_lex_string(JsonLexContext *lex)
18541854
}
18551855
else
18561856
{
1857-
char *p = s;
1857+
const char *p = s;
18581858

18591859
if (hi_surrogate != -1)
18601860
FAIL_AT_CHAR_END(JSON_UNICODE_LOW_SURROGATE);
@@ -1940,7 +1940,7 @@ json_lex_string(JsonLexContext *lex)
19401940
* the distance from lex->input to the token end+1 is returned to *total_len.
19411941
*/
19421942
static inline JsonParseErrorType
1943-
json_lex_number(JsonLexContext *lex, char *s,
1943+
json_lex_number(JsonLexContext *lex, const char *s,
19441944
bool *num_err, size_t *total_len)
19451945
{
19461946
bool error = false;

‎src/include/common/jsonapi.h

Copy file name to clipboardExpand all lines: src/include/common/jsonapi.h
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ typedef struct JsonIncrementalState JsonIncrementalState;
8888
#define JSONLEX_FREE_STRVAL (1 << 1)
8989
typedef struct JsonLexContext
9090
{
91-
char *input;
91+
const char *input;
9292
size_t input_length;
9393
int input_encoding;
94-
char *token_start;
95-
char *token_terminator;
96-
char *prev_token_terminator;
94+
const char *token_start;
95+
const char *token_terminator;
96+
const char *prev_token_terminator;
9797
bool incremental;
9898
JsonTokenType token_type;
9999
int lex_level;
100100
bits32 flags;
101101
int line_number; /* line number, starting from 1 */
102-
char *line_start; /* where that line starts within input */
102+
const char *line_start; /* where that line starts within input */
103103
JsonParserStack *pstack;
104104
JsonIncrementalState *inc_state;
105105
StringInfo strval;
@@ -157,7 +157,7 @@ extern JsonParseErrorType pg_parse_json(JsonLexContext *lex,
157157

158158
extern JsonParseErrorType pg_parse_json_incremental(JsonLexContext *lex,
159159
JsonSemAction *sem,
160-
char *json,
160+
const char *json,
161161
size_t len,
162162
bool is_last);
163163

@@ -192,7 +192,7 @@ extern JsonParseErrorType json_count_array_elements(JsonLexContext *lex,
192192
* cleanup.
193193
*/
194194
extern JsonLexContext *makeJsonLexContextCstringLen(JsonLexContext *lex,
195-
char *json,
195+
const char *json,
196196
size_t len,
197197
int encoding,
198198
bool need_escapes);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.