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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

82.2
/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) {
61,303,980✔
27
  double val = 0, temp = (double)INT64_MAX;
61,303,980✔
28
  char*  endPtr;
60,591,705✔
29
  bool   useDouble = false;
61,303,980✔
30
  SET_ERRNO(0);
61,303,980✔
31
  int64_t int64Val = taosStr2Int64(str, &endPtr, 0);
61,303,980✔
32
  if (*endPtr == '.' || ERRNO == ERANGE) {
61,303,980✔
33
    SET_ERRNO(0);
1,223✔
34
    val = taosStr2Double(str, &endPtr);
1,223✔
35
    useDouble = true;
1,223✔
36
  }
37
  if (endPtr == str || ERRNO == ERANGE || isnan(val)) {
61,303,980!
38
    return terrno = TSDB_CODE_INVALID_CFG_VALUE;
121✔
39
  }
40
  while (isspace((unsigned char)*endPtr)) endPtr++;
61,304,101✔
41
  uint64_t factor = 1;
61,303,859✔
42
  if (*endPtr != '\0') {
61,303,859✔
43
    switch (*endPtr) {
2,387!
44
      case 'P':
×
45
      case 'p': {
46
        temp /= UNIT_ONE_PEBIBYTE;
×
47
        factor = UNIT_ONE_PEBIBYTE;
×
48
      } break;
×
49
      case 'T':
134✔
50
      case 't': {
51
        temp /= UNIT_ONE_TEBIBYTE;
134✔
52
        factor = UNIT_ONE_TEBIBYTE;
134✔
53
      } break;
134✔
54
      case 'G':
402✔
55
      case 'g': {
56
        temp /= UNIT_ONE_GIBIBYTE;
402✔
57
        factor = UNIT_ONE_GIBIBYTE;
402✔
58
      } break;
402✔
59
      case 'M':
268✔
60
      case 'm': {
61
        temp /= UNIT_ONE_MEBIBYTE;
268✔
62
        factor = UNIT_ONE_MEBIBYTE;
268✔
63
      } break;
268✔
64
      case 'K':
1,220✔
65
      case 'k': {
66
        temp /= UNIT_ONE_KIBIBYTE;
1,220✔
67
        factor = UNIT_ONE_KIBIBYTE;
1,220✔
68
      } break;
1,220✔
69
      default:
363✔
70
        return terrno = TSDB_CODE_INVALID_CFG_VALUE;
363✔
71
    }
72
    endPtr++;
2,024✔
73

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

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

104
int32_t taosStrHumanToInt32(const char* str, int32_t* out) {
61,295,424✔
105
  int64_t res;
60,584,095✔
106
  int32_t code = parseCfgIntWithUnit(str, &res);
61,295,424✔
107
  if (code == TSDB_CODE_SUCCESS) {
61,295,424✔
108
    if (res < INT32_MIN || res > INT32_MAX) {
61,294,940!
109
      return terrno = TSDB_CODE_OUT_OF_RANGE;
6,186✔
110
    }
111
    *out = (int32_t)res;
61,288,754✔
112
  }
113
  return code;
61,289,238✔
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