• 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

41.41
/include/util/tbuffer.inc
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 "taoserror.h"
17
#include "tcoding.h"
18
#include "tutil.h"
19

20
struct SBuffer {
21
  uint32_t size;
22
  uint32_t capacity;
23
  void    *data;
24
};
25

26
struct SBufferReader {
27
  uint32_t offset;
28
  SBuffer *buffer;
29
};
30

31
// SBuffer
32
static FORCE_INLINE void tBufferInit(SBuffer *buffer) {
33
  buffer->size = 0;
2,129,250,817✔
34
  buffer->capacity = 0;
2,129,371,168✔
35
  buffer->data = NULL;
2,129,344,284✔
36
}
2,129,349,450✔
37

38
static FORCE_INLINE void tBufferDestroy(SBuffer *buffer) {
39
  buffer->size = 0;
2,147,483,647✔
40
  buffer->capacity = 0;
2,147,483,647✔
41
  if (buffer->data) {
2,147,483,647!
42
    taosMemoryFree(buffer->data);
626,378,981!
43
    buffer->data = NULL;
626,281,615✔
44
  }
45
}
2,147,483,647✔
46

47
static FORCE_INLINE void tBufferClear(SBuffer *buffer) { buffer->size = 0; }
1,656,762,344✔
48

49
static FORCE_INLINE int32_t tBufferEnsureCapacity(SBuffer *buffer, uint32_t capacity) {
50
  if (buffer->capacity < capacity) {
2,147,483,647!
51
    uint32_t newCapacity = (buffer->capacity > 0) ? (buffer->capacity << 1) : 1024;
675,159,231!
52
    while (newCapacity < capacity) {
755,033,077!
53
      newCapacity <<= 1;
79,883,125✔
54
    }
55
    void *newData = taosMemoryRealloc(buffer->data, newCapacity);
675,149,952!
56
    if (newData == NULL) {
674,322,861!
57
      return TSDB_CODE_OUT_OF_MEMORY;
×
58
    }
59
    buffer->data = newData;
674,322,861✔
60
    buffer->capacity = newCapacity;
674,577,058✔
61
  }
62
  return 0;
2,147,483,647✔
63
}
64

65
static FORCE_INLINE int32_t tBufferPut(SBuffer *buffer, const void *data, uint32_t size) {
66
  TAOS_CHECK_RETURN(tBufferEnsureCapacity(buffer, buffer->size + size));
2,147,483,647!
67
  memcpy((char *)buffer->data + buffer->size, data, size);
2,147,483,647!
68
  buffer->size += size;
2,147,483,647✔
69
  return 0;
2,147,483,647✔
70
}
71

72
static int32_t tBufferPutAt(SBuffer *buffer, uint32_t offset, const void *data, uint32_t size) {
2,147,483,647✔
73
  if (offset + size > buffer->size) {
2,147,483,647!
74
    return TSDB_CODE_OUT_OF_RANGE;
×
75
  }
76
  memcpy((char *)buffer->data + offset, data, size);
2,147,483,647!
77
  return 0;
2,147,483,647✔
78
}
79

80
static FORCE_INLINE int32_t tBufferPutI8(SBuffer *buffer, int8_t value) {
81
  return tBufferPut(buffer, &value, sizeof(value));
364,662,928✔
82
}
83

84
static FORCE_INLINE int32_t tBufferPutI16(SBuffer *buffer, int16_t value) {
85
  return tBufferPut(buffer, &value, sizeof(value));
86
}
87

88
static FORCE_INLINE int32_t tBufferPutI32(SBuffer *buffer, int32_t value) {
89
  return tBufferPut(buffer, &value, sizeof(value));
138,340,779✔
90
}
91

92
static FORCE_INLINE int32_t tBufferPutI64(SBuffer *buffer, int64_t value) {
93
  return tBufferPut(buffer, &value, sizeof(value));
652,379,769✔
94
}
95

96
static FORCE_INLINE int32_t tBufferPutU8(SBuffer *buffer, uint8_t value) {
97
  return tBufferPut(buffer, &value, sizeof(value));
879,506,946✔
98
}
99

100
static FORCE_INLINE int32_t tBufferPutU16(SBuffer *buffer, uint16_t value) {
101
  return tBufferPut(buffer, &value, sizeof(value));
102
}
103

104
static FORCE_INLINE int32_t tBufferPutU32(SBuffer *buffer, uint32_t value) {
105
  return tBufferPut(buffer, &value, sizeof(value));
143,281,380✔
106
}
107

108
static FORCE_INLINE int32_t tBufferPutU64(SBuffer *buffer, uint64_t value) {
109
  return tBufferPut(buffer, &value, sizeof(value));
44,964✔
110
}
111

112
static FORCE_INLINE int32_t tBufferPutU16v(SBuffer *buffer, uint16_t value) { return tBufferPutU64v(buffer, value); }
113

114
static FORCE_INLINE int32_t tBufferPutU32v(SBuffer *buffer, uint32_t value) { return tBufferPutU64v(buffer, value); }
13,147,344✔
115

116
static FORCE_INLINE int32_t tBufferPutU64v(SBuffer *buffer, uint64_t value) {
117
  while (value >= 0x80) {
879,144,638!
118
    TAOS_CHECK_RETURN(tBufferPutU8(buffer, (value & 0x7F) | 0x80));
541,771,263!
119
    value >>= 7;
270,887,453✔
120
  }
121
  return tBufferPutU8(buffer, value);
1,216,587,683!
122
}
123

124
static FORCE_INLINE int32_t tBufferPutI16v(SBuffer *buffer, int16_t value) {
125
  return tBufferPutU64v(buffer, ZIGZAGE(int16_t, value));
271,254,961✔
126
}
127

128
static FORCE_INLINE int32_t tBufferPutI32v(SBuffer *buffer, int32_t value) {
129
  return tBufferPutU64v(buffer, ZIGZAGE(int32_t, value));
919,043,519✔
130
}
131

132
static FORCE_INLINE int32_t tBufferPutI64v(SBuffer *buffer, int64_t value) {
133
  return tBufferPutU64v(buffer, ZIGZAGE(int64_t, value));
134
}
135

136
static FORCE_INLINE int32_t tBufferPutBinary(SBuffer *buffer, const void *data, uint32_t size) {
137
  TAOS_CHECK_RETURN(tBufferPutU32v(buffer, size));
138
  return tBufferPut(buffer, data, size);
139
}
140

141
static FORCE_INLINE int32_t tBufferPutCStr(SBuffer *buffer, const char *str) {
142
  return tBufferPutBinary(buffer, str, str ? strlen(str) + 1 : 0);
143
}
144

145
static FORCE_INLINE int32_t tBufferPutF32(SBuffer *buffer, float value) {
146
  union {
147
    float    f;
148
    uint32_t u;
149
  } u;
150
  u.f = value;
151
  return tBufferPutU32(buffer, u.u);
152
}
153

154
static FORCE_INLINE int32_t tBufferPutF64(SBuffer *buffer, double value) {
155
  union {
156
    double   f;
157
    uint64_t u;
158
  } u;
159
  u.f = value;
160
  return tBufferPutU64(buffer, u.u);
161
}
162

163
// reader
164
// SBufferReader
UNCOV
165
static int32_t tBufferReaderInit(SBufferReader *reader, uint32_t offset, SBuffer *buffer) {
×
UNCOV
166
  reader->offset = offset;
×
UNCOV
167
  reader->buffer = buffer;
×
UNCOV
168
  return 0;
×
169
}
170

171
static FORCE_INLINE int32_t tBufferGet(SBufferReader *reader, uint32_t size, void *data) {
172
  if (reader->offset + size > reader->buffer->size) {
2,147,483,647!
173
    return TSDB_CODE_OUT_OF_RANGE;
×
174
  }
175
  if (data) {
2,147,483,647!
176
    memcpy(data, BR_PTR(reader), size);
2,147,483,647!
177
  }
178
  reader->offset += size;
2,147,483,647✔
179
  return 0;
2,147,483,647✔
180
}
181

182
static int32_t tBufferGetI8(SBufferReader *reader, int8_t *value) { return tBufferGet(reader, sizeof(*value), value); }
2,147,483,647!
183

UNCOV
184
static int32_t tBufferGetI16(SBufferReader *reader, int16_t *value) {
×
UNCOV
185
  return tBufferGet(reader, sizeof(*value), value);
×
186
}
187

188
static int32_t tBufferGetI32(SBufferReader *reader, int32_t *value) {
762,540,367✔
189
  if (reader->offset + sizeof(int32_t) > reader->buffer->size) {
762,540,367!
190
    return TSDB_CODE_OUT_OF_RANGE;
×
191
  }
192
  if (value) {
762,577,568!
193
    *value = *(int32_t*)BR_PTR(reader);
762,581,663✔
194
  }
195
  reader->offset += sizeof(int32_t);
762,673,543✔
196
  return 0;
762,668,778✔
197
}
198

199
static int32_t tBufferGetI64(SBufferReader *reader, int64_t *value) {
2,147,483,647✔
200
  if (reader->offset + sizeof(int64_t) > reader->buffer->size) {
2,147,483,647!
201
    return TSDB_CODE_OUT_OF_RANGE;
×
202
  }
203
  if (value) {
2,147,483,647!
204
    *value = *(int64_t*)BR_PTR(reader);
2,147,483,647✔
205
  }
206
  reader->offset += sizeof(int64_t);
2,147,483,647✔
207
  return 0;
2,147,483,647✔
208
}
209

210
static int32_t tBufferGetU8(SBufferReader *reader, uint8_t *value) { return tBufferGet(reader, sizeof(*value), value); }
2,147,483,647!
211

UNCOV
212
static int32_t tBufferGetU16(SBufferReader *reader, uint16_t *value) {
×
UNCOV
213
  return tBufferGet(reader, sizeof(*value), value);
×
214
}
215

216
static int32_t tBufferGetU32(SBufferReader *reader, uint32_t *value) {
533,320,111!
217
  return tBufferGet(reader, sizeof(*value), value);
533,386,520✔
218
}
219

220
static int32_t tBufferGetU64(SBufferReader *reader, uint64_t *value) {
666,540!
221
  return tBufferGet(reader, sizeof(*value), value);
666,540✔
222
}
223

224
static int32_t tBufferGetU64v(SBufferReader *reader, uint64_t *value) {
2,147,483,647✔
225
  uint8_t  byte;
2,147,483,647✔
226
  int32_t  code;
227
  uint64_t u64 = 0;
2,147,483,647✔
228

229
  for (int32_t i = 0;; i++) {
2,147,483,647✔
230
    code = tBufferGetU8(reader, &byte);
2,147,483,647✔
231
    if (code) return code;
2,147,483,647!
232

233
    u64 |= (((uint64_t)(byte & 0x7F)) << (i * 7));
2,147,483,647!
234

235
    if (byte < 0x80) {
2,147,483,647✔
236
      break;
2,147,483,647✔
237
    }
238
  }
239

240
  if (value) {
2,147,483,647✔
241
    *value = u64;
2,147,483,647✔
242
  }
243

244
  return 0;
2,147,483,647✔
245
}
246

247
static int32_t tBufferGetU16v(SBufferReader *reader, uint16_t *value) {
416,376,105✔
248
  uint64_t u64;
416,376,105✔
249
  int32_t  code = tBufferGetU64v(reader, &u64);
416,396,656✔
250
  if (code) return code;
416,497,097!
251
  if (value) {
416,497,097✔
252
    *value = (uint16_t)u64;
416,463,824✔
253
  }
254
  return 0;
416,467,057✔
255
}
256

257
static int32_t tBufferGetU32v(SBufferReader *reader, uint32_t *value) {
1,749,564,879✔
258
  uint64_t u64;
1,749,564,879✔
259
  int32_t  code = tBufferGetU64v(reader, &u64);
1,749,604,561✔
260
  if (code) return code;
1,749,933,163!
261
  if (value) {
1,749,933,163✔
262
    *value = (uint32_t)u64;
1,750,053,085✔
263
  }
264
  return 0;
1,749,941,018✔
265
}
266

267
static int32_t tBufferGetI16v(SBufferReader *reader, int16_t *value) {
416,360,149✔
268
  uint16_t u16;
416,360,149✔
269
  int32_t  code = tBufferGetU16v(reader, &u16);
416,373,598✔
270
  if (code) return code;
416,483,754!
271
  if (value) {
416,483,754✔
272
    *value = ZIGZAGD(int16_t, u16);
416,461,370✔
273
  }
274
  return 0;
416,464,829✔
275
}
276

277
static int32_t tBufferGetI32v(SBufferReader *reader, int32_t *value) {
1,684,932,546✔
278
  uint32_t u32;
1,684,932,546✔
279
  int32_t  code = tBufferGetU32v(reader, &u32);
1,684,934,697✔
280
  if (code) return code;
1,685,529,677!
281
  if (value) {
1,685,529,677✔
282
    *value = ZIGZAGD(int32_t, u32);
1,685,448,958✔
283
  }
284
  return 0;
1,685,411,811✔
285
}
286

UNCOV
287
static int32_t tBufferGetI64v(SBufferReader *reader, int64_t *value) {
×
288
  uint64_t u64;
×
UNCOV
289
  int32_t  code = tBufferGetU64v(reader, &u64);
×
UNCOV
290
  if (code) return code;
×
UNCOV
291
  if (value) {
×
UNCOV
292
    *value = ZIGZAGD(int64_t, u64);
×
293
  }
UNCOV
294
  return 0;
×
295
}
296

UNCOV
297
static int32_t tBufferGetBinary(SBufferReader *reader, const void **data, uint32_t *size) {
×
298
  uint32_t tmpSize;
×
299
  int32_t  code;
300

301
  // size
UNCOV
302
  code = tBufferGetU32v(reader, &tmpSize);
×
UNCOV
303
  if (code) return code;
×
UNCOV
304
  if (size) {
×
UNCOV
305
    *size = tmpSize;
×
306
  }
307

308
  // data
UNCOV
309
  if (tmpSize > 0) {
×
UNCOV
310
    if (reader->offset + tmpSize > reader->buffer->size) {
×
311
      return TSDB_CODE_OUT_OF_RANGE;
×
312
    }
UNCOV
313
    if (data) {
×
UNCOV
314
      *data = BR_PTR(reader);
×
315
    }
UNCOV
316
    reader->offset += tmpSize;
×
317
  } else if (data) {
×
318
    *data = NULL;
×
319
  }
320

UNCOV
321
  return 0;
×
322
}
323

UNCOV
324
static int32_t tBufferGetCStr(SBufferReader *reader, const char **str) {
×
UNCOV
325
  return tBufferGetBinary(reader, (const void **)str, NULL);
×
326
}
327

UNCOV
328
static int32_t tBufferGetF32(SBufferReader *reader, float *value) {
×
329
  union {
330
    float    f;
331
    uint32_t u;
332
  } u;
×
UNCOV
333
  TAOS_CHECK_RETURN(tBufferGetU32(reader, &u.u));
×
UNCOV
334
  if (value) {
×
UNCOV
335
    *value = u.f;
×
336
  }
UNCOV
337
  return 0;
×
338
}
339

UNCOV
340
static int32_t tBufferGetF64(SBufferReader *reader, double *value) {
×
341
  union {
342
    double   f;
343
    uint64_t u;
344
  } u;
×
UNCOV
345
  TAOS_CHECK_RETURN(tBufferGetU64(reader, &u.u));
×
UNCOV
346
  if (value) {
×
UNCOV
347
    *value = u.f;
×
348
  }
UNCOV
349
  return 0;
×
350
}
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