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

taosdata / TDengine / #4113

17 May 2025 06:43AM UTC coverage: 62.054% (-0.8%) from 62.857%
#4113

push

travis-ci

web-flow
Merge pull request #31115 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

154737 of 318088 branches covered (48.65%)

Branch coverage included in aggregate %.

175 of 225 new or added lines in 20 files covered. (77.78%)

5853 existing lines in 216 files now uncovered.

239453 of 317147 relevant lines covered (75.5%)

15121865.73 hits per line

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

54.87
/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) {
286,901✔
27
  double val = 0, temp = (double)INT64_MAX;
286,901✔
28
  char*  endPtr;
29
  bool   useDouble = false;
286,901✔
30
  SET_ERRNO(0);
286,901✔
31
  int64_t int64Val = taosStr2Int64(str, &endPtr, 0);
286,901✔
32
  if (*endPtr == '.' || ERRNO == ERANGE) {
286,901!
UNCOV
33
    SET_ERRNO(0);
×
UNCOV
34
    val = taosStr2Double(str, &endPtr);
×
UNCOV
35
    useDouble = true;
×
36
  }
37
  if (endPtr == str || ERRNO == ERANGE || isnan(val)) {
286,901!
UNCOV
38
    return terrno = TSDB_CODE_INVALID_CFG_VALUE;
×
39
  }
40
  while (isspace((unsigned char)*endPtr)) endPtr++;
286,901!
41
  uint64_t factor = 1;
286,901✔
42
  if (*endPtr != '\0') {
286,901✔
43
    switch (*endPtr) {
15!
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✔
54
      case 'G':
×
55
      case 'g': {
56
        temp /= UNIT_ONE_GIBIBYTE;
×
57
        factor = UNIT_ONE_GIBIBYTE;
×
58
      } break;
×
59
      case 'M':
×
60
      case 'm': {
61
        temp /= UNIT_ONE_MEBIBYTE;
×
62
        factor = UNIT_ONE_MEBIBYTE;
×
63
      } break;
×
64
      case 'K':
5✔
65
      case 'k': {
66
        temp /= UNIT_ONE_KIBIBYTE;
5✔
67
        factor = UNIT_ONE_KIBIBYTE;
5✔
68
      } break;
5✔
UNCOV
69
      default:
×
UNCOV
70
        return terrno = TSDB_CODE_INVALID_CFG_VALUE;
×
71
    }
72
    endPtr++;
15✔
73

74
    if ((val > 0 && val > temp) || (val < 0 && val < -temp)) {
15!
75
      return terrno = TSDB_CODE_OUT_OF_RANGE;
×
76
    }
77
    val *= factor;
15✔
78
    int64Val *= factor;
15✔
79
  }
80
  while (isspace((unsigned char)*endPtr)) endPtr++;
286,901!
81
  if (*endPtr) {
286,901!
82
    return terrno = TSDB_CODE_INVALID_CFG_VALUE;
×
83
  }
84
  if (useDouble) {
286,901!
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;
286,901✔
93
  }
94
  return TSDB_CODE_SUCCESS;
286,901✔
95
}
96

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

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