• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4041

09 May 2025 07:58AM UTC coverage: 62.508% (-0.3%) from 62.788%
#4041

push

travis-ci

web-flow
enh: update database fetch functions to include status in JSON output (#31005)

155567 of 317611 branches covered (48.98%)

Branch coverage included in aggregate %.

15 of 18 new or added lines in 1 file covered. (83.33%)

3906 existing lines in 185 files now uncovered.

240901 of 316655 relevant lines covered (76.08%)

6304979.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.11
/source/util/src/tunit.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tunit.h"
17

18
#define UNIT_SIZE_CONVERT_FACTOR 1024LL
19
#define UNIT_ONE_KIBIBYTE        UNIT_SIZE_CONVERT_FACTOR
20
#define UNIT_ONE_MEBIBYTE        (UNIT_ONE_KIBIBYTE * UNIT_SIZE_CONVERT_FACTOR)
21
#define UNIT_ONE_GIBIBYTE        (UNIT_ONE_MEBIBYTE * UNIT_SIZE_CONVERT_FACTOR)
22
#define UNIT_ONE_TEBIBYTE        (UNIT_ONE_GIBIBYTE * UNIT_SIZE_CONVERT_FACTOR)
23
#define UNIT_ONE_PEBIBYTE        (UNIT_ONE_TEBIBYTE * UNIT_SIZE_CONVERT_FACTOR)
24
#define UNIT_ONE_EXBIBYTE        (UNIT_ONE_PEBIBYTE * UNIT_SIZE_CONVERT_FACTOR)
25

26
static int32_t parseCfgIntWithUnit(const char* str, int64_t* res) {
236,485✔
27
  double val = 0, temp = (double)INT64_MAX;
236,485✔
28
  char*  endPtr;
29
  bool   useDouble = false;
236,485✔
30
  SET_ERRNO(0);
236,485✔
31
  int64_t int64Val = taosStr2Int64(str, &endPtr, 0);
236,485✔
32
  if (*endPtr == '.' || ERRNO == ERANGE) {
236,485✔
33
    SET_ERRNO(0);
9✔
34
    val = taosStr2Double(str, &endPtr);
9✔
35
    useDouble = true;
9✔
36
  }
37
  if (endPtr == str || ERRNO == ERANGE || isnan(val)) {
236,485!
38
    return terrno = TSDB_CODE_INVALID_CFG_VALUE;
1✔
39
  }
40
  while (isspace((unsigned char)*endPtr)) endPtr++;
236,486✔
41
  uint64_t factor = 1;
236,484✔
42
  if (*endPtr != '\0') {
236,484✔
43
    switch (*endPtr) {
21!
44
      case 'P':
5✔
45
      case 'p': {
46
        temp /= UNIT_ONE_PEBIBYTE;
5✔
47
        factor = UNIT_ONE_PEBIBYTE;
5✔
48
      } break;
5✔
49
      case 'T':
5✔
50
      case 't': {
51
        temp /= UNIT_ONE_TEBIBYTE;
5✔
52
        factor = UNIT_ONE_TEBIBYTE;
5✔
53
      } break;
5✔
UNCOV
54
      case 'G':
×
55
      case 'g': {
UNCOV
56
        temp /= UNIT_ONE_GIBIBYTE;
×
UNCOV
57
        factor = UNIT_ONE_GIBIBYTE;
×
UNCOV
58
      } break;
×
UNCOV
59
      case 'M':
×
60
      case 'm': {
UNCOV
61
        temp /= UNIT_ONE_MEBIBYTE;
×
UNCOV
62
        factor = UNIT_ONE_MEBIBYTE;
×
UNCOV
63
      } break;
×
64
      case 'K':
8✔
65
      case 'k': {
66
        temp /= UNIT_ONE_KIBIBYTE;
8✔
67
        factor = UNIT_ONE_KIBIBYTE;
8✔
68
      } break;
8✔
69
      default:
3✔
70
        return terrno = TSDB_CODE_INVALID_CFG_VALUE;
3✔
71
    }
72
    endPtr++;
18✔
73

74
    if ((val > 0 && val > temp) || (val < 0 && val < -temp)) {
18!
75
      return terrno = TSDB_CODE_OUT_OF_RANGE;
×
76
    }
77
    val *= factor;
18✔
78
    int64Val *= factor;
18✔
79
  }
80
  while (isspace((unsigned char)*endPtr)) endPtr++;
236,481!
81
  if (*endPtr) {
236,481!
82
    return terrno = TSDB_CODE_INVALID_CFG_VALUE;
×
83
  }
84
  if (useDouble) {
236,481✔
85
    val = rint(val);
7✔
86
    if ((val > 0 && val >= (double)INT64_MAX) || (val < 0 && val <= (double)INT64_MIN)) {
7!
87
      return terrno = TSDB_CODE_OUT_OF_RANGE;
5✔
88
    } else {
89
      *res = (int64_t)val;
2✔
90
    }
91
  } else {
92
    *res = int64Val;
236,474✔
93
  }
94
  return TSDB_CODE_SUCCESS;
236,476✔
95
}
96

97
int32_t taosStrHumanToInt64(const char* str, int64_t* out) {
130✔
98
  int64_t res;
99
  int32_t code = parseCfgIntWithUnit(str, &res);
130✔
100
  if (code == TSDB_CODE_SUCCESS) *out = (int64_t)res;
130✔
101
  return code;
130✔
102
}
103

104
int32_t taosStrHumanToInt32(const char* str, int32_t* out) {
236,355✔
105
  int64_t res;
106
  int32_t code = parseCfgIntWithUnit(str, &res);
236,355✔
107
  if (code == TSDB_CODE_SUCCESS) {
236,355✔
108
    if (res < INT32_MIN || res > INT32_MAX) {
236,351!
109
      return terrno = TSDB_CODE_OUT_OF_RANGE;
×
110
    }
111
    *out = (int32_t)res;
236,351✔
112
  }
113
  return code;
236,355✔
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc