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

taosdata / TDengine / #3524

08 Nov 2024 04:27AM UTC coverage: 60.898% (+5.0%) from 55.861%
#3524

push

travis-ci

web-flow
Merge pull request #28647 from taosdata/fix/3.0/TD-32519_drop_ctb

fix TD-32519 drop child table with tsma caused crash

118687 of 248552 branches covered (47.75%)

Branch coverage included in aggregate %.

286 of 337 new or added lines in 18 files covered. (84.87%)

9647 existing lines in 190 files now uncovered.

199106 of 273291 relevant lines covered (72.85%)

15236719.35 hits per line

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

57.18
/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;
144,798,397✔
34
  buffer->capacity = 0;
144,798,397✔
35
  buffer->data = NULL;
144,798,397✔
36
}
125,940,409✔
37

38
static FORCE_INLINE void tBufferDestroy(SBuffer *buffer) {
39
  buffer->size = 0;
374,946,385✔
40
  buffer->capacity = 0;
374,946,385✔
41
  if (buffer->data) {
374,946,385!
42
    taosMemoryFree(buffer->data);
42,517,706✔
43
    buffer->data = NULL;
42,515,250✔
44
  }
45
}
374,943,929✔
46

47
static FORCE_INLINE void tBufferClear(SBuffer *buffer) { buffer->size = 0; }
79,925,470✔
48

49
static FORCE_INLINE int32_t tBufferEnsureCapacity(SBuffer *buffer, uint32_t capacity) {
50
  if (buffer->capacity < capacity) {
167,122,077!
51
    uint32_t newCapacity = (buffer->capacity > 0) ? (buffer->capacity << 1) : 1024;
45,266,856!
52
    while (newCapacity < capacity) {
48,420,110!
53
      newCapacity <<= 1;
3,153,254✔
54
    }
55
    void *newData = taosMemoryRealloc(buffer->data, newCapacity);
45,266,856✔
56
    if (newData == NULL) {
45,265,444!
UNCOV
57
      return TSDB_CODE_OUT_OF_MEMORY;
×
58
    }
59
    buffer->data = newData;
45,265,444✔
60
    buffer->capacity = newCapacity;
45,265,444✔
61
  }
62
  return 0;
168,111,328✔
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));
71,290,067!
67
  memcpy((char *)buffer->data + buffer->size, data, size);
44,923,589✔
68
  buffer->size += size;
44,923,589✔
69
  return 0;
44,923,589✔
70
}
71

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

80
static FORCE_INLINE int32_t tBufferPutI8(SBuffer *buffer, int8_t value) {
81
  return tBufferPut(buffer, &value, sizeof(value));
7,506,569✔
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));
2,870,121✔
90
}
91

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

96
static FORCE_INLINE int32_t tBufferPutU8(SBuffer *buffer, uint8_t value) {
97
  return tBufferPut(buffer, &value, sizeof(value));
18,568,130✔
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));
3,089,015✔
106
}
107

108
static FORCE_INLINE int32_t tBufferPutU64(SBuffer *buffer, uint64_t value) {
109
  return tBufferPut(buffer, &value, sizeof(value));
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); }
356,565✔
115

116
static FORCE_INLINE int32_t tBufferPutU64v(SBuffer *buffer, uint64_t value) {
117
  while (value >= 0x80) {
18,556,908✔
118
    TAOS_CHECK_RETURN(tBufferPutU8(buffer, (value & 0x7F) | 0x80));
11,467,062!
119
    value >>= 7;
5,733,531✔
120
  }
121
  return tBufferPutU8(buffer, value);
25,646,791!
122
}
123

124
static FORCE_INLINE int32_t tBufferPutI16v(SBuffer *buffer, int16_t value) {
125
  return tBufferPutU64v(buffer, ZIGZAGE(int16_t, value));
6,028,035✔
126
}
127

128
static FORCE_INLINE int32_t tBufferPutI32v(SBuffer *buffer, int32_t value) {
129
  return tBufferPutU64v(buffer, ZIGZAGE(int32_t, value));
18,905,626✔
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
165
static int32_t tBufferReaderInit(SBufferReader *reader, uint32_t offset, SBuffer *buffer) {
×
166
  reader->offset = offset;
×
167
  reader->buffer = buffer;
×
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); }
267,473,230!
183

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

188
static int32_t tBufferGetI32(SBufferReader *reader, int32_t *value) {
17,656,826!
189
  return tBufferGet(reader, sizeof(*value), value);
17,656,826✔
190
}
191

192
static int32_t tBufferGetI64(SBufferReader *reader, int64_t *value) {
2,111,920,553!
193
  return tBufferGet(reader, sizeof(*value), value);
2,111,920,553✔
194
}
195

196
static int32_t tBufferGetU8(SBufferReader *reader, uint8_t *value) { return tBufferGet(reader, sizeof(*value), value); }
606,282,550!
197

198
static int32_t tBufferGetU16(SBufferReader *reader, uint16_t *value) {
×
199
  return tBufferGet(reader, sizeof(*value), value);
×
200
}
201

202
static int32_t tBufferGetU32(SBufferReader *reader, uint32_t *value) {
52,342,291!
203
  return tBufferGet(reader, sizeof(*value), value);
52,342,291✔
204
}
205

206
static int32_t tBufferGetU64(SBufferReader *reader, uint64_t *value) {
×
207
  return tBufferGet(reader, sizeof(*value), value);
×
208
}
209

210
static int32_t tBufferGetU64v(SBufferReader *reader, uint64_t *value) {
207,408,437✔
211
  uint8_t  byte;
212
  int32_t  code;
213
  uint64_t u64 = 0;
207,408,437✔
214

215
  for (int32_t i = 0;; i++) {
207,408,437✔
216
    code = tBufferGetU8(reader, &byte);
302,465,491✔
217
    if (code) return code;
302,496,934!
218

219
    u64 |= (((uint64_t)(byte & 0x7F)) << (i * 7));
302,496,934✔
220

221
    if (byte < 0x80) {
302,496,934✔
222
      break;
207,439,880✔
223
    }
224
  }
225

226
  if (value) {
207,439,880✔
227
    *value = u64;
207,443,794✔
228
  }
229

230
  return 0;
207,439,880✔
231
}
232

233
static int32_t tBufferGetU16v(SBufferReader *reader, uint16_t *value) {
43,660,347✔
234
  uint64_t u64;
235
  int32_t  code = tBufferGetU64v(reader, &u64);
43,660,347✔
236
  if (code) return code;
43,661,934!
237
  if (value) {
43,661,934!
238
    *value = (uint16_t)u64;
43,662,305✔
239
  }
240
  return 0;
43,661,934✔
241
}
242

243
static int32_t tBufferGetU32v(SBufferReader *reader, uint32_t *value) {
163,910,584✔
244
  uint64_t u64;
245
  int32_t  code = tBufferGetU64v(reader, &u64);
163,910,584✔
246
  if (code) return code;
163,900,849!
247
  if (value) {
163,900,849!
248
    *value = (uint32_t)u64;
163,904,861✔
249
  }
250
  return 0;
163,900,849✔
251
}
252

253
static int32_t tBufferGetI16v(SBufferReader *reader, int16_t *value) {
43,660,540✔
254
  uint16_t u16;
255
  int32_t  code = tBufferGetU16v(reader, &u16);
43,660,540✔
256
  if (code) return code;
43,662,520!
257
  if (value) {
43,662,520!
258
    *value = ZIGZAGD(int16_t, u16);
43,662,759✔
259
  }
260
  return 0;
43,662,520✔
261
}
262

263
static int32_t tBufferGetI32v(SBufferReader *reader, int32_t *value) {
158,991,634✔
264
  uint32_t u32;
265
  int32_t  code = tBufferGetU32v(reader, &u32);
158,991,634✔
266
  if (code) return code;
158,986,777!
267
  if (value) {
158,986,777!
268
    *value = ZIGZAGD(int32_t, u32);
158,990,090✔
269
  }
270
  return 0;
158,986,777✔
271
}
272

273
static int32_t tBufferGetI64v(SBufferReader *reader, int64_t *value) {
×
274
  uint64_t u64;
275
  int32_t  code = tBufferGetU64v(reader, &u64);
×
276
  if (code) return code;
×
277
  if (value) {
×
278
    *value = ZIGZAGD(int64_t, u64);
×
279
  }
280
  return 0;
×
281
}
282

283
static int32_t tBufferGetBinary(SBufferReader *reader, const void **data, uint32_t *size) {
×
284
  uint32_t tmpSize;
285
  int32_t  code;
286

287
  // size
288
  code = tBufferGetU32v(reader, &tmpSize);
×
289
  if (code) return code;
×
290
  if (size) {
×
291
    *size = tmpSize;
×
292
  }
293

294
  // data
295
  if (tmpSize > 0) {
×
296
    if (reader->offset + tmpSize > reader->buffer->size) {
×
297
      return TSDB_CODE_OUT_OF_RANGE;
×
298
    }
299
    if (data) {
×
300
      *data = BR_PTR(reader);
×
301
    }
302
    reader->offset += tmpSize;
×
303
  } else if (data) {
×
304
    *data = NULL;
×
305
  }
306

307
  return 0;
×
308
}
309

310
static int32_t tBufferGetCStr(SBufferReader *reader, const char **str) {
×
311
  return tBufferGetBinary(reader, (const void **)str, NULL);
×
312
}
313

314
static int32_t tBufferGetF32(SBufferReader *reader, float *value) {
×
315
  union {
316
    float    f;
317
    uint32_t u;
318
  } u;
319
  TAOS_CHECK_RETURN(tBufferGetU32(reader, &u.u));
×
320
  if (value) {
×
321
    *value = u.f;
×
322
  }
323
  return 0;
×
324
}
325

326
static int32_t tBufferGetF64(SBufferReader *reader, double *value) {
×
327
  union {
328
    double   f;
329
    uint64_t u;
330
  } u;
331
  TAOS_CHECK_RETURN(tBufferGetU64(reader, &u.u));
×
332
  if (value) {
×
333
    *value = u.f;
×
334
  }
335
  return 0;
×
336
}
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