LCOV - code coverage report
Current view: top level - spdk/lib/json - json_util.c (source / functions) Hit Total Coverage
Test: Combined Lines: 301 325 92.6 %
Date: 2024-07-13 18:15:50 Functions: 31 32 96.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 222 285 77.9 %

           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                 :     384926 : spdk_json_val_len(const struct spdk_json_val *val)
      15                 :            : {
      16         [ -  + ]:     384926 :         if (val == NULL) {
      17                 :          0 :                 return 0;
      18                 :            :         }
      19                 :            : 
      20   [ +  +  +  + ]:     384926 :         if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN || val->type == SPDK_JSON_VAL_OBJECT_BEGIN) {
      21                 :      65353 :                 return val->len + 2;
      22                 :            :         }
      23                 :            : 
      24                 :     319573 :         return 1;
      25                 :            : }
      26                 :            : 
      27                 :            : bool
      28                 :   58474065 : spdk_json_strequal(const struct spdk_json_val *val, const char *str)
      29                 :            : {
      30                 :            :         size_t len;
      31                 :            : 
      32   [ +  +  +  + ]:   58474065 :         if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) {
      33                 :          6 :                 return false;
      34                 :            :         }
      35                 :            : 
      36         [ -  + ]:   58474059 :         len = strlen(str);
      37         [ +  + ]:   58474059 :         if (val->len != len) {
      38                 :   54771074 :                 return false;
      39                 :            :         }
      40                 :            : 
      41   [ -  +  -  + ]:    3702987 :         return memcmp(val->start, str, len) == 0;
      42                 :            : }
      43                 :            : 
      44                 :            : char *
      45                 :      75746 : spdk_json_strdup(const struct spdk_json_val *val)
      46                 :            : {
      47                 :            :         size_t len;
      48                 :            :         char *s;
      49                 :            : 
      50   [ +  +  +  - ]:      75746 :         if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) {
      51                 :         30 :                 return NULL;
      52                 :            :         }
      53                 :            : 
      54                 :      75716 :         len = val->len;
      55                 :            : 
      56   [ +  +  +  + ]:      75716 :         if (memchr(val->start, '\0', len)) {
      57                 :            :                 /* String contains embedded NUL, so it is not a valid C string. */
      58                 :         12 :                 return NULL;
      59                 :            :         }
      60                 :            : 
      61                 :      75704 :         s = malloc(len + 1);
      62         [ -  + ]:      75704 :         if (s == NULL) {
      63                 :          0 :                 return s;
      64                 :            :         }
      65                 :            : 
      66   [ -  +  -  + ]:      75704 :         memcpy(s, val->start, len);
      67                 :      75704 :         s[len] = '\0';
      68                 :            : 
      69                 :      75704 :         return s;
      70                 :            : }
      71                 :            : 
      72                 :            : struct spdk_json_num {
      73                 :            :         bool negative;
      74                 :            :         uint64_t significand;
      75                 :            :         int64_t exponent;
      76                 :            : };
      77                 :            : 
      78                 :            : static int
      79                 :      31611 : 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                 :      31611 :         uint64_t frac_digits = 0;
      85                 :      31611 :         uint64_t exponent_u64 = 0;
      86                 :      31611 :         bool exponent_negative = false;
      87                 :            :         enum {
      88                 :            :                 NUM_STATE_INT,
      89                 :            :                 NUM_STATE_FRAC,
      90                 :            :                 NUM_STATE_EXP,
      91                 :            :         } state;
      92                 :            : 
      93         [ -  + ]:      31611 :         memset(num, 0, sizeof(*num));
      94                 :            : 
      95         [ +  + ]:      31611 :         if (val->type != SPDK_JSON_VAL_NUMBER) {
      96                 :         48 :                 return -EINVAL;
      97                 :            :         }
      98                 :            : 
      99                 :      31563 :         remaining = val->len;
     100         [ -  + ]:      31563 :         if (remaining == 0) {
     101                 :          0 :                 return -EINVAL;
     102                 :            :         }
     103                 :            : 
     104                 :      31563 :         iter = val->start;
     105         [ +  + ]:      31563 :         if (*iter == '-') {
     106                 :        139 :                 num->negative = true;
     107                 :        139 :                 iter++;
     108                 :        139 :                 remaining--;
     109                 :            :         }
     110                 :            : 
     111                 :      31563 :         state = NUM_STATE_INT;
     112                 :      31563 :         pval = &num->significand;
     113         [ +  + ]:     118989 :         while (remaining--) {
     114                 :      87432 :                 char c = *iter++;
     115                 :            : 
     116         [ +  + ]:      87432 :                 if (c == '.') {
     117                 :        162 :                         state = NUM_STATE_FRAC;
     118   [ +  +  +  + ]:      87270 :                 } else if (c == 'e' || c == 'E') {
     119                 :        276 :                         state = NUM_STATE_EXP;
     120                 :        276 :                         pval = &exponent_u64;
     121         [ +  + ]:      86994 :                 } else if (c == '-') {
     122         [ -  + ]:        126 :                         assert(state == NUM_STATE_EXP);
     123                 :        126 :                         exponent_negative = true;
     124         [ -  + ]:      86868 :                 } else if (c == '+') {
     125         [ #  # ]:          0 :                         assert(state == NUM_STATE_EXP);
     126                 :            :                         /* exp_negative = false; */ /* already false by default */
     127                 :            :                 } else {
     128                 :            :                         uint64_t new_val;
     129                 :            : 
     130   [ +  -  +  + ]:      86868 :                         assert(c >= '0' && c <= '9');
     131                 :      86868 :                         new_val = *pval * 10 + c - '0';
     132         [ +  + ]:      86868 :                         if (new_val < *pval) {
     133                 :          6 :                                 return -ERANGE;
     134                 :            :                         }
     135                 :            : 
     136         [ +  + ]:      86862 :                         if (state == NUM_STATE_FRAC) {
     137                 :        360 :                                 frac_digits++;
     138                 :            :                         }
     139                 :            : 
     140                 :      86862 :                         *pval = new_val;
     141                 :            :                 }
     142                 :            :         }
     143                 :            : 
     144         [ +  + ]:      31557 :         if (exponent_negative) {
     145         [ -  + ]:        126 :                 if (exponent_u64 > 9223372036854775808ULL) { /* abs(INT64_MIN) */
     146                 :          0 :                         return -ERANGE;
     147                 :            :                 }
     148                 :        126 :                 num->exponent = (int64_t) - exponent_u64;
     149                 :            :         } else {
     150         [ -  + ]:      31431 :                 if (exponent_u64 > INT64_MAX) {
     151                 :          0 :                         return -ERANGE;
     152                 :            :                 }
     153                 :      31431 :                 num->exponent = exponent_u64;
     154                 :            :         }
     155                 :      31557 :         num->exponent -= frac_digits;
     156                 :            : 
     157                 :            :         /* Apply as much of the exponent as possible without overflow or truncation */
     158         [ +  + ]:      31557 :         if (num->exponent < 0) {
     159   [ +  +  +  +  :        492 :                 while (num->exponent && num->significand >= 10 && num->significand % 10 == 0) {
                   +  + ]
     160                 :        246 :                         num->significand /= 10;
     161                 :        246 :                         num->exponent++;
     162                 :            :                 }
     163                 :            :         } else { /* positive exponent */
     164         [ +  + ]:      31719 :                 while (num->exponent) {
     165                 :        426 :                         uint64_t new_val = num->significand * 10;
     166                 :            : 
     167         [ +  + ]:        426 :                         if (new_val < num->significand) {
     168                 :         18 :                                 break;
     169                 :            :                         }
     170                 :            : 
     171                 :        408 :                         num->significand = new_val;
     172                 :        408 :                         num->exponent--;
     173                 :            :                 }
     174                 :            :         }
     175                 :            : 
     176                 :      31557 :         return 0;
     177                 :            : }
     178                 :            : 
     179                 :            : int
     180                 :        534 : spdk_json_number_to_uint8(const struct spdk_json_val *val, uint8_t *num)
     181                 :            : {
     182                 :        291 :         struct spdk_json_num split_num;
     183                 :            :         int rc;
     184                 :            : 
     185                 :        534 :         rc = json_number_split(val, &split_num);
     186         [ -  + ]:        534 :         if (rc) {
     187                 :          0 :                 return rc;
     188                 :            :         }
     189                 :            : 
     190   [ +  -  -  +  :        534 :         if (split_num.exponent || split_num.negative) {
                   -  + ]
     191                 :          0 :                 return -ERANGE;
     192                 :            :         }
     193                 :            : 
     194         [ -  + ]:        534 :         if (split_num.significand > UINT8_MAX) {
     195                 :          0 :                 return -ERANGE;
     196                 :            :         }
     197                 :        534 :         *num = (uint8_t)split_num.significand;
     198                 :        534 :         return 0;
     199                 :            : }
     200                 :            : 
     201                 :            : int
     202                 :        738 : spdk_json_number_to_uint16(const struct spdk_json_val *val, uint16_t *num)
     203                 :            : {
     204                 :        366 :         struct spdk_json_num split_num;
     205                 :            :         int rc;
     206                 :            : 
     207                 :        738 :         rc = json_number_split(val, &split_num);
     208         [ +  + ]:        738 :         if (rc) {
     209                 :          6 :                 return rc;
     210                 :            :         }
     211                 :            : 
     212   [ +  +  +  +  :        732 :         if (split_num.exponent || split_num.negative) {
                   +  + ]
     213                 :         60 :                 return -ERANGE;
     214                 :            :         }
     215                 :            : 
     216         [ +  + ]:        672 :         if (split_num.significand > UINT16_MAX) {
     217                 :         12 :                 return -ERANGE;
     218                 :            :         }
     219                 :        660 :         *num = (uint16_t)split_num.significand;
     220                 :        660 :         return 0;
     221                 :            : }
     222                 :            : 
     223                 :            : int
     224                 :       2388 : spdk_json_number_to_int32(const struct spdk_json_val *val, int32_t *num)
     225                 :            : {
     226                 :        700 :         struct spdk_json_num split_num;
     227                 :            :         int rc;
     228                 :            : 
     229                 :       2388 :         rc = json_number_split(val, &split_num);
     230         [ +  + ]:       2388 :         if (rc) {
     231                 :         12 :                 return rc;
     232                 :            :         }
     233                 :            : 
     234         [ +  + ]:       2376 :         if (split_num.exponent) {
     235                 :         54 :                 return -ERANGE;
     236                 :            :         }
     237                 :            : 
     238   [ +  +  +  + ]:       2322 :         if (split_num.negative) {
     239         [ +  + ]:         77 :                 if (split_num.significand > 2147483648) { /* abs(INT32_MIN) */
     240                 :          6 :                         return -ERANGE;
     241                 :            :                 }
     242                 :         71 :                 *num = (int32_t) - (int64_t)split_num.significand;
     243                 :         71 :                 return 0;
     244                 :            :         }
     245                 :            : 
     246                 :            :         /* positive */
     247         [ +  + ]:       2245 :         if (split_num.significand > INT32_MAX) {
     248                 :          6 :                 return -ERANGE;
     249                 :            :         }
     250                 :       2239 :         *num = (int32_t)split_num.significand;
     251                 :       2239 :         return 0;
     252                 :            : }
     253                 :            : 
     254                 :            : int
     255                 :      18992 : spdk_json_number_to_uint32(const struct spdk_json_val *val, uint32_t *num)
     256                 :            : {
     257                 :       7805 :         struct spdk_json_num split_num;
     258                 :            :         int rc;
     259                 :            : 
     260                 :      18992 :         rc = json_number_split(val, &split_num);
     261         [ +  + ]:      18992 :         if (rc) {
     262                 :         24 :                 return rc;
     263                 :            :         }
     264                 :            : 
     265   [ +  +  +  +  :      18968 :         if (split_num.exponent || split_num.negative) {
                   +  + ]
     266                 :         38 :                 return -ERANGE;
     267                 :            :         }
     268                 :            : 
     269         [ +  + ]:      18930 :         if (split_num.significand > UINT32_MAX) {
     270                 :          6 :                 return -ERANGE;
     271                 :            :         }
     272                 :      18924 :         *num = (uint32_t)split_num.significand;
     273                 :      18924 :         return 0;
     274                 :            : }
     275                 :            : 
     276                 :            : int
     277                 :       8959 : spdk_json_number_to_uint64(const struct spdk_json_val *val, uint64_t *num)
     278                 :            : {
     279                 :       4551 :         struct spdk_json_num split_num;
     280                 :            :         int rc;
     281                 :            : 
     282                 :       8959 :         rc = json_number_split(val, &split_num);
     283         [ +  + ]:       8959 :         if (rc) {
     284                 :         12 :                 return rc;
     285                 :            :         }
     286                 :            : 
     287   [ +  +  +  +  :       8947 :         if (split_num.exponent || split_num.negative) {
                   +  + ]
     288                 :         72 :                 return -ERANGE;
     289                 :            :         }
     290                 :            : 
     291                 :       8875 :         *num = split_num.significand;
     292                 :       8875 :         return 0;
     293                 :            : }
     294                 :            : 
     295                 :            : static int
     296                 :     118290 : _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                 :     118290 :         bool invalid = false;
     301                 :            :         size_t decidx;
     302                 :            :         bool *seen;
     303                 :            : 
     304   [ +  -  +  + ]:     118290 :         if (values == NULL || values->type != SPDK_JSON_VAL_OBJECT_BEGIN) {
     305                 :          6 :                 return -1;
     306                 :            :         }
     307                 :            : 
     308                 :     118284 :         seen = calloc(sizeof(bool), num_decoders);
     309         [ -  + ]:     118284 :         if (seen == NULL) {
     310                 :          0 :                 return -1;
     311                 :            :         }
     312                 :            : 
     313         [ +  + ]:     491758 :         for (i = 0; i < values->len;) {
     314                 :     373474 :                 const struct spdk_json_val *name = &values[i + 1];
     315                 :     373474 :                 const struct spdk_json_val *v = &values[i + 2];
     316                 :     373474 :                 bool found = false;
     317                 :            : 
     318         [ +  + ]:    1290127 :                 for (decidx = 0; decidx < num_decoders; decidx++) {
     319                 :    1283208 :                         const struct spdk_json_object_decoder *dec = &decoders[decidx];
     320         [ +  + ]:    1283208 :                         if (spdk_json_strequal(name, dec->name)) {
     321                 :     366555 :                                 void *field = (void *)((uintptr_t)out + dec->offset);
     322                 :            : 
     323                 :     366555 :                                 found = true;
     324                 :            : 
     325   [ +  +  +  + ]:     366555 :                                 if (seen[decidx]) {
     326                 :            :                                         /* duplicate field name */
     327                 :          6 :                                         invalid = true;
     328   [ -  +  -  + ]:          6 :                                         SPDK_JSON_DEBUG("Duplicate key '%s'\n", dec->name);
     329                 :            :                                 } else {
     330                 :     366549 :                                         seen[decidx] = true;
     331         [ +  + ]:     366549 :                                         if (dec->decode_func(v, field)) {
     332                 :          8 :                                                 invalid = true;
     333   [ -  +  -  + ]:          8 :                                                 SPDK_JSON_DEBUG("Decoder failed to decode key '%s'\n", dec->name);
     334                 :            :                                                 /* keep going to fill out any other valid keys */
     335                 :            :                                         }
     336                 :            :                                 }
     337                 :     366555 :                                 break;
     338                 :            :                         }
     339                 :            :                 }
     340                 :            : 
     341   [ +  +  +  + ]:     373474 :                 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                 :            :                 }
     345                 :            : 
     346                 :     373474 :                 i += 1 + spdk_json_val_len(v);
     347                 :            :         }
     348                 :            : 
     349         [ +  + ]:     618166 :         for (decidx = 0; decidx < num_decoders; decidx++) {
     350   [ +  +  +  +  :     499900 :                 if (!decoders[decidx].optional && !seen[decidx]) {
             -  +  +  + ]
     351                 :            :                         /* required field is missing */
     352                 :         18 :                         invalid = true;
     353                 :         18 :                         break;
     354                 :            :                 }
     355                 :            :         }
     356                 :            : 
     357                 :     118284 :         free(seen);
     358         [ +  + ]:     118284 :         return invalid ? -1 : 0;
     359                 :            : }
     360                 :            : 
     361                 :            : void
     362                 :          6 : spdk_json_free_object(const struct spdk_json_object_decoder *decoders, size_t num_decoders,
     363                 :            :                       void *obj)
     364                 :            : {
     365                 :          6 :         struct spdk_json_val invalid_val = {
     366                 :            :                 .start = "",
     367                 :            :                 .len = 0,
     368                 :            :                 .type = SPDK_JSON_VAL_INVALID
     369                 :            :         };
     370                 :            :         size_t decidx;
     371                 :            : 
     372         [ +  + ]:         30 :         for (decidx = 0; decidx < num_decoders; decidx++) {
     373                 :         24 :                 const struct spdk_json_object_decoder *dec = &decoders[decidx];
     374                 :         24 :                 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                 :         24 :                 dec->decode_func(&invalid_val, field);
     380                 :            :         }
     381                 :          6 : }
     382                 :            : 
     383                 :            : 
     384                 :            : int
     385                 :     111854 : 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                 :     111854 :         return _json_decode_object(values, decoders, num_decoders, out, false);
     389                 :            : }
     390                 :            : 
     391                 :            : int
     392                 :       6436 : 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                 :       6436 :         return _json_decode_object(values, decoders, num_decoders, out, true);
     396                 :            : }
     397                 :            : 
     398                 :            : int
     399                 :       3362 : 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   [ +  -  +  + ]:       3362 :         if (values == NULL || values->type != SPDK_JSON_VAL_ARRAY_BEGIN) {
     406                 :          6 :                 return -1;
     407                 :            :         }
     408                 :            : 
     409                 :       3356 :         *out_size = 0;
     410                 :       3356 :         field = out;
     411         [ +  + ]:      10449 :         for (i = 0; i < values->len;) {
     412                 :       7111 :                 const struct spdk_json_val *v = &values[i + 1];
     413                 :            : 
     414         [ +  + ]:       7111 :                 if (*out_size == max_size) {
     415                 :          6 :                         return -1;
     416                 :            :                 }
     417                 :            : 
     418         [ +  + ]:       7105 :                 if (decode_func(v, field)) {
     419                 :         12 :                         return -1;
     420                 :            :                 }
     421                 :            : 
     422                 :       7093 :                 i += spdk_json_val_len(v);
     423                 :       7093 :                 field += stride;
     424                 :       7093 :                 (*out_size)++;
     425                 :            :         }
     426                 :            : 
     427                 :       3338 :         return 0;
     428                 :            : }
     429                 :            : 
     430                 :            : int
     431                 :      25591 : spdk_json_decode_bool(const struct spdk_json_val *val, void *out)
     432                 :            : {
     433                 :      25591 :         bool *f = out;
     434                 :            : 
     435   [ +  +  +  + ]:      25591 :         if (val->type != SPDK_JSON_VAL_TRUE && val->type != SPDK_JSON_VAL_FALSE) {
     436                 :          6 :                 return -1;
     437                 :            :         }
     438                 :            : 
     439                 :      25585 :         *f = val->type == SPDK_JSON_VAL_TRUE;
     440                 :      25585 :         return 0;
     441                 :            : }
     442                 :            : 
     443                 :            : int
     444                 :        534 : spdk_json_decode_uint8(const struct spdk_json_val *val, void *out)
     445                 :            : {
     446                 :        534 :         uint8_t *i = out;
     447                 :            : 
     448                 :        534 :         return spdk_json_number_to_uint8(val, i);
     449                 :            : }
     450                 :            : 
     451                 :            : int
     452                 :        602 : spdk_json_decode_uint16(const struct spdk_json_val *val, void *out)
     453                 :            : {
     454                 :        602 :         uint16_t *i = out;
     455                 :            : 
     456                 :        602 :         return spdk_json_number_to_uint16(val, i);
     457                 :            : }
     458                 :            : 
     459                 :            : int
     460                 :       2316 : spdk_json_decode_int32(const struct spdk_json_val *val, void *out)
     461                 :            : {
     462                 :       2316 :         int32_t *i = out;
     463                 :            : 
     464                 :       2316 :         return spdk_json_number_to_int32(val, i);
     465                 :            : }
     466                 :            : 
     467                 :            : int
     468                 :      18992 : spdk_json_decode_uint32(const struct spdk_json_val *val, void *out)
     469                 :            : {
     470                 :      18992 :         uint32_t *i = out;
     471                 :            : 
     472                 :      18992 :         return spdk_json_number_to_uint32(val, i);
     473                 :            : }
     474                 :            : 
     475                 :            : int
     476                 :       7910 : spdk_json_decode_uint64(const struct spdk_json_val *val, void *out)
     477                 :            : {
     478                 :       7910 :         uint64_t *i = out;
     479                 :            : 
     480                 :       7910 :         return spdk_json_number_to_uint64(val, i);
     481                 :            : }
     482                 :            : 
     483                 :            : int
     484                 :      75696 : spdk_json_decode_string(const struct spdk_json_val *val, void *out)
     485                 :            : {
     486                 :      75696 :         char **s = out;
     487                 :            : 
     488                 :      75696 :         free(*s);
     489                 :            : 
     490                 :      75696 :         *s = spdk_json_strdup(val);
     491                 :            : 
     492         [ +  + ]:      75696 :         if (*s) {
     493                 :      75654 :                 return 0;
     494                 :            :         } else {
     495                 :         42 :                 return -1;
     496                 :            :         }
     497                 :            : }
     498                 :            : 
     499                 :            : int
     500                 :       1423 : spdk_json_decode_uuid(const struct spdk_json_val *val, void *out)
     501                 :            : {
     502                 :       1423 :         struct spdk_uuid *uuid = out;
     503                 :       1423 :         char *str = NULL;
     504                 :            :         int rc;
     505                 :            : 
     506                 :       1423 :         rc = spdk_json_decode_string(val, &str);
     507         [ +  + ]:       1423 :         if (rc != 0) {
     508                 :          6 :                 return rc;
     509                 :            :         }
     510                 :            : 
     511                 :       1417 :         rc = spdk_uuid_parse(uuid, str);
     512                 :       1417 :         free(str);
     513                 :            : 
     514         [ +  + ]:       1417 :         return rc == 0 ? 0 : -1;
     515                 :            : }
     516                 :            : 
     517                 :            : static struct spdk_json_val *
     518                 :       8658 : 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         [ -  + ]:       8658 :         assert((type & (SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN)) != 0);
     522                 :            : 
     523         [ -  + ]:       8658 :         assert(object != NULL);
     524                 :            : 
     525         [ +  + ]:       8658 :         if ((object->type & type) == 0) {
     526                 :        110 :                 return NULL;
     527                 :            :         }
     528                 :            : 
     529                 :       8548 :         object++;
     530         [ -  + ]:       8548 :         if (object->len == 0) {
     531                 :          0 :                 return NULL;
     532                 :            :         }
     533                 :            : 
     534                 :       8548 :         return object;
     535                 :            : }
     536                 :            : 
     537                 :            : static struct spdk_json_val *
     538                 :       4616 : json_value(struct spdk_json_val *key)
     539                 :            : {
     540         [ +  - ]:       4616 :         return key->type == SPDK_JSON_VAL_NAME ? key + 1 : NULL;
     541                 :            : }
     542                 :            : 
     543                 :            : int
     544                 :       2290 : 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                 :       2290 :         struct spdk_json_val *_key = NULL;
     548                 :       2290 :         struct spdk_json_val *_val = NULL;
     549                 :            :         struct spdk_json_val *it_first, *it;
     550                 :            : 
     551         [ -  + ]:       2290 :         assert(object != NULL);
     552                 :            : 
     553                 :       2290 :         it_first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN);
     554         [ +  + ]:       2290 :         if (!it_first) {
     555   [ -  +  -  + ]:         30 :                 SPDK_JSON_DEBUG("Not enclosed in {}\n");
     556                 :         30 :                 return -EPROTOTYPE;
     557                 :            :         }
     558                 :            : 
     559         [ +  + ]:       2597 :         for (it = it_first;
     560         [ +  + ]:       3911 :              it != NULL;
     561                 :       2314 :              it = spdk_json_next(it)) {
     562         [ -  + ]:       2344 :                 if (it->type != SPDK_JSON_VAL_NAME) {
     563                 :          0 :                         continue;
     564                 :            :                 }
     565                 :            : 
     566         [ +  + ]:       2344 :                 if (spdk_json_strequal(it, key_name) != true) {
     567                 :         90 :                         continue;
     568                 :            :                 }
     569                 :            : 
     570         [ -  + ]:       2254 :                 if (_key) {
     571   [ #  #  #  # ]:          0 :                         SPDK_JSON_DEBUG("Duplicate key '%s'", key_name);
     572                 :          0 :                         return -EINVAL;
     573                 :            :                 }
     574                 :            : 
     575                 :       2254 :                 _key = it;
     576                 :       2254 :                 _val = json_value(_key);
     577                 :            : 
     578   [ +  +  +  + ]:       2254 :                 if (type != SPDK_JSON_VAL_ANY && (_val->type & type) == 0) {
     579   [ -  +  -  + ]:         30 :                         SPDK_JSON_DEBUG("key '%s' type is %#x but expected one of %#x\n", key_name, _val->type, type);
     580                 :         30 :                         return -EDOM;
     581                 :            :                 }
     582                 :            :         }
     583                 :            : 
     584         [ +  + ]:       2230 :         if (key) {
     585                 :         30 :                 *key = _key;
     586                 :            :         }
     587                 :            : 
     588         [ +  - ]:       2230 :         if (val) {
     589                 :       2230 :                 *val = _val;
     590                 :            :         }
     591                 :            : 
     592         [ +  + ]:       2230 :         return _val ? 0 : -ENOENT;
     593                 :            : }
     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                 :       2248 : 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                 :       2248 :         return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_ARRAY_BEGIN);
     607                 :            : }
     608                 :            : 
     609                 :            : struct spdk_json_val *
     610                 :         12 : spdk_json_object_first(struct spdk_json_val *object)
     611                 :            : {
     612                 :         12 :         struct spdk_json_val *first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN);
     613                 :            : 
     614                 :            :         /* Empty object? */
     615   [ +  -  +  - ]:         12 :         return first && first->type != SPDK_JSON_VAL_OBJECT_END ? first : NULL;
     616                 :            : }
     617                 :            : 
     618                 :            : struct spdk_json_val *
     619                 :       6356 : spdk_json_array_first(struct spdk_json_val *array_begin)
     620                 :            : {
     621                 :       6356 :         struct spdk_json_val *first = json_first(array_begin, SPDK_JSON_VAL_ARRAY_BEGIN);
     622                 :            : 
     623                 :            :         /* Empty array? */
     624   [ +  +  +  + ]:       6356 :         return first && first->type != SPDK_JSON_VAL_ARRAY_END ? first : NULL;
     625                 :            : }
     626                 :            : 
     627                 :            : static struct spdk_json_val *
     628                 :      16194 : 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         [ +  + ]:      16194 :         if (val->type == SPDK_JSON_VAL_OBJECT_BEGIN) {
     635                 :      13904 :                 end_type = SPDK_JSON_VAL_OBJECT_END;
     636         [ +  - ]:       2290 :         } else if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN) {
     637                 :       2290 :                 end_type = SPDK_JSON_VAL_ARRAY_END;
     638                 :            :         } 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                 :      16194 :         lvl = 1;
     645   [ +  -  +  + ]:     578887 :         for (it = val + 1; it->type != SPDK_JSON_VAL_INVALID && lvl != 0; it++) {
     646         [ +  + ]:     562693 :                 if (it->type == val->type) {
     647                 :      32392 :                         lvl++;
     648         [ +  + ]:     530301 :                 } else if (it->type == end_type) {
     649                 :      48586 :                         lvl--;
     650                 :            :                 }
     651                 :            :         }
     652                 :            : 
     653                 :            :         /* if lvl != 0 we have invalid JSON object */
     654         [ -  + ]:      16194 :         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                 :            :         }
     658                 :            : 
     659                 :      16194 :         return it;
     660                 :            : }
     661                 :            : 
     662                 :            : struct spdk_json_val *
     663                 :      18598 : spdk_json_next(struct spdk_json_val *it)
     664                 :            : {
     665                 :            :         struct spdk_json_val *val, *next;
     666                 :            : 
     667   [ +  +  +  -  :      18598 :         switch (it->type) {
                      - ]
     668                 :       2344 :         case SPDK_JSON_VAL_NAME:
     669                 :       2344 :                 val = json_value(it);
     670                 :       2344 :                 next = spdk_json_next(val);
     671                 :       2344 :                 break;
     672                 :            : 
     673                 :            :         /* We are in the middle of an array - get to next entry */
     674                 :         60 :         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                 :         60 :                 val = it + 1;
     680                 :         60 :                 return val;
     681                 :            : 
     682                 :      16194 :         case SPDK_JSON_VAL_ARRAY_BEGIN:
     683                 :            :         case SPDK_JSON_VAL_OBJECT_BEGIN:
     684                 :      16194 :                 next = json_skip_object_or_array(it);
     685                 :      16194 :                 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         [ +  + ]:      18538 :         if (next == NULL) {
     700                 :       2242 :                 return NULL;
     701                 :            :         }
     702                 :            : 
     703         [ +  + ]:      16296 :         switch (next->type) {
     704                 :       7064 :         case SPDK_JSON_VAL_ARRAY_END:
     705                 :            :         case SPDK_JSON_VAL_OBJECT_END:
     706                 :            :         case SPDK_JSON_VAL_INVALID:
     707                 :       7064 :                 return NULL;
     708                 :       9232 :         default:
     709                 :            :                 /* Next value */
     710                 :       9232 :                 return next;
     711                 :            :         }
     712                 :            : }
     713                 :            : 
     714                 :       3599 : SPDK_LOG_REGISTER_COMPONENT(json_util)

Generated by: LCOV version 1.14