LCOV - code coverage report
Current view: top level - src/univalue/lib - univalue.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 52 145 35.9 %
Date: 2023-11-10 23:46:46 Functions: 10 25 40.0 %
Branches: 16 107 15.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2014 BitPay Inc.
       2                 :            : // Copyright 2015 Bitcoin Core Developers
       3                 :            : // Distributed under the MIT software license, see the accompanying
       4                 :            : // file COPYING or https://opensource.org/licenses/mit-license.php.
       5                 :            : 
       6                 :            : #include <univalue.h>
       7                 :            : 
       8                 :            : #include <iomanip>
       9                 :            : #include <map>
      10                 :            : #include <memory>
      11                 :            : #include <sstream>
      12                 :            : #include <string>
      13                 :            : #include <utility>
      14                 :            : #include <vector>
      15                 :            : 
      16                 :          2 : const UniValue NullUniValue;
      17                 :            : 
      18                 :        117 : void UniValue::clear()
      19                 :            : {
      20                 :        117 :     typ = VNULL;
      21                 :        117 :     val.clear();
      22                 :        117 :     keys.clear();
      23                 :        117 :     values.clear();
      24                 :        117 : }
      25                 :            : 
      26                 :          0 : void UniValue::setNull()
      27                 :            : {
      28                 :          0 :     clear();
      29                 :          0 : }
      30                 :            : 
      31                 :         47 : void UniValue::setBool(bool val_)
      32                 :            : {
      33                 :         47 :     clear();
      34                 :         47 :     typ = VBOOL;
      35         [ +  + ]:         47 :     if (val_)
      36                 :         20 :         val = "1";
      37                 :         47 : }
      38                 :            : 
      39                 :         36 : static bool validNumStr(const std::string& s)
      40                 :            : {
      41                 :         36 :     std::string tokenVal;
      42                 :            :     unsigned int consumed;
      43         [ +  - ]:         36 :     enum jtokentype tt = getJsonToken(tokenVal, consumed, s.data(), s.data() + s.size());
      44                 :         36 :     return (tt == JTOK_NUMBER);
      45                 :         36 : }
      46                 :            : 
      47                 :         36 : void UniValue::setNumStr(std::string str)
      48                 :            : {
      49         [ +  - ]:         36 :     if (!validNumStr(str)) {
      50 [ #  # ][ #  # ]:          0 :         throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
         [ #  # ][ #  # ]
                 [ #  # ]
      51                 :            :     }
      52                 :            : 
      53                 :         36 :     clear();
      54                 :         36 :     typ = VNUM;
      55                 :         36 :     val = std::move(str);
      56                 :         36 : }
      57                 :            : 
      58                 :          4 : void UniValue::setInt(uint64_t val_)
      59                 :            : {
      60                 :          4 :     std::ostringstream oss;
      61                 :            : 
      62         [ +  - ]:          4 :     oss << val_;
      63                 :            : 
      64 [ +  - ][ -  + ]:          4 :     return setNumStr(oss.str());
      65                 :          4 : }
      66                 :            : 
      67                 :         30 : void UniValue::setInt(int64_t val_)
      68                 :            : {
      69                 :         30 :     std::ostringstream oss;
      70                 :            : 
      71         [ +  - ]:         30 :     oss << val_;
      72                 :            : 
      73 [ +  - ][ -  + ]:         30 :     return setNumStr(oss.str());
      74                 :         30 : }
      75                 :            : 
      76                 :          2 : void UniValue::setFloat(double val_)
      77                 :            : {
      78                 :          2 :     std::ostringstream oss;
      79                 :            : 
      80 [ +  - ][ +  - ]:          2 :     oss << std::setprecision(16) << val_;
                 [ +  - ]
      81                 :            : 
      82 [ +  - ][ -  + ]:          2 :     return setNumStr(oss.str());
      83                 :          2 : }
      84                 :            : 
      85                 :         34 : void UniValue::setStr(std::string str)
      86                 :            : {
      87                 :         34 :     clear();
      88                 :         34 :     typ = VSTR;
      89                 :         34 :     val = std::move(str);
      90                 :         34 : }
      91                 :            : 
      92                 :          0 : void UniValue::setArray()
      93                 :            : {
      94                 :          0 :     clear();
      95                 :          0 :     typ = VARR;
      96                 :          0 : }
      97                 :            : 
      98                 :          0 : void UniValue::setObject()
      99                 :            : {
     100                 :          0 :     clear();
     101                 :          0 :     typ = VOBJ;
     102                 :          0 : }
     103                 :            : 
     104                 :          0 : void UniValue::push_back(UniValue val)
     105                 :            : {
     106                 :          0 :     checkType(VARR);
     107                 :            : 
     108                 :          0 :     values.push_back(std::move(val));
     109                 :          0 : }
     110                 :            : 
     111                 :          0 : void UniValue::push_backV(const std::vector<UniValue>& vec)
     112                 :            : {
     113                 :          0 :     checkType(VARR);
     114                 :            : 
     115                 :          0 :     values.insert(values.end(), vec.begin(), vec.end());
     116                 :          0 : }
     117                 :            : 
     118                 :          1 : void UniValue::pushKVEnd(std::string key, UniValue val)
     119                 :            : {
     120                 :          1 :     checkType(VOBJ);
     121                 :            : 
     122                 :          1 :     keys.push_back(std::move(key));
     123                 :          1 :     values.push_back(std::move(val));
     124                 :          1 : }
     125                 :            : 
     126                 :          0 : void UniValue::pushKV(std::string key, UniValue val)
     127                 :            : {
     128                 :          0 :     checkType(VOBJ);
     129                 :            : 
     130                 :            :     size_t idx;
     131         [ #  # ]:          0 :     if (findKey(key, idx))
     132                 :          0 :         values[idx] = std::move(val);
     133                 :            :     else
     134         [ #  # ]:          0 :         pushKVEnd(std::move(key), std::move(val));
     135                 :          0 : }
     136                 :            : 
     137                 :          0 : void UniValue::pushKVs(UniValue obj)
     138                 :            : {
     139                 :          0 :     checkType(VOBJ);
     140                 :          0 :     obj.checkType(VOBJ);
     141                 :            : 
     142         [ #  # ]:          0 :     for (size_t i = 0; i < obj.keys.size(); i++)
     143 [ #  # ][ #  # ]:          0 :         pushKVEnd(std::move(obj.keys.at(i)), std::move(obj.values.at(i)));
     144                 :          0 : }
     145                 :            : 
     146                 :          0 : void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
     147                 :            : {
     148         [ #  # ]:          0 :     if (typ != VOBJ)
     149                 :          0 :         return;
     150                 :            : 
     151                 :          0 :     kv.clear();
     152         [ #  # ]:          0 :     for (size_t i = 0; i < keys.size(); i++)
     153                 :          0 :         kv[keys[i]] = values[i];
     154                 :          0 : }
     155                 :            : 
     156                 :          0 : bool UniValue::findKey(const std::string& key, size_t& retIdx) const
     157                 :            : {
     158         [ #  # ]:          0 :     for (size_t i = 0; i < keys.size(); i++) {
     159         [ #  # ]:          0 :         if (keys[i] == key) {
     160                 :          0 :             retIdx = i;
     161                 :          0 :             return true;
     162                 :            :         }
     163                 :          0 :     }
     164                 :            : 
     165                 :          0 :     return false;
     166                 :          0 : }
     167                 :            : 
     168                 :          0 : bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
     169                 :            : {
     170         [ #  # ]:          0 :     if (typ != VOBJ) {
     171                 :          0 :         return false;
     172                 :            :     }
     173                 :            : 
     174         [ #  # ]:          0 :     for (const auto& object: t) {
     175                 :          0 :         size_t idx = 0;
     176         [ #  # ]:          0 :         if (!findKey(object.first, idx)) {
     177                 :          0 :             return false;
     178                 :            :         }
     179                 :            : 
     180         [ #  # ]:          0 :         if (values.at(idx).getType() != object.second) {
     181                 :          0 :             return false;
     182                 :            :         }
     183                 :            :     }
     184                 :            : 
     185                 :          0 :     return true;
     186                 :          0 : }
     187                 :            : 
     188                 :          0 : const UniValue& UniValue::operator[](const std::string& key) const
     189                 :            : {
     190         [ #  # ]:          0 :     if (typ != VOBJ)
     191                 :          0 :         return NullUniValue;
     192                 :            : 
     193                 :          0 :     size_t index = 0;
     194         [ #  # ]:          0 :     if (!findKey(key, index))
     195                 :          0 :         return NullUniValue;
     196                 :            : 
     197                 :          0 :     return values.at(index);
     198                 :          0 : }
     199                 :            : 
     200                 :          0 : const UniValue& UniValue::operator[](size_t index) const
     201                 :            : {
     202 [ #  # ][ #  # ]:          0 :     if (typ != VOBJ && typ != VARR)
     203                 :          0 :         return NullUniValue;
     204         [ #  # ]:          0 :     if (index >= values.size())
     205                 :          0 :         return NullUniValue;
     206                 :            : 
     207                 :          0 :     return values.at(index);
     208                 :          0 : }
     209                 :            : 
     210                 :         12 : void UniValue::checkType(const VType& expected) const
     211                 :            : {
     212         [ +  - ]:         12 :     if (typ != expected) {
     213 [ #  # ][ #  # ]:          0 :         throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     214 [ #  # ][ #  # ]:          0 :                                  std::string{uvTypeName(expected)}};
     215                 :            :     }
     216                 :         12 : }
     217                 :            : 
     218                 :          0 : const char *uvTypeName(UniValue::VType t)
     219                 :            : {
     220   [ #  #  #  #  :          0 :     switch (t) {
                #  #  # ]
     221                 :          0 :     case UniValue::VNULL: return "null";
     222                 :          0 :     case UniValue::VBOOL: return "bool";
     223                 :          0 :     case UniValue::VOBJ: return "object";
     224                 :          0 :     case UniValue::VARR: return "array";
     225                 :          0 :     case UniValue::VSTR: return "string";
     226                 :          0 :     case UniValue::VNUM: return "number";
     227                 :            :     }
     228                 :            : 
     229                 :            :     // not reached
     230                 :          0 :     return nullptr;
     231                 :          0 : }
     232                 :            : 
     233                 :          0 : const UniValue& UniValue::find_value(std::string_view key) const
     234                 :            : {
     235         [ #  # ]:          0 :     for (unsigned int i = 0; i < keys.size(); ++i) {
     236         [ #  # ]:          0 :         if (keys[i] == key) {
     237                 :          0 :             return values.at(i);
     238                 :            :         }
     239                 :          0 :     }
     240                 :          0 :     return NullUniValue;
     241                 :          0 : }
     242                 :            : 

Generated by: LCOV version 1.14