LCOV - code coverage report
Current view: top level - spdk/lib/json - json_util.c (source / functions) Hit Total Coverage
Test: Combined Lines: 335 361 92.8 %
Date: 2024-11-20 19:15:45 Functions: 31 32 96.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 411 713 57.6 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/json.h"
       7                 :            : 
       8                 :            : #include "spdk_internal/utf.h"
       9                 :            : #include "spdk/log.h"
      10                 :            : 
      11                 :            : #define SPDK_JSON_DEBUG(...) SPDK_DEBUGLOG(json_util, __VA_ARGS__)
      12                 :            : 
      13                 :            : size_t
      14                 :     339119 : spdk_json_val_len(const struct spdk_json_val *val)
      15                 :            : {
      16         [ +  + ]:     339119 :         if (val == NULL) {
      17                 :          0 :                 return 0;
      18                 :            :         }
      19                 :            : 
      20   [ +  +  +  +  :     339119 :         if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN || val->type == SPDK_JSON_VAL_OBJECT_BEGIN) {
          +  +  +  -  +  
                -  +  + ]
      21   [ +  -  +  - ]:      56702 :                 return val->len + 2;
      22                 :            :         }
      23                 :            : 
      24                 :     282417 :         return 1;
      25                 :      14725 : }
      26                 :            : 
      27                 :            : bool
      28                 :   56579527 : spdk_json_strequal(const struct spdk_json_val *val, const char *str)
      29                 :            : {
      30                 :            :         size_t len;
      31                 :            : 
      32   [ +  +  +  +  :   56579527 :         if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) {
          +  +  +  -  +  
                -  +  - ]
      33                 :          4 :                 return false;
      34                 :            :         }
      35                 :            : 
      36         [ +  + ]:   56579523 :         len = strlen(str);
      37   [ +  +  +  -  :   56579523 :         if (val->len != len) {
                   +  + ]
      38                 :   53018936 :                 return false;
      39                 :            :         }
      40                 :            : 
      41   [ +  +  +  +  :    3560587 :         return memcmp(val->start, str, len) == 0;
             +  -  +  - ]
      42                 :    2492020 : }
      43                 :            : 
      44                 :            : char *
      45                 :      65382 : spdk_json_strdup(const struct spdk_json_val *val)
      46                 :            : {
      47                 :            :         size_t len;
      48                 :            :         char *s;
      49                 :            : 
      50   [ +  +  +  +  :      65382 :         if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) {
          -  +  #  #  #  
                #  #  # ]
      51                 :         20 :                 return NULL;
      52                 :            :         }
      53                 :            : 
      54   [ +  -  +  - ]:      65362 :         len = val->len;
      55                 :            : 
      56   [ +  +  +  +  :      65362 :         if (memchr(val->start, '\0', len)) {
             +  -  -  + ]
      57                 :            :                 /* String contains embedded NUL, so it is not a valid C string. */
      58                 :          8 :                 return NULL;
      59                 :            :         }
      60                 :            : 
      61                 :      65354 :         s = malloc(len + 1);
      62         [ +  + ]:      65354 :         if (s == NULL) {
      63                 :          0 :                 return s;
      64                 :            :         }
      65                 :            : 
      66   [ +  +  +  +  :      65354 :         memcpy(s, val->start, len);
             +  -  +  - ]
      67   [ +  -  +  - ]:      65354 :         s[len] = '\0';
      68                 :            : 
      69                 :      65354 :         return s;
      70                 :       2675 : }
      71                 :            : 
      72                 :            : struct spdk_json_num {
      73                 :            :         bool negative;
      74                 :            :         uint64_t significand;
      75                 :            :         int64_t exponent;
      76                 :            : };
      77                 :            : 
      78                 :            : static int
      79                 :      31417 : json_number_split(const struct spdk_json_val *val, struct spdk_json_num *num)
      80                 :            : {
      81                 :            :         const char *iter;
      82                 :            :         size_t remaining;
      83                 :            :         uint64_t *pval;
      84                 :      31417 :         uint64_t frac_digits = 0;
      85                 :      31417 :         uint64_t exponent_u64 = 0;
      86                 :      31417 :         bool exponent_negative = false;
      87                 :            :         enum {
      88                 :            :                 NUM_STATE_INT,
      89                 :            :                 NUM_STATE_FRAC,
      90                 :            :                 NUM_STATE_EXP,
      91                 :            :         } state;
      92                 :            : 
      93         [ +  + ]:      31417 :         memset(num, 0, sizeof(*num));
      94                 :            : 
      95   [ +  +  +  -  :      31417 :         if (val->type != SPDK_JSON_VAL_NUMBER) {
                   -  + ]
      96                 :         32 :                 return -EINVAL;
      97                 :            :         }
      98                 :            : 
      99   [ +  -  +  - ]:      31385 :         remaining = val->len;
     100         [ +  + ]:      31385 :         if (remaining == 0) {
     101                 :          0 :                 return -EINVAL;
     102                 :            :         }
     103                 :            : 
     104   [ +  -  +  - ]:      31385 :         iter = val->start;
     105   [ +  +  +  - ]:      31385 :         if (*iter == '-') {
     106   [ #  #  #  # ]:        109 :                 num->negative = true;
     107         [ #  # ]:        109 :                 iter++;
     108                 :        109 :                 remaining--;
     109                 :         15 :         }
     110                 :            : 
     111                 :      31385 :         state = NUM_STATE_INT;
     112         [ +  - ]:      31385 :         pval = &num->significand;
     113         [ +  + ]:     106604 :         while (remaining--) {
     114   [ +  -  +  - ]:      75223 :                 char c = *iter++;
     115                 :            : 
     116         [ +  + ]:      75223 :                 if (c == '.') {
     117                 :        108 :                         state = NUM_STATE_FRAC;
     118   [ +  +  +  + ]:      75142 :                 } else if (c == 'e' || c == 'E') {
     119                 :        184 :                         state = NUM_STATE_EXP;
     120                 :        184 :                         pval = &exponent_u64;
     121         [ +  + ]:      74977 :                 } else if (c == '-') {
     122   [ +  +  #  # ]:         84 :                         assert(state == NUM_STATE_EXP);
     123                 :         84 :                         exponent_negative = true;
     124         [ -  + ]:      74868 :                 } else if (c == '+') {
     125   [ #  #  #  # ]:          0 :                         assert(state == NUM_STATE_EXP);
     126                 :            :                         /* exp_negative = false; */ /* already false by default */
     127                 :          0 :                 } else {
     128                 :            :                         uint64_t new_val;
     129                 :            : 
     130   [ +  -  +  - ]:      74847 :                         assert(c >= '0' && c <= '9');
     131         [ +  - ]:      74847 :                         new_val = *pval * 10 + c - '0';
     132   [ +  +  -  + ]:      74847 :                         if (new_val < *pval) {
     133                 :          4 :                                 return -ERANGE;
     134                 :            :                         }
     135                 :            : 
     136         [ +  + ]:      74843 :                         if (state == NUM_STATE_FRAC) {
     137                 :        240 :                                 frac_digits++;
     138                 :         60 :                         }
     139                 :            : 
     140         [ +  - ]:      74843 :                         *pval = new_val;
     141                 :            :                 }
     142                 :            :         }
     143                 :            : 
     144   [ +  +  -  + ]:      31381 :         if (exponent_negative) {
     145         [ -  + ]:         84 :                 if (exponent_u64 > 9223372036854775808ULL) { /* abs(INT64_MIN) */
     146                 :          0 :                         return -ERANGE;
     147                 :            :                 }
     148   [ #  #  #  # ]:         84 :                 num->exponent = (int64_t) - exponent_u64;
     149                 :         21 :         } else {
     150         [ -  + ]:      31297 :                 if (exponent_u64 > INT64_MAX) {
     151                 :          0 :                         return -ERANGE;
     152                 :            :                 }
     153   [ +  -  +  - ]:      31297 :                 num->exponent = exponent_u64;
     154                 :            :         }
     155   [ +  -  +  - ]:      31381 :         num->exponent -= frac_digits;
     156                 :            : 
     157                 :            :         /* Apply as much of the exponent as possible without overflow or truncation */
     158   [ +  +  +  -  :      31381 :         if (num->exponent < 0) {
                   +  - ]
     159   [ +  +  +  +  :        328 :                 while (num->exponent && num->significand >= 10 && num->significand % 10 == 0) {
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     160   [ #  #  #  #  :        164 :                         num->significand /= 10;
                   #  # ]
     161   [ #  #  #  # ]:        164 :                         num->exponent++;
     162                 :            :                 }
     163                 :         41 :         } else { /* positive exponent */
     164   [ +  +  +  -  :      31489 :                 while (num->exponent) {
                   +  - ]
     165   [ #  #  #  # ]:        284 :                         uint64_t new_val = num->significand * 10;
     166                 :            : 
     167   [ +  +  #  #  :        284 :                         if (new_val < num->significand) {
                   #  # ]
     168                 :         12 :                                 break;
     169                 :            :                         }
     170                 :            : 
     171   [ #  #  #  # ]:        272 :                         num->significand = new_val;
     172   [ #  #  #  # ]:        272 :                         num->exponent--;
     173                 :            :                 }
     174                 :            :         }
     175                 :            : 
     176                 :      31381 :         return 0;
     177                 :       2772 : }
     178                 :            : 
     179                 :            : int
     180                 :        527 : spdk_json_number_to_uint8(const struct spdk_json_val *val, uint8_t *num)
     181                 :            : {
     182                 :        251 :         struct spdk_json_num split_num;
     183                 :            :         int rc;
     184                 :            : 
     185                 :        527 :         rc = json_number_split(val, &split_num);
     186         [ -  + ]:        527 :         if (rc) {
     187                 :          0 :                 return rc;
     188                 :            :         }
     189                 :            : 
     190   [ +  -  +  +  :        527 :         if (split_num.exponent || split_num.negative) {
             +  +  -  + ]
     191                 :          0 :                 return -ERANGE;
     192                 :            :         }
     193                 :            : 
     194   [ +  +  -  + ]:        527 :         if (split_num.significand > UINT8_MAX) {
     195                 :          0 :                 return -ERANGE;
     196                 :            :         }
     197   [ +  -  +  - ]:        527 :         *num = (uint8_t)split_num.significand;
     198                 :        527 :         return 0;
     199                 :         32 : }
     200                 :            : 
     201                 :            : int
     202                 :        907 : spdk_json_number_to_uint16(const struct spdk_json_val *val, uint16_t *num)
     203                 :            : {
     204                 :        290 :         struct spdk_json_num split_num;
     205                 :            :         int rc;
     206                 :            : 
     207                 :        907 :         rc = json_number_split(val, &split_num);
     208         [ +  + ]:        907 :         if (rc) {
     209                 :          4 :                 return rc;
     210                 :            :         }
     211                 :            : 
     212   [ +  +  +  +  :        903 :         if (split_num.exponent || split_num.negative) {
             +  +  -  + ]
     213                 :         40 :                 return -ERANGE;
     214                 :            :         }
     215                 :            : 
     216   [ +  +  -  + ]:        863 :         if (split_num.significand > UINT16_MAX) {
     217                 :          8 :                 return -ERANGE;
     218                 :            :         }
     219   [ +  -  +  - ]:        855 :         *num = (uint16_t)split_num.significand;
     220                 :        855 :         return 0;
     221                 :        262 : }
     222                 :            : 
     223                 :            : int
     224                 :       2588 : spdk_json_number_to_int32(const struct spdk_json_val *val, int32_t *num)
     225                 :            : {
     226                 :        853 :         struct spdk_json_num split_num;
     227                 :            :         int rc;
     228                 :            : 
     229                 :       2588 :         rc = json_number_split(val, &split_num);
     230         [ +  + ]:       2588 :         if (rc) {
     231                 :          8 :                 return rc;
     232                 :            :         }
     233                 :            : 
     234   [ +  +  -  + ]:       2580 :         if (split_num.exponent) {
     235                 :         36 :                 return -ERANGE;
     236                 :            :         }
     237                 :            : 
     238   [ +  +  +  + ]:       2544 :         if (split_num.negative) {
     239   [ +  +  #  # ]:         67 :                 if (split_num.significand > 2147483648) { /* abs(INT32_MIN) */
     240                 :          4 :                         return -ERANGE;
     241                 :            :                 }
     242   [ #  #  #  #  :         63 :                 *num = (int32_t) - (int64_t)split_num.significand;
                   #  # ]
     243                 :         63 :                 return 0;
     244                 :            :         }
     245                 :            : 
     246                 :            :         /* positive */
     247   [ +  +  -  + ]:       2477 :         if (split_num.significand > INT32_MAX) {
     248                 :          4 :                 return -ERANGE;
     249                 :            :         }
     250   [ +  -  +  - ]:       2473 :         *num = (int32_t)split_num.significand;
     251                 :       2473 :         return 0;
     252                 :        147 : }
     253                 :            : 
     254                 :            : int
     255                 :      21028 : spdk_json_number_to_uint32(const struct spdk_json_val *val, uint32_t *num)
     256                 :            : {
     257                 :       6494 :         struct spdk_json_num split_num;
     258                 :            :         int rc;
     259                 :            : 
     260                 :      21028 :         rc = json_number_split(val, &split_num);
     261         [ +  + ]:      21028 :         if (rc) {
     262                 :         16 :                 return rc;
     263                 :            :         }
     264                 :            : 
     265   [ +  +  +  +  :      21012 :         if (split_num.exponent || split_num.negative) {
             +  +  -  + ]
     266                 :         26 :                 return -ERANGE;
     267                 :            :         }
     268                 :            : 
     269   [ +  +  -  + ]:      20986 :         if (split_num.significand > UINT32_MAX) {
     270                 :          4 :                 return -ERANGE;
     271                 :            :         }
     272   [ +  -  +  - ]:      20982 :         *num = (uint32_t)split_num.significand;
     273                 :      20982 :         return 0;
     274                 :       1833 : }
     275                 :            : 
     276                 :            : int
     277                 :       6367 : spdk_json_number_to_uint64(const struct spdk_json_val *val, uint64_t *num)
     278                 :            : {
     279                 :       2862 :         struct spdk_json_num split_num;
     280                 :            :         int rc;
     281                 :            : 
     282                 :       6367 :         rc = json_number_split(val, &split_num);
     283         [ +  + ]:       6367 :         if (rc) {
     284                 :          8 :                 return rc;
     285                 :            :         }
     286                 :            : 
     287   [ +  +  +  +  :       6359 :         if (split_num.exponent || split_num.negative) {
             +  +  -  + ]
     288                 :         48 :                 return -ERANGE;
     289                 :            :         }
     290                 :            : 
     291   [ +  -  +  - ]:       6311 :         *num = split_num.significand;
     292                 :       6311 :         return 0;
     293                 :        498 : }
     294                 :            : 
     295                 :            : static int
     296                 :     100502 : _json_decode_object(const struct spdk_json_val *values,
     297                 :            :                     const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out, bool relaxed)
     298                 :            : {
     299                 :            :         uint32_t i;
     300                 :     100502 :         bool invalid = false;
     301                 :            :         size_t decidx;
     302                 :            :         bool *seen;
     303                 :            : 
     304   [ +  -  +  +  :     100502 :         if (values == NULL || values->type != SPDK_JSON_VAL_OBJECT_BEGIN) {
             +  -  -  + ]
     305                 :          4 :                 return -1;
     306                 :            :         }
     307                 :            : 
     308                 :     100498 :         seen = calloc(sizeof(bool), num_decoders);
     309         [ +  + ]:     100498 :         if (seen == NULL) {
     310                 :          0 :                 return -1;
     311                 :            :         }
     312                 :            : 
     313   [ +  +  +  -  :     429412 :         for (i = 0; i < values->len;) {
                   +  + ]
     314         [ +  - ]:     328914 :                 const struct spdk_json_val *name = &values[i + 1];
     315         [ +  - ]:     328914 :                 const struct spdk_json_val *v = &values[i + 2];
     316                 :     328914 :                 bool found = false;
     317                 :            : 
     318         [ +  + ]:    1232084 :                 for (decidx = 0; decidx < num_decoders; decidx++) {
     319         [ +  - ]:    1224043 :                         const struct spdk_json_object_decoder *dec = &decoders[decidx];
     320   [ +  +  +  -  :    1224043 :                         if (spdk_json_strequal(name, dec->name)) {
                   +  + ]
     321   [ +  -  +  - ]:     320873 :                                 void *field = (void *)((uintptr_t)out + dec->offset);
     322                 :            : 
     323                 :     320873 :                                 found = true;
     324                 :            : 
     325   [ +  +  +  +  :     320873 :                                 if (seen[decidx]) {
             +  -  -  + ]
     326                 :            :                                         /* duplicate field name */
     327                 :          4 :                                         invalid = true;
     328   [ +  +  -  +  :          4 :                                         SPDK_JSON_DEBUG("Duplicate key '%s'\n", dec->name);
          #  #  #  #  #  
                      # ]
     329                 :          1 :                                 } else {
     330   [ +  -  +  - ]:     320869 :                                         seen[decidx] = true;
     331   [ +  +  +  -  :     320869 :                                         if (dec->decode_func(v, field)) {
          -  +  +  -  +  
                      - ]
     332                 :          6 :                                                 invalid = true;
     333   [ +  +  -  +  :          6 :                                                 SPDK_JSON_DEBUG("Decoder failed to decode key '%s'\n", dec->name);
          #  #  #  #  #  
                      # ]
     334                 :            :                                                 /* keep going to fill out any other valid keys */
     335                 :          1 :                                         }
     336                 :            :                                 }
     337                 :     320873 :                                 break;
     338                 :            :                         }
     339                 :      35867 :                 }
     340                 :            : 
     341   [ +  +  +  +  :     328914 :                 if (!relaxed && !found) {
             +  -  +  - ]
     342                 :          6 :                         invalid = true;
     343   [ +  +  -  +  :          6 :                         SPDK_JSON_DEBUG("Decoder not found for key '%.*s'\n", name->len, (char *)name->start);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     344                 :          1 :                 }
     345                 :            : 
     346                 :     328914 :                 i += 1 + spdk_json_val_len(v);
     347                 :            :         }
     348                 :            : 
     349         [ +  + ]:     555229 :         for (decidx = 0; decidx < num_decoders; decidx++) {
     350   [ +  +  +  +  :     454743 :                 if (!decoders[decidx].optional && !seen[decidx]) {
          +  +  +  +  +  
          +  +  -  +  -  
             +  -  +  - ]
     351                 :            :                         /* required field is missing */
     352                 :         12 :                         invalid = true;
     353                 :         12 :                         break;
     354                 :            :                 }
     355                 :      16893 :         }
     356                 :            : 
     357                 :     100498 :         free(seen);
     358         [ +  + ]:     100498 :         return invalid ? -1 : 0;
     359                 :       4076 : }
     360                 :            : 
     361                 :            : void
     362                 :          4 : spdk_json_free_object(const struct spdk_json_object_decoder *decoders, size_t num_decoders,
     363                 :            :                       void *obj)
     364                 :            : {
     365                 :          4 :         struct spdk_json_val invalid_val = {
     366                 :            :                 .start = "",
     367                 :            :                 .len = 0,
     368                 :            :                 .type = SPDK_JSON_VAL_INVALID
     369                 :            :         };
     370                 :            :         size_t decidx;
     371                 :            : 
     372         [ +  + ]:         20 :         for (decidx = 0; decidx < num_decoders; decidx++) {
     373         [ #  # ]:         16 :                 const struct spdk_json_object_decoder *dec = &decoders[decidx];
     374   [ #  #  #  # ]:         16 :                 void *field = (void *)((uintptr_t)obj + dec->offset);
     375                 :            : 
     376                 :            :                 /* decoding an invalid value will free the
     377                 :            :                  * previous memory without allocating it again.
     378                 :            :                  */
     379   [ #  #  #  #  :         16 :                 dec->decode_func(&invalid_val, field);
             #  #  #  # ]
     380                 :          4 :         }
     381                 :          4 : }
     382                 :            : 
     383                 :            : 
     384                 :            : int
     385                 :      92867 : spdk_json_decode_object(const struct spdk_json_val *values,
     386                 :            :                         const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out)
     387                 :            : {
     388                 :      92867 :         return _json_decode_object(values, decoders, num_decoders, out, false);
     389                 :            : }
     390                 :            : 
     391                 :            : int
     392                 :       7635 : spdk_json_decode_object_relaxed(const struct spdk_json_val *values,
     393                 :            :                                 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out)
     394                 :            : {
     395                 :       7635 :         return _json_decode_object(values, decoders, num_decoders, out, true);
     396                 :            : }
     397                 :            : 
     398                 :            : int
     399                 :       2802 : spdk_json_decode_array(const struct spdk_json_val *values, spdk_json_decode_fn decode_func,
     400                 :            :                        void *out, size_t max_size, size_t *out_size, size_t stride)
     401                 :            : {
     402                 :            :         uint32_t i;
     403                 :            :         char *field;
     404                 :            : 
     405   [ +  -  +  +  :       2802 :         if (values == NULL || values->type != SPDK_JSON_VAL_ARRAY_BEGIN) {
             +  -  -  + ]
     406                 :          4 :                 return -1;
     407                 :            :         }
     408                 :            : 
     409         [ +  - ]:       2798 :         *out_size = 0;
     410                 :       2798 :         field = out;
     411   [ +  +  +  -  :       8393 :         for (i = 0; i < values->len;) {
                   +  + ]
     412         [ +  - ]:       5607 :                 const struct spdk_json_val *v = &values[i + 1];
     413                 :            : 
     414   [ +  +  +  - ]:       5607 :                 if (*out_size == max_size) {
     415                 :          4 :                         return -1;
     416                 :            :                 }
     417                 :            : 
     418   [ +  +  +  -  :       5603 :                 if (decode_func(v, field)) {
                   +  - ]
     419                 :          8 :                         return -1;
     420                 :            :                 }
     421                 :            : 
     422                 :       5595 :                 i += spdk_json_val_len(v);
     423         [ +  - ]:       5595 :                 field += stride;
     424                 :       5595 :                 (*out_size)++;
     425                 :            :         }
     426                 :            : 
     427                 :       2786 :         return 0;
     428                 :         72 : }
     429                 :            : 
     430                 :            : int
     431                 :      26259 : spdk_json_decode_bool(const struct spdk_json_val *val, void *out)
     432                 :            : {
     433                 :      26259 :         bool *f = out;
     434                 :            : 
     435   [ +  +  +  +  :      26259 :         if (val->type != SPDK_JSON_VAL_TRUE && val->type != SPDK_JSON_VAL_FALSE) {
          +  +  +  -  +  
                -  +  - ]
     436                 :          4 :                 return -1;
     437                 :            :         }
     438                 :            : 
     439   [ +  -  +  -  :      26255 :         *f = val->type == SPDK_JSON_VAL_TRUE;
                   +  - ]
     440                 :      26255 :         return 0;
     441                 :        731 : }
     442                 :            : 
     443                 :            : int
     444                 :        527 : spdk_json_decode_uint8(const struct spdk_json_val *val, void *out)
     445                 :            : {
     446                 :        527 :         uint8_t *i = out;
     447                 :            : 
     448                 :        527 :         return spdk_json_number_to_uint8(val, i);
     449                 :            : }
     450                 :            : 
     451                 :            : int
     452                 :        727 : spdk_json_decode_uint16(const struct spdk_json_val *val, void *out)
     453                 :            : {
     454                 :        727 :         uint16_t *i = out;
     455                 :            : 
     456                 :        727 :         return spdk_json_number_to_uint16(val, i);
     457                 :            : }
     458                 :            : 
     459                 :            : int
     460                 :       2540 : spdk_json_decode_int32(const struct spdk_json_val *val, void *out)
     461                 :            : {
     462                 :       2540 :         int32_t *i = out;
     463                 :            : 
     464                 :       2540 :         return spdk_json_number_to_int32(val, i);
     465                 :            : }
     466                 :            : 
     467                 :            : int
     468                 :      21028 : spdk_json_decode_uint32(const struct spdk_json_val *val, void *out)
     469                 :            : {
     470                 :      21028 :         uint32_t *i = out;
     471                 :            : 
     472                 :      21028 :         return spdk_json_number_to_uint32(val, i);
     473                 :            : }
     474                 :            : 
     475                 :            : int
     476                 :       6304 : spdk_json_decode_uint64(const struct spdk_json_val *val, void *out)
     477                 :            : {
     478                 :       6304 :         uint64_t *i = out;
     479                 :            : 
     480                 :       6304 :         return spdk_json_number_to_uint64(val, i);
     481                 :            : }
     482                 :            : 
     483                 :            : int
     484                 :      65331 : spdk_json_decode_string(const struct spdk_json_val *val, void *out)
     485                 :            : {
     486                 :      65331 :         char **s = out;
     487                 :            : 
     488         [ +  - ]:      65331 :         free(*s);
     489                 :            : 
     490         [ +  - ]:      65331 :         *s = spdk_json_strdup(val);
     491                 :            : 
     492   [ +  +  +  - ]:      65331 :         if (*s) {
     493                 :      65303 :                 return 0;
     494                 :            :         } else {
     495                 :         28 :                 return -1;
     496                 :            :         }
     497                 :       2672 : }
     498                 :            : 
     499                 :            : int
     500                 :       1091 : spdk_json_decode_uuid(const struct spdk_json_val *val, void *out)
     501                 :            : {
     502                 :       1091 :         struct spdk_uuid *uuid = out;
     503                 :       1091 :         char *str = NULL;
     504                 :            :         int rc;
     505                 :            : 
     506                 :       1091 :         rc = spdk_json_decode_string(val, &str);
     507         [ +  + ]:       1091 :         if (rc != 0) {
     508                 :          4 :                 return rc;
     509                 :            :         }
     510                 :            : 
     511                 :       1087 :         rc = spdk_uuid_parse(uuid, str);
     512                 :       1087 :         free(str);
     513                 :            : 
     514         [ +  + ]:       1087 :         return rc == 0 ? 0 : -1;
     515                 :        194 : }
     516                 :            : 
     517                 :            : static struct spdk_json_val *
     518                 :       6708 : json_first(struct spdk_json_val *object, enum spdk_json_val_type type)
     519                 :            : {
     520                 :            :         /* 'object' must be JSON object or array. 'type' might be combination of these two. */
     521   [ +  +  #  # ]:       6708 :         assert((type & (SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN)) != 0);
     522                 :            : 
     523   [ +  +  #  # ]:       6708 :         assert(object != NULL);
     524                 :            : 
     525   [ +  +  +  -  :       6708 :         if ((object->type & type) == 0) {
                   +  + ]
     526                 :        112 :                 return NULL;
     527                 :            :         }
     528                 :            : 
     529         [ +  - ]:       6596 :         object++;
     530   [ +  +  +  -  :       6596 :         if (object->len == 0) {
                   +  - ]
     531                 :          0 :                 return NULL;
     532                 :            :         }
     533                 :            : 
     534                 :       6596 :         return object;
     535                 :        511 : }
     536                 :            : 
     537                 :            : static struct spdk_json_val *
     538                 :       3248 : json_value(struct spdk_json_val *key)
     539                 :            : {
     540   [ +  -  +  -  :       3248 :         return key->type == SPDK_JSON_VAL_NAME ? key + 1 : NULL;
             +  -  -  + ]
     541                 :            : }
     542                 :            : 
     543                 :            : int
     544                 :       1616 : spdk_json_find(struct spdk_json_val *object, const char *key_name, struct spdk_json_val **key,
     545                 :            :                struct spdk_json_val **val, enum spdk_json_val_type type)
     546                 :            : {
     547                 :       1616 :         struct spdk_json_val *_key = NULL;
     548                 :       1616 :         struct spdk_json_val *_val = NULL;
     549                 :            :         struct spdk_json_val *it_first, *it;
     550                 :            : 
     551   [ +  +  #  # ]:       1616 :         assert(object != NULL);
     552                 :            : 
     553                 :       1616 :         it_first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN);
     554         [ +  + ]:       1616 :         if (!it_first) {
     555   [ +  +  -  +  :         28 :                 SPDK_JSON_DEBUG("Not enclosed in {}\n");
                   #  # ]
     556                 :         28 :                 return -EPROTOTYPE;
     557                 :            :         }
     558                 :            : 
     559         [ +  + ]:       1710 :         for (it = it_first;
     560         [ +  + ]:       3220 :              it != NULL;
     561                 :       1632 :              it = spdk_json_next(it)) {
     562   [ +  +  +  -  :       1660 :                 if (it->type != SPDK_JSON_VAL_NAME) {
                   -  + ]
     563                 :          0 :                         continue;
     564                 :            :                 }
     565                 :            : 
     566         [ +  + ]:       1660 :                 if (spdk_json_strequal(it, key_name) != true) {
     567                 :         76 :                         continue;
     568                 :            :                 }
     569                 :            : 
     570         [ +  + ]:       1584 :                 if (_key) {
     571   [ #  #  #  #  :          0 :                         SPDK_JSON_DEBUG("Duplicate key '%s'", key_name);
                   #  # ]
     572                 :          0 :                         return -EINVAL;
     573                 :            :                 }
     574                 :            : 
     575                 :       1584 :                 _key = it;
     576                 :       1584 :                 _val = json_value(_key);
     577                 :            : 
     578   [ +  +  +  +  :       1584 :                 if (type != SPDK_JSON_VAL_ANY && (_val->type & type) == 0) {
             +  -  -  + ]
     579   [ +  +  -  +  :         28 :                         SPDK_JSON_DEBUG("key '%s' type is %#x but expected one of %#x\n", key_name, _val->type, type);
          #  #  #  #  #  
                      # ]
     580                 :         28 :                         return -EDOM;
     581                 :            :                 }
     582                 :        110 :         }
     583                 :            : 
     584         [ +  + ]:       1560 :         if (key) {
     585         [ #  # ]:         20 :                 *key = _key;
     586                 :          5 :         }
     587                 :            : 
     588         [ +  + ]:       1560 :         if (val) {
     589         [ +  - ]:       1560 :                 *val = _val;
     590                 :        111 :         }
     591                 :            : 
     592         [ +  + ]:       1560 :         return _val ? 0 : -ENOENT;
     593                 :        117 : }
     594                 :            : 
     595                 :            : int
     596                 :          0 : spdk_json_find_string(struct spdk_json_val *object, const char *key_name,
     597                 :            :                       struct spdk_json_val **key, struct spdk_json_val **val)
     598                 :            : {
     599                 :          0 :         return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_STRING);
     600                 :            : }
     601                 :            : 
     602                 :            : int
     603                 :       1588 : spdk_json_find_array(struct spdk_json_val *object, const char *key_name,
     604                 :            :                      struct spdk_json_val **key, struct spdk_json_val **val)
     605                 :            : {
     606                 :       1588 :         return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_ARRAY_BEGIN);
     607                 :            : }
     608                 :            : 
     609                 :            : struct spdk_json_val *
     610                 :          8 : spdk_json_object_first(struct spdk_json_val *object)
     611                 :            : {
     612                 :          8 :         struct spdk_json_val *first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN);
     613                 :            : 
     614                 :            :         /* Empty object? */
     615   [ +  -  +  -  :          8 :         return first && first->type != SPDK_JSON_VAL_OBJECT_END ? first : NULL;
             #  #  #  # ]
     616                 :            : }
     617                 :            : 
     618                 :            : struct spdk_json_val *
     619                 :       5084 : spdk_json_array_first(struct spdk_json_val *array_begin)
     620                 :            : {
     621                 :       5084 :         struct spdk_json_val *first = json_first(array_begin, SPDK_JSON_VAL_ARRAY_BEGIN);
     622                 :            : 
     623                 :            :         /* Empty array? */
     624   [ +  +  +  +  :       5084 :         return first && first->type != SPDK_JSON_VAL_ARRAY_END ? first : NULL;
             +  -  +  + ]
     625                 :            : }
     626                 :            : 
     627                 :            : static struct spdk_json_val *
     628                 :      15702 : json_skip_object_or_array(struct spdk_json_val *val)
     629                 :            : {
     630                 :            :         unsigned lvl;
     631                 :            :         enum spdk_json_val_type end_type;
     632                 :            :         struct spdk_json_val *it;
     633                 :            : 
     634   [ +  +  +  -  :      15702 :         if (val->type == SPDK_JSON_VAL_OBJECT_BEGIN) {
                   +  + ]
     635                 :      14086 :                 end_type = SPDK_JSON_VAL_OBJECT_END;
     636   [ +  -  +  -  :       3081 :         } else if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN) {
                   +  - ]
     637                 :       1616 :                 end_type = SPDK_JSON_VAL_ARRAY_END;
     638                 :        118 :         } else {
     639   [ #  #  #  #  :          0 :                 SPDK_JSON_DEBUG("Expected JSON object (%#x) or array (%#x) but got %#x\n",
          #  #  #  #  #  
                      # ]
     640                 :            :                                 SPDK_JSON_VAL_OBJECT_BEGIN, SPDK_JSON_VAL_ARRAY_BEGIN, val->type);
     641                 :          0 :                 return NULL;
     642                 :            :         }
     643                 :            : 
     644                 :      15702 :         lvl = 1;
     645   [ +  +  +  +  :     658636 :         for (it = val + 1; it->type != SPDK_JSON_VAL_INVALID && lvl != 0; it++) {
          +  -  -  +  +  
                +  +  - ]
     646   [ +  +  +  -  :     642934 :                 if (it->type == val->type) {
          +  -  +  -  +  
                      + ]
     647                 :      34458 :                         lvl++;
     648   [ +  +  +  -  :     612537 :                 } else if (it->type == end_type) {
                   +  + ]
     649                 :      50160 :                         lvl--;
     650                 :       5644 :                 }
     651                 :      74323 :         }
     652                 :            : 
     653                 :            :         /* if lvl != 0 we have invalid JSON object */
     654         [ +  + ]:      15702 :         if (lvl != 0) {
     655   [ #  #  #  #  :          0 :                 SPDK_JSON_DEBUG("Can't find end of object (type: %#x): lvl (%u) != 0)\n", val->type, lvl);
          #  #  #  #  #  
                      # ]
     656                 :          0 :                 it = NULL;
     657                 :          0 :         }
     658                 :            : 
     659                 :      15702 :         return it;
     660                 :       1583 : }
     661                 :            : 
     662                 :            : struct spdk_json_val *
     663                 :      17394 : spdk_json_next(struct spdk_json_val *it)
     664                 :            : {
     665                 :            :         struct spdk_json_val *val, *next;
     666                 :            : 
     667   [ +  +  +  -  :      17394 :         switch (it->type) {
             +  -  -  +  
                      + ]
     668                 :       1525 :         case SPDK_JSON_VAL_NAME:
     669                 :       1652 :                 val = json_value(it);
     670                 :       1652 :                 next = spdk_json_next(val);
     671                 :       1652 :                 break;
     672                 :            : 
     673                 :            :         /* We are in the middle of an array - get to next entry */
     674                 :         30 :         case SPDK_JSON_VAL_NULL:
     675                 :            :         case SPDK_JSON_VAL_TRUE:
     676                 :            :         case SPDK_JSON_VAL_FALSE:
     677                 :            :         case SPDK_JSON_VAL_NUMBER:
     678                 :            :         case SPDK_JSON_VAL_STRING:
     679         [ #  # ]:         40 :                 val = it + 1;
     680                 :         40 :                 return val;
     681                 :            : 
     682                 :      14119 :         case SPDK_JSON_VAL_ARRAY_BEGIN:
     683                 :            :         case SPDK_JSON_VAL_OBJECT_BEGIN:
     684                 :      15702 :                 next = json_skip_object_or_array(it);
     685                 :      15702 :                 break;
     686                 :            : 
     687                 :            :         /* Can't go to the next object if started from the end of array or object */
     688                 :          0 :         case SPDK_JSON_VAL_ARRAY_END:
     689                 :            :         case SPDK_JSON_VAL_OBJECT_END:
     690                 :            :         case SPDK_JSON_VAL_INVALID:
     691                 :          0 :                 return NULL;
     692                 :          0 :         default:
     693         [ #  # ]:          0 :                 assert(false);
     694                 :            :                 return NULL;
     695                 :            : 
     696                 :            :         }
     697                 :            : 
     698                 :            :         /* EOF ? */
     699         [ +  + ]:      17354 :         if (next == NULL) {
     700                 :       1568 :                 return NULL;
     701                 :            :         }
     702                 :            : 
     703   [ +  +  +  -  :      15786 :         switch (next->type) {
                   +  + ]
     704                 :       5487 :         case SPDK_JSON_VAL_ARRAY_END:
     705                 :            :         case SPDK_JSON_VAL_OBJECT_END:
     706                 :            :         case SPDK_JSON_VAL_INVALID:
     707                 :       5964 :                 return NULL;
     708                 :       8702 :         default:
     709                 :            :                 /* Next value */
     710                 :       9822 :                 return next;
     711                 :            :         }
     712                 :       1720 : }
     713                 :            : 
     714                 :       2907 : SPDK_LOG_REGISTER_COMPONENT(json_util)

Generated by: LCOV version 1.15