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

taosdata / TDengine / #4829

30 Oct 2025 09:25AM UTC coverage: 49.734% (-11.3%) from 61.071%
#4829

push

travis-ci

web-flow
Merge pull request #33435 from taosdata/3.0

merge 3.0

123072 of 323930 branches covered (37.99%)

Branch coverage included in aggregate %.

7 of 25 new or added lines in 3 files covered. (28.0%)

35232 existing lines in 327 files now uncovered.

172062 of 269495 relevant lines covered (63.85%)

70709785.06 hits per line

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

48.31
/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) {
12,102,944✔
27
  double val = 0, temp = (double)INT64_MAX;
12,102,944✔
28
  char*  endPtr;
12,051,243✔
29
  bool   useDouble = false;
12,102,944✔
30
  SET_ERRNO(0);
12,102,944✔
31
  int64_t int64Val = taosStr2Int64(str, &endPtr, 0);
12,102,944✔
32
  if (*endPtr == '.' || ERRNO == ERANGE) {
12,102,944!
UNCOV
33
    SET_ERRNO(0);
×
UNCOV
34
    val = taosStr2Double(str, &endPtr);
×
UNCOV
35
    useDouble = true;
×
36
  }
37
  if (endPtr == str || ERRNO == ERANGE || isnan(val)) {
12,102,944!
UNCOV
38
    return terrno = TSDB_CODE_INVALID_CFG_VALUE;
×
39
  }
40
  while (isspace((unsigned char)*endPtr)) endPtr++;
12,102,944!
41
  uint64_t factor = 1;
12,102,944✔
42
  if (*endPtr != '\0') {
12,102,944✔
43
    switch (*endPtr) {
145!
44
      case 'P':
×
45
      case 'p': {
46
        temp /= UNIT_ONE_PEBIBYTE;
×
47
        factor = UNIT_ONE_PEBIBYTE;
×
48
      } break;
×
UNCOV
49
      case 'T':
×
50
      case 't': {
UNCOV
51
        temp /= UNIT_ONE_TEBIBYTE;
×
UNCOV
52
        factor = UNIT_ONE_TEBIBYTE;
×
UNCOV
53
      } break;
×
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':
145✔
65
      case 'k': {
66
        temp /= UNIT_ONE_KIBIBYTE;
145✔
67
        factor = UNIT_ONE_KIBIBYTE;
145✔
68
      } break;
145✔
UNCOV
69
      default:
×
UNCOV
70
        return terrno = TSDB_CODE_INVALID_CFG_VALUE;
×
71
    }
72
    endPtr++;
145✔
73

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

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

104
int32_t taosStrHumanToInt32(const char* str, int32_t* out) {
12,101,526✔
105
  int64_t res;
12,050,115✔
106
  int32_t code = parseCfgIntWithUnit(str, &res);
12,101,526✔
107
  if (code == TSDB_CODE_SUCCESS) {
12,101,526!
108
    if (res < INT32_MIN || res > INT32_MAX) {
12,101,526!
109
      return terrno = TSDB_CODE_OUT_OF_RANGE;
282✔
110
    }
111
    *out = (int32_t)res;
12,101,244✔
112
  }
113
  return code;
12,101,244✔
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