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

taosdata / TDengine / #3532

20 Nov 2024 07:11AM UTC coverage: 60.78% (+0.6%) from 60.213%
#3532

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

119943 of 252352 branches covered (47.53%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

463 existing lines in 99 files now uncovered.

200682 of 275165 relevant lines covered (72.93%)

15642683.31 hits per line

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

55.9
/source/common/src/tmisce.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
#define _DEFAULT_SOURCE
17
#include "tmisce.h"
18
#include "tdatablock.h"
19
#include "tglobal.h"
20
#include "tjson.h"
21

22
int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp) {
40,969✔
23
  pEp->port = 0;
40,969✔
24
  memset(pEp->fqdn, 0, TSDB_FQDN_LEN);
40,969✔
25
  strncpy(pEp->fqdn, ep, TSDB_FQDN_LEN - 1);
40,969✔
26

27
  char* temp = strchr(pEp->fqdn, ':');
40,969✔
28
  if (temp) {
40,969✔
29
    *temp = 0;
32,315✔
30
    pEp->port = atoi(temp + 1);
32,315✔
31
  }
32

33
  if (pEp->port == 0) {
40,969✔
34
    pEp->port = tsServerPort;
8,654✔
35
  }
36

37
  if (pEp->port <= 0) {
40,969!
38
    return TSDB_CODE_INVALID_PARA;
×
39
  }
40

41
  return 0;
40,969✔
42
}
43

44
int32_t addEpIntoEpSet(SEpSet* pEpSet, const char* fqdn, uint16_t port) {
1,048,844✔
45
  if (pEpSet == NULL || fqdn == NULL || strlen(fqdn) == 0) {
1,048,844!
46
    return TSDB_CODE_INVALID_PARA;
×
47
  }
48

49
  int32_t index = pEpSet->numOfEps;
1,048,868✔
50
  if (index >= sizeof(pEpSet->eps) / sizeof(pEpSet->eps[0])) {
1,048,868!
51
    return TSDB_CODE_OUT_OF_RANGE;
×
52
  }
53
  tstrncpy(pEpSet->eps[index].fqdn, fqdn, tListLen(pEpSet->eps[index].fqdn));
1,048,868✔
54
  pEpSet->eps[index].port = port;
1,048,868✔
55
  pEpSet->numOfEps += 1;
1,048,868✔
56
  return 0;
1,048,868✔
57
}
58

59
bool isEpsetEqual(const SEpSet* s1, const SEpSet* s2) {
88,684✔
60
  if (s1->numOfEps != s2->numOfEps || s1->inUse != s2->inUse) {
88,684✔
61
    return false;
2,564✔
62
  }
63

64
  for (int32_t i = 0; i < s1->numOfEps; i++) {
176,240✔
65
    if (s1->eps[i].port != s2->eps[i].port || strncmp(s1->eps[i].fqdn, s2->eps[i].fqdn, TSDB_FQDN_LEN) != 0)
90,450✔
66
      return false;
330✔
67
  }
68
  return true;
85,790✔
69
}
70

71
void epsetAssign(SEpSet* pDst, const SEpSet* pSrc) {
58,989✔
72
  if (pSrc == NULL || pDst == NULL) {
58,989!
73
    return;
×
74
  }
75

76
  pDst->inUse = pSrc->inUse;
58,989✔
77
  pDst->numOfEps = pSrc->numOfEps;
58,989✔
78
  for (int32_t i = 0; i < pSrc->numOfEps; ++i) {
119,260✔
79
    pDst->eps[i].port = pSrc->eps[i].port;
60,271✔
80
    tstrncpy(pDst->eps[i].fqdn, pSrc->eps[i].fqdn, tListLen(pSrc->eps[i].fqdn));
60,271✔
81
  }
82
}
83

84
void epAssign(SEp* pDst, SEp* pSrc) {
167,128✔
85
  if (pSrc == NULL || pDst == NULL) {
167,128!
86
    return;
×
87
  }
88
  memset(pDst->fqdn, 0, tListLen(pSrc->fqdn));
167,128✔
89
  tstrncpy(pDst->fqdn, pSrc->fqdn, tListLen(pSrc->fqdn));
167,128✔
90
  pDst->port = pSrc->port;
167,128✔
91
}
92

93
void epsetSort(SEpSet* pDst) {
1,027,908✔
94
  if (pDst->numOfEps <= 1) {
1,027,908✔
95
    return;
935,489✔
96
  }
97
  int validIdx = false;
92,419✔
98
  SEp ep = {0};
92,419✔
99
  if (pDst->inUse >= 0 && pDst->inUse < pDst->numOfEps) {
92,419!
100
    validIdx = true;
92,422✔
101
    epAssign(&ep, &pDst->eps[pDst->inUse]);
92,422✔
102
  }
103

104
  for (int i = 0; i < pDst->numOfEps - 1; i++) {
244,076✔
105
    for (int j = 0; j < pDst->numOfEps - 1 - i; j++) {
367,332✔
106
      SEp* f = &pDst->eps[j];
215,677✔
107
      SEp* s = &pDst->eps[j + 1];
215,677✔
108
      int  cmp = strncmp(f->fqdn, s->fqdn, sizeof(f->fqdn));
215,677✔
109
      if (cmp > 0 || (cmp == 0 && f->port > s->port)) {
215,677!
110
        SEp ep1 = {0};
24,902✔
111
        epAssign(&ep1, f);
24,902✔
112
        epAssign(f, s);
24,902✔
113
        epAssign(s, &ep1);
24,902✔
114
      }
115
    }
116
  }
117
  if (validIdx == true)
92,421!
118
    for (int i = 0; i < pDst->numOfEps; i++) {
134,494!
119
      int cmp = strncmp(ep.fqdn, pDst->eps[i].fqdn, sizeof(ep.fqdn));
134,494✔
120
      if (cmp == 0 && ep.port == pDst->eps[i].port) {
134,494!
121
        pDst->inUse = i;
92,422✔
122
        break;
92,422✔
123
      }
124
    }
125
}
126

127
void updateEpSet_s(SCorEpSet* pEpSet, SEpSet* pNewEpSet) {
2,628✔
128
  taosCorBeginWrite(&pEpSet->version);
2,628!
129
  pEpSet->epSet = *pNewEpSet;
2,628✔
130
  taosCorEndWrite(&pEpSet->version);
2,628✔
131
}
2,628✔
132

133
SEpSet getEpSet_s(SCorEpSet* pEpSet) {
31,293,868✔
134
  SEpSet ep = {0};
31,293,868✔
135
  taosCorBeginRead(&pEpSet->version);
31,291,518!
136
  ep = pEpSet->epSet;
31,603,695✔
137
  taosCorEndRead(&pEpSet->version);
31,603,695!
138

139
  return ep;
31,610,054✔
140
}
141

142
int32_t epsetToStr(const SEpSet* pEpSet, char* pBuf, int32_t cap) {
360,027✔
143
  int32_t ret = 0;
360,027✔
144
  int32_t nwrite = 0;
360,027✔
145

146
  nwrite = snprintf(pBuf + nwrite, cap, "epset:{");
360,027✔
147
  if (nwrite <= 0 || nwrite >= cap) {
360,027!
148
    return TSDB_CODE_OUT_OF_BUFFER;
×
149
  }
150
  cap -= nwrite;
360,027✔
151

152
  for (int _i = 0; (_i < pEpSet->numOfEps) && (cap > 0); _i++) {
851,576!
153
    if (_i == pEpSet->numOfEps - 1) {
491,549✔
154
      ret = snprintf(pBuf + nwrite, cap, "%d. %s:%d", _i, pEpSet->eps[_i].fqdn, pEpSet->eps[_i].port);
360,009✔
155
    } else {
156
      ret = snprintf(pBuf + nwrite, cap, "%d. %s:%d, ", _i, pEpSet->eps[_i].fqdn, pEpSet->eps[_i].port);
131,540✔
157
    }
158

159
    if (ret <= 0 || ret >= cap) {
491,549!
UNCOV
160
      return TSDB_CODE_OUT_OF_BUFFER;
×
161
    }
162

163
    nwrite += ret;
491,549✔
164
    cap -= ret;
491,549✔
165
  }
166

167
  if (cap <= 0) {
360,027!
168
    return TSDB_CODE_OUT_OF_BUFFER;
×
169
  }
170

171
  ret = snprintf(pBuf + nwrite, cap, "}, inUse:%d", pEpSet->inUse);
360,027✔
172
  if (ret <= 0 || ret >= cap) {
360,027!
173
    return TSDB_CODE_OUT_OF_BUFFER;
×
174
  } else {
175
    return TSDB_CODE_SUCCESS;
360,027✔
176
  }
177
}
178

179
int32_t taosGenCrashJsonMsg(int signum, char** pMsg, int64_t clusterId, int64_t startTime) {
×
180
  int32_t code = 0;
×
181
  SJson*  pJson = tjsonCreateObject();
×
182
  if (pJson == NULL) return terrno;
×
183

184
  char tmp[4096] = {0};
×
185

186
  TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "reportVersion", 1), NULL, _exit);
×
187

188
  TAOS_CHECK_GOTO(tjsonAddIntegerToObject(pJson, "clusterId", clusterId), NULL, _exit);
×
189
  TAOS_CHECK_GOTO(tjsonAddIntegerToObject(pJson, "startTime", startTime), NULL, _exit);
×
190

191
  // Do NOT invoke the taosGetFqdn here.
192
  // this function may be invoked when memory exception occurs,so we should assume that it is running in a memory locked
193
  // environment. The lock operation by taosGetFqdn may cause this program deadlock.
194
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "fqdn", tsLocalFqdn), NULL, _exit);
×
195

196
  TAOS_CHECK_GOTO(tjsonAddIntegerToObject(pJson, "pid", taosGetPId()), NULL, _exit);
×
197

198
  code = taosGetAppName(tmp, NULL);
×
199
  if (code != 0) {
×
200
    TAOS_CHECK_GOTO(code, NULL, _exit);
×
201
  }
202
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "appName", tmp), NULL, _exit);
×
203

204
  if (taosGetOsReleaseName(tmp, NULL, NULL, sizeof(tmp)) == 0) {
×
205
    TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "os", tmp), NULL, _exit);
×
206
  } else {
207
    // do nothing
208
  }
209

210
  float numOfCores = 0;
×
211
  if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores) == 0) {
×
212
    TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "cpuModel", tmp), NULL, _exit);
×
213
    TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores), NULL, _exit);
×
214
  } else {
215
    TAOS_CHECK_GOTO(tjsonAddDoubleToObject(pJson, "numOfCpu", tsNumOfCores), NULL, _exit);
×
216
  }
217

218
  int32_t nBytes = snprintf(tmp, sizeof(tmp), "%" PRId64 " kB", tsTotalMemoryKB);
×
219
  if (nBytes <= 9 || nBytes >= sizeof(tmp)) {
×
220
    TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_RANGE, NULL, _exit);
×
221
  }
222
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "memory", tmp), NULL, _exit);
×
223

224
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "version", td_version), NULL, _exit);
×
225
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "buildInfo", td_buildinfo), NULL, _exit);
×
226
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "gitInfo", td_gitinfo), NULL, _exit);
×
227

228
  TAOS_CHECK_GOTO(tjsonAddIntegerToObject(pJson, "crashSig", signum), NULL, _exit);
×
229
  TAOS_CHECK_GOTO(tjsonAddIntegerToObject(pJson, "crashTs", taosGetTimestampUs()), NULL, _exit);
×
230

231
#ifdef _TD_DARWIN_64
232
  taosLogTraceToBuf(tmp, sizeof(tmp), 4);
233
#elif !defined(WINDOWS)
234
  taosLogTraceToBuf(tmp, sizeof(tmp), 3);
×
235
#else
236
  taosLogTraceToBuf(tmp, sizeof(tmp), 8);
237
#endif
238

239
  TAOS_CHECK_GOTO(tjsonAddStringToObject(pJson, "stackInfo", tmp), NULL, _exit);
×
240

241
  char* pCont = tjsonToString(pJson);
×
242
  if (pCont == NULL) {
×
243
    code = terrno;
×
244
    TAOS_CHECK_GOTO(code, NULL, _exit);
×
245
    goto _exit;
×
246
  }
247

248
  tjsonDelete(pJson);
×
249
  *pMsg = pCont;
×
250
  pJson = NULL;
×
251
_exit:
×
252
  tjsonDelete(pJson);
×
253
  TAOS_RETURN(code);
×
254
}
255

256
int32_t dumpConfToDataBlock(SSDataBlock* pBlock, int32_t startCol) {
545✔
257
  int32_t  code = 0;
545✔
258
  SConfig* pConf = taosGetCfg();
545✔
259
  if (pConf == NULL) {
545!
260
    return TSDB_CODE_INVALID_CFG;
×
261
  }
262

263
  int32_t      numOfRows = 0;
545✔
264
  int32_t      col = startCol;
545✔
265
  SConfigItem* pItem = NULL;
545✔
266
  SConfigIter* pIter = NULL;
545✔
267

268
  int8_t locked = 0;
545✔
269

270
  size_t       exSize = 0;
545✔
271
  size_t       index = 0;
545✔
272
  SConfigItem* pDataDirItem = cfgGetItem(pConf, "dataDir");
545✔
273
  if (pDataDirItem) {
545✔
274
    exSize = TMAX(taosArrayGetSize(pDataDirItem->array), 1) - 1;
98✔
275
  }
276

277
  TAOS_CHECK_GOTO(blockDataEnsureCapacity(pBlock, cfgGetSize(pConf) + exSize), NULL, _exit);
545!
278

279
  TAOS_CHECK_GOTO(cfgCreateIter(pConf, &pIter), NULL, _exit);
545!
280

281
  cfgLock(pConf);
545✔
282
  locked = 1;
545✔
283

284
  while ((pItem = cfgNextIter(pIter)) != NULL) {
57,595✔
285
_start:
57,050✔
286
    col = startCol;
57,050✔
287

288
    // GRANT_CFG_SKIP;
289
    char name[TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE] = {0};
57,050✔
290
    STR_WITH_MAXSIZE_TO_VARSTR(name, pItem->name, TSDB_CONFIG_OPTION_LEN + VARSTR_HEADER_SIZE);
57,050✔
291

292
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, col++);
57,050✔
293
    if (pColInfo == NULL) {
57,050!
294
      code = terrno;
×
295
      TAOS_CHECK_GOTO(code, NULL, _exit);
×
296
    }
297

298
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, name, false), NULL, _exit);
57,050!
299

300
    char    value[TSDB_CONFIG_PATH_LEN + VARSTR_HEADER_SIZE] = {0};
57,050✔
301
    int32_t valueLen = 0;
57,050✔
302
    SDiskCfg* pDiskCfg = NULL;
57,050✔
303
    if (strcasecmp(pItem->name, "dataDir") == 0 && exSize > 0) {
57,050!
304
      char*     buf = &value[VARSTR_HEADER_SIZE];
×
305
      pDiskCfg = taosArrayGet(pItem->array, index);
×
306
      valueLen = tsnprintf(buf, TSDB_CONFIG_PATH_LEN, "%s", pDiskCfg->dir);
×
307
      index++;
×
308
    } else {
309
      TAOS_CHECK_GOTO(cfgDumpItemValue(pItem, &value[VARSTR_HEADER_SIZE], TSDB_CONFIG_PATH_LEN, &valueLen), NULL,
57,050!
310
                      _exit);
311
    }
312
    varDataSetLen(value, valueLen);
57,050✔
313

314
    pColInfo = taosArrayGet(pBlock->pDataBlock, col++);
57,050✔
315
    if (pColInfo == NULL) {
57,050!
316
      code = terrno;
×
317
      TAOS_CHECK_GOTO(code, NULL, _exit);
×
318
    }
319

320
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, value, false), NULL, _exit);
57,050!
321

322
    char scope[TSDB_CONFIG_SCOPE_LEN + VARSTR_HEADER_SIZE] = {0};
57,050✔
323
    TAOS_CHECK_GOTO(cfgDumpItemScope(pItem, &scope[VARSTR_HEADER_SIZE], TSDB_CONFIG_SCOPE_LEN, &valueLen), NULL, _exit);
57,050!
324
    varDataSetLen(scope, valueLen);
57,050✔
325

326
    pColInfo = taosArrayGet(pBlock->pDataBlock, col++);
57,050✔
327
    if (pColInfo == NULL) {
57,050!
328
      code = terrno;
×
329
      TAOS_CHECK_GOTO(code, NULL, _exit);
×
330
    }
331
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, scope, false), NULL, _exit);
57,050!
332

333
    char info[TSDB_CONFIG_INFO_LEN + VARSTR_HEADER_SIZE] = {0};
57,050✔
334
    if (strcasecmp(pItem->name, "dataDir") == 0 && pDiskCfg) {
57,050!
335
      char* buf = &info[VARSTR_HEADER_SIZE];
×
336
      valueLen = tsnprintf(buf, TSDB_CONFIG_INFO_LEN, "level %d primary %d disabled %" PRIi8, pDiskCfg->level,
×
337
                              pDiskCfg->primary, pDiskCfg->disable);
×
338
    } else {
339
      valueLen = 0;
57,050✔
340
    }
341
    varDataSetLen(info, valueLen);
57,050✔
342

343
    pColInfo = taosArrayGet(pBlock->pDataBlock, col++);
57,050✔
344
    if (pColInfo == NULL) {
57,050!
345
      code = terrno;
×
346
      TAOS_CHECK_GOTO(code, NULL, _exit);
×
347
    }
348
    TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, info, false), NULL, _exit);
57,050!
349

350
    numOfRows++;
57,050✔
351
    if (index > 0 && index <= exSize) {
57,050!
352
      goto _start;
×
353
    }
354
}
355
  pBlock->info.rows = numOfRows;
545✔
356
_exit:
545✔
357
  if (locked) cfgUnLock(pConf);
545!
358
  cfgDestroyIter(pIter);
545✔
359
  TAOS_RETURN(code);
545✔
360
}
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