jsonapi: Use size_t
authorPeter Eisentraut <[email protected]>
Fri, 21 Jun 2024 05:50:02 +0000 (07:50 +0200)
committerPeter Eisentraut <[email protected]>
Fri, 21 Jun 2024 05:53:30 +0000 (07:53 +0200)
Use size_t instead of int for object sizes in the jsonapi.  This makes
the API better self-documenting.

Reviewed-by: Andrew Dunstan <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/f732b014-f614-4600-a437-dba5a2c3738b%40eisentraut.org

src/common/jsonapi.c
src/common/parse_manifest.c
src/include/common/jsonapi.h
src/include/common/parse_manifest.h

index 172310f6f19dbc78ea34ed931a8638a95d0e01b5..f71c1c54b2a5c18690a4189ba0c1f4e1e82aff41 100644 (file)
@@ -85,7 +85,7 @@ struct JsonParserStack
 {
    int         stack_size;
    char       *prediction;
-   int         pred_index;
+   size_t      pred_index;
    /* these two are indexed by lex_level */
    char      **fnames;
    bool       *fnull;
@@ -212,7 +212,7 @@ static char JSON_PROD_GOAL[] = {JSON_TOKEN_END, JSON_NT_JSON, 0};
 
 static inline JsonParseErrorType json_lex_string(JsonLexContext *lex);
 static inline JsonParseErrorType json_lex_number(JsonLexContext *lex, char *s,
-                                                bool *num_err, int *total_len);
+                                                bool *num_err, size_t *total_len);
 static inline JsonParseErrorType parse_scalar(JsonLexContext *lex, JsonSemAction *sem);
 static JsonParseErrorType parse_object_field(JsonLexContext *lex, JsonSemAction *sem);
 static JsonParseErrorType parse_object(JsonLexContext *lex, JsonSemAction *sem);
@@ -269,10 +269,10 @@ lex_expect(JsonParseContext ctx, JsonLexContext *lex, JsonTokenType token)
  * str is of length len, and need not be null-terminated.
  */
 bool
-IsValidJsonNumber(const char *str, int len)
+IsValidJsonNumber(const char *str, size_t len)
 {
    bool        numeric_error;
-   int         total_len;
+   size_t      total_len;
    JsonLexContext dummy_lex;
 
    if (len <= 0)
@@ -324,7 +324,7 @@ IsValidJsonNumber(const char *str, int len)
  */
 JsonLexContext *
 makeJsonLexContextCstringLen(JsonLexContext *lex, char *json,
-                            int len, int encoding, bool need_escapes)
+                            size_t len, int encoding, bool need_escapes)
 {
    if (lex == NULL)
    {
@@ -650,7 +650,7 @@ JsonParseErrorType
 pg_parse_json_incremental(JsonLexContext *lex,
                          JsonSemAction *sem,
                          char *json,
-                         int len,
+                         size_t len,
                          bool is_last)
 {
    JsonTokenType tok;
@@ -888,7 +888,7 @@ pg_parse_json_incremental(JsonLexContext *lex,
                            }
                            else
                            {
-                               int         tlen = (lex->token_terminator - lex->token_start);
+                               ptrdiff_t   tlen = (lex->token_terminator - lex->token_start);
 
                                pstack->scalar_val = palloc(tlen + 1);
                                memcpy(pstack->scalar_val, lex->token_start, tlen);
@@ -1332,7 +1332,7 @@ json_lex(JsonLexContext *lex)
         * recursive call
         */
        StringInfo  ptok = &(lex->inc_state->partial_token);
-       int         added = 0;
+       size_t      added = 0;
        bool        tok_done = false;
        JsonLexContext dummy_lex;
        JsonParseErrorType partial_result;
@@ -1354,7 +1354,7 @@ json_lex(JsonLexContext *lex)
                    break;
            }
 
-           for (int i = 0; i < lex->input_length; i++)
+           for (size_t i = 0; i < lex->input_length; i++)
            {
                char        c = lex->input[i];
 
@@ -1382,7 +1382,7 @@ json_lex(JsonLexContext *lex)
 
                bool        numend = false;
 
-               for (int i = 0; i < lex->input_length && !numend; i++)
+               for (size_t i = 0; i < lex->input_length && !numend; i++)
                {
                    char        cc = lex->input[i];
 
@@ -1418,7 +1418,7 @@ json_lex(JsonLexContext *lex)
             * {null, false, true} literals as well as any trailing
             * alphanumeric junk on non-string tokens.
             */
-           for (int i = added; i < lex->input_length; i++)
+           for (size_t i = added; i < lex->input_length; i++)
            {
                char        cc = lex->input[i];
 
@@ -1941,7 +1941,7 @@ json_lex_string(JsonLexContext *lex)
  */
 static inline JsonParseErrorType
 json_lex_number(JsonLexContext *lex, char *s,
-               bool *num_err, int *total_len)
+               bool *num_err, size_t *total_len)
 {
    bool        error = false;
    int         len = s - lex->input;
index 821fba3967a78abccff90ac67d7d72bde5c5a903..373a4f6c00fa94187cc2d58a8b09410ae2b8554d 100644 (file)
@@ -183,7 +183,7 @@ json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incs
 
 void
 json_parse_manifest_incremental_chunk(
-                                     JsonManifestParseIncrementalState *incstate, char *chunk, int size,
+                                     JsonManifestParseIncrementalState *incstate, char *chunk, size_t size,
                                      bool is_last)
 {
    JsonParseErrorType res,
index f1ab17fc9f28e47398e2fb30b11072eb45f26db4..5d3ae4e09b8f76ea118e63a0e2c301136f2c1489 100644 (file)
@@ -89,7 +89,7 @@ typedef struct JsonIncrementalState JsonIncrementalState;
 typedef struct JsonLexContext
 {
    char       *input;
-   int         input_length;
+   size_t      input_length;
    int         input_encoding;
    char       *token_start;
    char       *token_terminator;
@@ -158,7 +158,7 @@ extern JsonParseErrorType pg_parse_json(JsonLexContext *lex,
 extern JsonParseErrorType pg_parse_json_incremental(JsonLexContext *lex,
                                                    JsonSemAction *sem,
                                                    char *json,
-                                                   int len,
+                                                   size_t len,
                                                    bool is_last);
 
 /* the null action object used for pure validation */
@@ -193,7 +193,7 @@ extern JsonParseErrorType json_count_array_elements(JsonLexContext *lex,
  */
 extern JsonLexContext *makeJsonLexContextCstringLen(JsonLexContext *lex,
                                                    char *json,
-                                                   int len,
+                                                   size_t len,
                                                    int encoding,
                                                    bool need_escapes);
 
@@ -219,6 +219,6 @@ extern char *json_errdetail(JsonParseErrorType error, JsonLexContext *lex);
  *
  * str argument does not need to be nul-terminated.
  */
-extern bool IsValidJsonNumber(const char *str, int len);
+extern bool IsValidJsonNumber(const char *str, size_t len);
 
 #endif                         /* JSONAPI_H */
index 0d04239c38dda5fa8cda763fca82c6f8cdb0bf59..2777b1e9d22c5a01a04d7aefda3d5366b96abc8a 100644 (file)
@@ -51,7 +51,7 @@ extern void json_parse_manifest(JsonManifestParseContext *context,
                                char *buffer, size_t size);
 extern JsonManifestParseIncrementalState *json_parse_manifest_incremental_init(JsonManifestParseContext *context);
 extern void json_parse_manifest_incremental_chunk(
-                                                 JsonManifestParseIncrementalState *incstate, char *chunk, int size,
+                                                 JsonManifestParseIncrementalState *incstate, char *chunk, size_t size,
                                                  bool is_last);
 extern void json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incstate);