rrdlabels.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. // ----------------------------------------------------------------------------
  5. // labels sanitization
  6. /*
  7. * All labels follow these rules:
  8. *
  9. * Character Symbol Values Names
  10. * UTF-8 characters UTF-8 yes -> _
  11. * Lower case letter [a-z] yes yes
  12. * Upper case letter [A-Z] yes -> [a-z]
  13. * Digit [0-9] yes yes
  14. * Underscore _ yes yes
  15. * Minus - yes yes
  16. * Plus + yes -> _
  17. * Colon : yes -> _
  18. * Semicolon ; -> : -> _
  19. * Equal = -> : -> _
  20. * Period . yes yes
  21. * Comma , -> . -> .
  22. * Slash / yes yes
  23. * Backslash \ -> / -> /
  24. * At @ yes -> _
  25. * Space yes -> _
  26. * Opening parenthesis ( yes -> _
  27. * Closing parenthesis ) yes -> _
  28. * anything else -> _ -> _
  29. *
  30. * The above rules should allow users to set in tags (indicative):
  31. *
  32. * 1. hostnames and domain names as-is
  33. * 2. email addresses as-is
  34. * 3. floating point numbers, converted to always use a dot as the decimal point
  35. *
  36. * Leading and trailing spaces and control characters are removed from both label
  37. * names and values.
  38. *
  39. * Multiple spaces inside the label name or the value are removed (only 1 is retained).
  40. * In names spaces are also converted to underscores.
  41. *
  42. * Names that are only underscores are rejected (they do not enter the dictionary).
  43. *
  44. * The above rules do not require any conversion to be included in JSON strings.
  45. *
  46. * Label names and values are truncated to LABELS_MAX_LENGTH (200) characters.
  47. *
  48. * When parsing, label key and value are separated by the first colon (:) found.
  49. * So label:value1:value2 is parsed as key = "label", value = "value1:value2"
  50. *
  51. * This means a label key cannot contain a colon (:) - it is converted to
  52. * underscore if it does.
  53. *
  54. */
  55. #define RRDLABELS_MAX_NAME_LENGTH 200
  56. #define RRDLABELS_MAX_VALUE_LENGTH 800 // 800 in bytes, up to 200 UTF-8 characters
  57. static unsigned char label_spaces_char_map[256];
  58. static unsigned char label_names_char_map[256];
  59. static unsigned char label_values_char_map[256] = {
  60. [0] = '\0', //
  61. [1] = '_', //
  62. [2] = '_', //
  63. [3] = '_', //
  64. [4] = '_', //
  65. [5] = '_', //
  66. [6] = '_', //
  67. [7] = '_', //
  68. [8] = '_', //
  69. [9] = '_', //
  70. [10] = '_', //
  71. [11] = '_', //
  72. [12] = '_', //
  73. [13] = '_', //
  74. [14] = '_', //
  75. [15] = '_', //
  76. [16] = '_', //
  77. [17] = '_', //
  78. [18] = '_', //
  79. [19] = '_', //
  80. [20] = '_', //
  81. [21] = '_', //
  82. [22] = '_', //
  83. [23] = '_', //
  84. [24] = '_', //
  85. [25] = '_', //
  86. [26] = '_', //
  87. [27] = '_', //
  88. [28] = '_', //
  89. [29] = '_', //
  90. [30] = '_', //
  91. [31] = '_', //
  92. [32] = ' ', // SPACE keep
  93. [33] = '_', // !
  94. [34] = '_', // "
  95. [35] = '_', // #
  96. [36] = '_', // $
  97. [37] = '_', // %
  98. [38] = '_', // &
  99. [39] = '_', // '
  100. [40] = '(', // ( keep
  101. [41] = ')', // ) keep
  102. [42] = '_', // *
  103. [43] = '+', // + keep
  104. [44] = '.', // , convert , to .
  105. [45] = '-', // - keep
  106. [46] = '.', // . keep
  107. [47] = '/', // / keep
  108. [48] = '0', // 0 keep
  109. [49] = '1', // 1 keep
  110. [50] = '2', // 2 keep
  111. [51] = '3', // 3 keep
  112. [52] = '4', // 4 keep
  113. [53] = '5', // 5 keep
  114. [54] = '6', // 6 keep
  115. [55] = '7', // 7 keep
  116. [56] = '8', // 8 keep
  117. [57] = '9', // 9 keep
  118. [58] = ':', // : keep
  119. [59] = ':', // ; convert ; to :
  120. [60] = '_', // <
  121. [61] = ':', // = convert = to :
  122. [62] = '_', // >
  123. [63] = '_', // ?
  124. [64] = '@', // @
  125. [65] = 'A', // A keep
  126. [66] = 'B', // B keep
  127. [67] = 'C', // C keep
  128. [68] = 'D', // D keep
  129. [69] = 'E', // E keep
  130. [70] = 'F', // F keep
  131. [71] = 'G', // G keep
  132. [72] = 'H', // H keep
  133. [73] = 'I', // I keep
  134. [74] = 'J', // J keep
  135. [75] = 'K', // K keep
  136. [76] = 'L', // L keep
  137. [77] = 'M', // M keep
  138. [78] = 'N', // N keep
  139. [79] = 'O', // O keep
  140. [80] = 'P', // P keep
  141. [81] = 'Q', // Q keep
  142. [82] = 'R', // R keep
  143. [83] = 'S', // S keep
  144. [84] = 'T', // T keep
  145. [85] = 'U', // U keep
  146. [86] = 'V', // V keep
  147. [87] = 'W', // W keep
  148. [88] = 'X', // X keep
  149. [89] = 'Y', // Y keep
  150. [90] = 'Z', // Z keep
  151. [91] = '_', // [
  152. [92] = '/', // backslash convert \ to /
  153. [93] = '_', // ]
  154. [94] = '_', // ^
  155. [95] = '_', // _ keep
  156. [96] = '_', // `
  157. [97] = 'a', // a keep
  158. [98] = 'b', // b keep
  159. [99] = 'c', // c keep
  160. [100] = 'd', // d keep
  161. [101] = 'e', // e keep
  162. [102] = 'f', // f keep
  163. [103] = 'g', // g keep
  164. [104] = 'h', // h keep
  165. [105] = 'i', // i keep
  166. [106] = 'j', // j keep
  167. [107] = 'k', // k keep
  168. [108] = 'l', // l keep
  169. [109] = 'm', // m keep
  170. [110] = 'n', // n keep
  171. [111] = 'o', // o keep
  172. [112] = 'p', // p keep
  173. [113] = 'q', // q keep
  174. [114] = 'r', // r keep
  175. [115] = 's', // s keep
  176. [116] = 't', // t keep
  177. [117] = 'u', // u keep
  178. [118] = 'v', // v keep
  179. [119] = 'w', // w keep
  180. [120] = 'x', // x keep
  181. [121] = 'y', // y keep
  182. [122] = 'z', // z keep
  183. [123] = '_', // {
  184. [124] = '_', // |
  185. [125] = '_', // }
  186. [126] = '_', // ~
  187. [127] = '_', //
  188. [128] = '_', //
  189. [129] = '_', //
  190. [130] = '_', //
  191. [131] = '_', //
  192. [132] = '_', //
  193. [133] = '_', //
  194. [134] = '_', //
  195. [135] = '_', //
  196. [136] = '_', //
  197. [137] = '_', //
  198. [138] = '_', //
  199. [139] = '_', //
  200. [140] = '_', //
  201. [141] = '_', //
  202. [142] = '_', //
  203. [143] = '_', //
  204. [144] = '_', //
  205. [145] = '_', //
  206. [146] = '_', //
  207. [147] = '_', //
  208. [148] = '_', //
  209. [149] = '_', //
  210. [150] = '_', //
  211. [151] = '_', //
  212. [152] = '_', //
  213. [153] = '_', //
  214. [154] = '_', //
  215. [155] = '_', //
  216. [156] = '_', //
  217. [157] = '_', //
  218. [158] = '_', //
  219. [159] = '_', //
  220. [160] = '_', //
  221. [161] = '_', //
  222. [162] = '_', //
  223. [163] = '_', //
  224. [164] = '_', //
  225. [165] = '_', //
  226. [166] = '_', //
  227. [167] = '_', //
  228. [168] = '_', //
  229. [169] = '_', //
  230. [170] = '_', //
  231. [171] = '_', //
  232. [172] = '_', //
  233. [173] = '_', //
  234. [174] = '_', //
  235. [175] = '_', //
  236. [176] = '_', //
  237. [177] = '_', //
  238. [178] = '_', //
  239. [179] = '_', //
  240. [180] = '_', //
  241. [181] = '_', //
  242. [182] = '_', //
  243. [183] = '_', //
  244. [184] = '_', //
  245. [185] = '_', //
  246. [186] = '_', //
  247. [187] = '_', //
  248. [188] = '_', //
  249. [189] = '_', //
  250. [190] = '_', //
  251. [191] = '_', //
  252. [192] = '_', //
  253. [193] = '_', //
  254. [194] = '_', //
  255. [195] = '_', //
  256. [196] = '_', //
  257. [197] = '_', //
  258. [198] = '_', //
  259. [199] = '_', //
  260. [200] = '_', //
  261. [201] = '_', //
  262. [202] = '_', //
  263. [203] = '_', //
  264. [204] = '_', //
  265. [205] = '_', //
  266. [206] = '_', //
  267. [207] = '_', //
  268. [208] = '_', //
  269. [209] = '_', //
  270. [210] = '_', //
  271. [211] = '_', //
  272. [212] = '_', //
  273. [213] = '_', //
  274. [214] = '_', //
  275. [215] = '_', //
  276. [216] = '_', //
  277. [217] = '_', //
  278. [218] = '_', //
  279. [219] = '_', //
  280. [220] = '_', //
  281. [221] = '_', //
  282. [222] = '_', //
  283. [223] = '_', //
  284. [224] = '_', //
  285. [225] = '_', //
  286. [226] = '_', //
  287. [227] = '_', //
  288. [228] = '_', //
  289. [229] = '_', //
  290. [230] = '_', //
  291. [231] = '_', //
  292. [232] = '_', //
  293. [233] = '_', //
  294. [234] = '_', //
  295. [235] = '_', //
  296. [236] = '_', //
  297. [237] = '_', //
  298. [238] = '_', //
  299. [239] = '_', //
  300. [240] = '_', //
  301. [241] = '_', //
  302. [242] = '_', //
  303. [243] = '_', //
  304. [244] = '_', //
  305. [245] = '_', //
  306. [246] = '_', //
  307. [247] = '_', //
  308. [248] = '_', //
  309. [249] = '_', //
  310. [250] = '_', //
  311. [251] = '_', //
  312. [252] = '_', //
  313. [253] = '_', //
  314. [254] = '_', //
  315. [255] = '_' //
  316. };
  317. __attribute__((constructor)) void initialize_labels_keys_char_map(void) {
  318. // copy the values char map to the names char map
  319. size_t i;
  320. for(i = 0; i < 256 ;i++)
  321. label_names_char_map[i] = label_values_char_map[i];
  322. // apply overrides to the label names map
  323. label_names_char_map['A'] = 'a';
  324. label_names_char_map['B'] = 'b';
  325. label_names_char_map['C'] = 'c';
  326. label_names_char_map['D'] = 'd';
  327. label_names_char_map['E'] = 'e';
  328. label_names_char_map['F'] = 'f';
  329. label_names_char_map['G'] = 'g';
  330. label_names_char_map['H'] = 'h';
  331. label_names_char_map['I'] = 'i';
  332. label_names_char_map['J'] = 'j';
  333. label_names_char_map['K'] = 'k';
  334. label_names_char_map['L'] = 'l';
  335. label_names_char_map['M'] = 'm';
  336. label_names_char_map['N'] = 'n';
  337. label_names_char_map['O'] = 'o';
  338. label_names_char_map['P'] = 'p';
  339. label_names_char_map['Q'] = 'q';
  340. label_names_char_map['R'] = 'r';
  341. label_names_char_map['S'] = 's';
  342. label_names_char_map['T'] = 't';
  343. label_names_char_map['U'] = 'u';
  344. label_names_char_map['V'] = 'v';
  345. label_names_char_map['W'] = 'w';
  346. label_names_char_map['X'] = 'x';
  347. label_names_char_map['Y'] = 'y';
  348. label_names_char_map['Z'] = 'z';
  349. label_names_char_map['='] = '_';
  350. label_names_char_map[':'] = '_';
  351. label_names_char_map['+'] = '_';
  352. label_names_char_map[';'] = '_';
  353. label_names_char_map['@'] = '_';
  354. label_names_char_map['('] = '_';
  355. label_names_char_map[')'] = '_';
  356. label_names_char_map[' '] = '_';
  357. label_names_char_map['\\'] = '/';
  358. // create the spaces map
  359. for(i = 0; i < 256 ;i++)
  360. label_spaces_char_map[i] = (isspace(i) || iscntrl(i) || !isprint(i))?1:0;
  361. }
  362. static size_t rrdlabels_sanitize(unsigned char *dst, const unsigned char *src, size_t dst_size, unsigned char *char_map, bool utf, const char *empty) {
  363. if(unlikely(!dst_size)) return 0;
  364. if(unlikely(!src || !*src)) {
  365. strncpyz((char *)dst, empty, dst_size);
  366. dst[dst_size - 1] = '\0';
  367. return strlen((char *)dst);
  368. }
  369. unsigned char *d = dst;
  370. // make room for the final string termination
  371. unsigned char *end = &d[dst_size - 1];
  372. // copy while converting, but keep only one white space
  373. // we start wil last_is_space = 1 to skip leading spaces
  374. int last_is_space = 1;
  375. size_t mblen = 0;
  376. while(*src && d < end) {
  377. unsigned char c = *src;
  378. if(IS_UTF8_STARTBYTE(c) && IS_UTF8_BYTE(src[1]) && d + 2 < end) {
  379. // UTF-8 multi-byte encoded character
  380. // find how big this character is (2-4 bytes)
  381. size_t utf_character_size = 2;
  382. while(utf_character_size <= 4 && src[utf_character_size] && IS_UTF8_BYTE(src[utf_character_size]) && !IS_UTF8_STARTBYTE(src[utf_character_size]))
  383. utf_character_size++;
  384. if(utf) {
  385. while(utf_character_size) {
  386. utf_character_size--;
  387. *d++ = *src++;
  388. }
  389. }
  390. else {
  391. // UTF-8 characters are not allowed.
  392. // Assume it is an underscore
  393. // and skip all except the first byte
  394. *d++ = '_';
  395. src += (utf_character_size - 1);
  396. }
  397. last_is_space = 0;
  398. mblen++;
  399. continue;
  400. }
  401. if(label_spaces_char_map[c]) {
  402. // a space character
  403. if(!last_is_space) {
  404. // add one space
  405. *d++ = char_map[c];
  406. mblen++;
  407. }
  408. last_is_space++;
  409. }
  410. else {
  411. *d++ = char_map[c];
  412. last_is_space = 0;
  413. mblen++;
  414. }
  415. src++;
  416. }
  417. // remove the last trailing space
  418. if(last_is_space && d > dst) {
  419. d--;
  420. mblen--;
  421. }
  422. // put a termination at the end of what we copied
  423. *d = '\0';
  424. // check if dst is all underscores and empty it if it is
  425. d = dst;
  426. while(*d == '_') d++;
  427. if(unlikely(*d == '\0')) {
  428. *dst = '\0';
  429. mblen = 0;
  430. }
  431. if(unlikely(*dst == '\0')) {
  432. strncpyz((char *)dst, empty, dst_size);
  433. dst[dst_size - 1] = '\0';
  434. return strlen((char *)dst);
  435. }
  436. return mblen;
  437. }
  438. static inline size_t rrdlabels_sanitize_name(char *dst, const char *src, size_t dst_size) {
  439. return rrdlabels_sanitize((unsigned char *)dst, (const unsigned char *)src, dst_size, label_names_char_map, 0, "");
  440. }
  441. static inline size_t rrdlabels_sanitize_value(char *dst, const char *src, size_t dst_size) {
  442. return rrdlabels_sanitize((unsigned char *)dst, (const unsigned char *)src, dst_size, label_values_char_map, 1, "[none]");
  443. }
  444. // ----------------------------------------------------------------------------
  445. // rrdlabels_create()
  446. typedef struct rrdlabel {
  447. STRING *label_value;
  448. RRDLABEL_SRC label_source;
  449. } RRDLABEL;
  450. static void rrdlabel_insert_callback(const char *name, void *value, void *data) {
  451. (void)name;
  452. DICTIONARY *dict = (DICTIONARY *)data; (void)dict;
  453. RRDLABEL *lb = (RRDLABEL *)value;
  454. // label_value is already allocated by the STRING
  455. lb->label_source |= RRDLABEL_FLAG_NEW;
  456. lb->label_source &= ~RRDLABEL_FLAG_OLD;
  457. }
  458. static void rrdlabel_delete_callback(const char *name, void *value, void *data) {
  459. (void)name;
  460. DICTIONARY *dict = (DICTIONARY *)data; (void)dict;
  461. RRDLABEL *lb = (RRDLABEL *)value;
  462. string_freez(lb->label_value);
  463. lb->label_value = NULL;
  464. }
  465. static void rrdlabel_conflict_callback(const char *name, void *oldvalue, void *newvalue, void *data) {
  466. (void)name;
  467. DICTIONARY *dict = (DICTIONARY *)data; (void)dict;
  468. RRDLABEL *lbold = (RRDLABEL *)oldvalue;
  469. RRDLABEL *lbnew = (RRDLABEL *)newvalue;
  470. if(lbold->label_value == lbnew->label_value || strcmp(string2str(lbold->label_value), string2str(lbnew->label_value)) == 0) {
  471. // they are the same
  472. lbold->label_source |= lbnew->label_source;
  473. lbold->label_source |= RRDLABEL_FLAG_OLD;
  474. lbold->label_source &= ~RRDLABEL_FLAG_NEW;
  475. // free the new one
  476. string_freez(lbnew->label_value);
  477. }
  478. else {
  479. // they are different
  480. string_freez(lbold->label_value);
  481. lbold->label_value = lbnew->label_value;
  482. lbold->label_source = lbnew->label_source;
  483. lbold->label_source |= RRDLABEL_FLAG_NEW;
  484. lbold->label_source &= ~RRDLABEL_FLAG_OLD;
  485. }
  486. }
  487. DICTIONARY *rrdlabels_create(void) {
  488. DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_DONT_OVERWRITE_VALUE);
  489. dictionary_register_insert_callback(dict, rrdlabel_insert_callback, dict);
  490. dictionary_register_delete_callback(dict, rrdlabel_delete_callback, dict);
  491. dictionary_register_conflict_callback(dict, rrdlabel_conflict_callback, dict);
  492. return dict;
  493. }
  494. // ----------------------------------------------------------------------------
  495. // rrdlabels_destroy()
  496. void rrdlabels_destroy(DICTIONARY *labels_dict) {
  497. dictionary_destroy(labels_dict);
  498. }
  499. // ----------------------------------------------------------------------------
  500. // rrdlabels_add()
  501. static void labels_add_already_sanitized(DICTIONARY *dict, const char *key, const char *value, RRDLABEL_SRC ls) {
  502. if(ls & RRDLABEL_FLAG_NEW) ls &= ~RRDLABEL_FLAG_NEW;
  503. if(ls & RRDLABEL_FLAG_OLD) ls &= ~RRDLABEL_FLAG_OLD;
  504. RRDLABEL tmp = {
  505. .label_source = ls,
  506. .label_value = string_strdupz(value)
  507. };
  508. dictionary_set(dict, key, &tmp, sizeof(RRDLABEL));
  509. }
  510. void rrdlabels_add(DICTIONARY *dict, const char *name, const char *value, RRDLABEL_SRC ls) {
  511. if(!dict) {
  512. error("%s(): called with NULL dictionary.", __FUNCTION__ );
  513. return;
  514. }
  515. char n[RRDLABELS_MAX_NAME_LENGTH + 1], v[RRDLABELS_MAX_VALUE_LENGTH + 1];
  516. rrdlabels_sanitize_name(n, name, RRDLABELS_MAX_NAME_LENGTH);
  517. rrdlabels_sanitize_value(v, value, RRDLABELS_MAX_VALUE_LENGTH);
  518. if(!*n) {
  519. error("%s: cannot add name '%s' (value '%s') which is sanitized as empty string", __FUNCTION__, name, value);
  520. return;
  521. }
  522. labels_add_already_sanitized(dict, n, v, ls);
  523. }
  524. static const char *get_quoted_string_up_to(char *dst, size_t dst_size, const char *string, char upto1, char upto2) {
  525. size_t len = 0;
  526. char *d = dst, quote = 0;
  527. while(*string && len++ < dst_size) {
  528. if(unlikely(!quote && (*string == '\'' || *string == '"'))) {
  529. quote = *string++;
  530. continue;
  531. }
  532. if(unlikely(quote && *string == quote)) {
  533. quote = 0;
  534. string++;
  535. continue;
  536. }
  537. if(unlikely(quote && *string == '\\' && string[1])) {
  538. string++;
  539. *d++ = *string++;
  540. continue;
  541. }
  542. if(unlikely(!quote && (*string == upto1 || *string == upto2))) break;
  543. *d++ = *string++;
  544. }
  545. *d = '\0';
  546. if(*string) string++;
  547. return string;
  548. }
  549. void rrdlabels_add_pair(DICTIONARY *dict, const char *string, RRDLABEL_SRC ls) {
  550. if(!dict) {
  551. error("%s(): called with NULL dictionary.", __FUNCTION__ );
  552. return;
  553. }
  554. char name[RRDLABELS_MAX_NAME_LENGTH + 1];
  555. string = get_quoted_string_up_to(name, RRDLABELS_MAX_NAME_LENGTH, string, '=', ':');
  556. char value[RRDLABELS_MAX_VALUE_LENGTH + 1];
  557. get_quoted_string_up_to(value, RRDLABELS_MAX_VALUE_LENGTH, string, '\0', '\0');
  558. rrdlabels_add(dict, name, value, ls);
  559. }
  560. // ----------------------------------------------------------------------------
  561. // rrdlabels_get_to_buffer_or_null()
  562. void rrdlabels_get_value_to_buffer_or_null(DICTIONARY *labels, BUFFER *wb, const char *key, const char *quote, const char *null) {
  563. DICTIONARY_ITEM *acquired_item = dictionary_get_and_acquire_item(labels, key);
  564. RRDLABEL *lb = dictionary_acquired_item_value(acquired_item);
  565. if(lb && lb->label_value)
  566. buffer_sprintf(wb, "%s%s%s", quote, string2str(lb->label_value), quote);
  567. else
  568. buffer_strcat(wb, null);
  569. dictionary_acquired_item_release(labels, acquired_item);
  570. }
  571. // ----------------------------------------------------------------------------
  572. // rrdlabels_unmark_all()
  573. // remove labels RRDLABEL_FLAG_OLD and RRDLABEL_FLAG_NEW from all dictionary items
  574. static int remove_flags_old_new(const char *name, void *value, void *data) {
  575. (void)name;
  576. (void)data;
  577. RRDLABEL *lb = (RRDLABEL *)value;
  578. if(lb->label_source & RRDLABEL_FLAG_OLD) lb->label_source &= ~RRDLABEL_FLAG_OLD;
  579. if(lb->label_source & RRDLABEL_FLAG_NEW) lb->label_source &= ~RRDLABEL_FLAG_NEW;
  580. return 1;
  581. }
  582. void rrdlabels_unmark_all(DICTIONARY *labels) {
  583. dictionary_walkthrough_read(labels, remove_flags_old_new, NULL);
  584. }
  585. // ----------------------------------------------------------------------------
  586. // rrdlabels_remove_all_unmarked()
  587. // remove dictionary items that are neither old, nor new
  588. static int remove_not_old_not_new_callback(const char *name, void *value, void *data) {
  589. DICTIONARY *dict = (DICTIONARY *)data;
  590. RRDLABEL *lb = (RRDLABEL *)value;
  591. if(!(lb->label_source & (RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW | RRDLABEL_FLAG_PERMANENT))) {
  592. dictionary_del_having_write_lock(dict, name);
  593. return 1;
  594. }
  595. return 0;
  596. }
  597. void rrdlabels_remove_all_unmarked(DICTIONARY *labels) {
  598. dictionary_walkthrough_write(labels, remove_not_old_not_new_callback, labels);
  599. }
  600. // ----------------------------------------------------------------------------
  601. // rrdlabels_walkthrough_read()
  602. struct labels_walkthrough {
  603. int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  604. void *data;
  605. };
  606. static int labels_walkthrough_callback(const char *name, void *value, void *data) {
  607. struct labels_walkthrough *d = (struct labels_walkthrough *)data;
  608. RRDLABEL *lb = (RRDLABEL *)value;
  609. RRDLABEL_SRC ls = lb->label_source;
  610. if(ls & RRDLABEL_FLAG_NEW) ls &= ~RRDLABEL_FLAG_NEW;
  611. if(ls & RRDLABEL_FLAG_OLD) ls &= ~RRDLABEL_FLAG_OLD;
  612. return d->callback(name, string2str(lb->label_value), ls, d->data);
  613. }
  614. int rrdlabels_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data) {
  615. struct labels_walkthrough d = {
  616. .callback = callback,
  617. .data = data
  618. };
  619. return dictionary_walkthrough_read(labels, labels_walkthrough_callback, &d);
  620. }
  621. int rrdlabels_sorted_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data) {
  622. struct labels_walkthrough d = {
  623. .callback = callback,
  624. .data = data
  625. };
  626. return dictionary_sorted_walkthrough_read(labels, labels_walkthrough_callback, &d);
  627. }
  628. // ----------------------------------------------------------------------------
  629. // rrdlabels_migrate_to_these()
  630. // migrate an existing label list to a new list, INPLACE
  631. static int copy_label_to_dictionary_callback(const char *name, void *value, void *data) {
  632. DICTIONARY *dst = (DICTIONARY *)data;
  633. RRDLABEL *lb = (RRDLABEL *)value;
  634. labels_add_already_sanitized(dst, name, string2str(lb->label_value), lb->label_source);
  635. return 1;
  636. }
  637. void rrdlabels_migrate_to_these(DICTIONARY *dst, DICTIONARY *src) {
  638. if(!dst || !src) return;
  639. // remove the RRDLABEL_FLAG_OLD and RRDLABEL_FLAG_NEW from all items
  640. rrdlabels_unmark_all(dst);
  641. // Mark the existing ones as RRDLABEL_FLAG_OLD,
  642. // or the newly added ones as RRDLABEL_FLAG_NEW
  643. dictionary_walkthrough_read(src, copy_label_to_dictionary_callback, dst);
  644. // remove the unmarked dst
  645. rrdlabels_remove_all_unmarked(dst);
  646. }
  647. void rrdlabels_copy(DICTIONARY *dst, DICTIONARY *src) {
  648. if(!dst || !src) return;
  649. dictionary_walkthrough_read(src, copy_label_to_dictionary_callback, dst);
  650. }
  651. // ----------------------------------------------------------------------------
  652. // rrdlabels_match_simple_pattern()
  653. // returns true when there are keys in the dictionary matching a simple pattern
  654. struct simple_pattern_match_name_value {
  655. SIMPLE_PATTERN *pattern;
  656. char equal;
  657. };
  658. static int simple_pattern_match_name_only_callback(const char *name, void *value, void *data) {
  659. struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
  660. (void)value;
  661. // we return -1 to stop the walkthrough on first match
  662. if(simple_pattern_matches(t->pattern, name)) return -1;
  663. return 0;
  664. }
  665. static int simple_pattern_match_name_and_value_callback(const char *name, void *value, void *data) {
  666. struct simple_pattern_match_name_value *t = (struct simple_pattern_match_name_value *)data;
  667. RRDLABEL *lb = (RRDLABEL *)value;
  668. // we return -1 to stop the walkthrough on first match
  669. if(simple_pattern_matches(t->pattern, name)) return -1;
  670. size_t len = RRDLABELS_MAX_NAME_LENGTH + RRDLABELS_MAX_VALUE_LENGTH + 2; // +1 for =, +1 for \0
  671. char tmp[len], *dst = &tmp[0];
  672. const char *v = string2str(lb->label_value);
  673. // copy the name
  674. while(*name) *dst++ = *name++;
  675. // add the equal
  676. *dst++ = t->equal;
  677. // add the value
  678. while(*v) *dst++ = *v++;
  679. // terminate it
  680. *dst = '\0';
  681. if(simple_pattern_matches(t->pattern, tmp)) return -1;
  682. return 0;
  683. }
  684. bool rrdlabels_match_simple_pattern_parsed(DICTIONARY *labels, SIMPLE_PATTERN *pattern, char equal) {
  685. if (!labels) return false;
  686. struct simple_pattern_match_name_value t = {
  687. .pattern = pattern,
  688. .equal = equal
  689. };
  690. int ret = dictionary_walkthrough_read(labels, equal?simple_pattern_match_name_and_value_callback:simple_pattern_match_name_only_callback, &t);
  691. return (ret == -1)?true:false;
  692. }
  693. bool rrdlabels_match_simple_pattern(DICTIONARY *labels, const char *simple_pattern_txt) {
  694. if (!labels) return false;
  695. SIMPLE_PATTERN *pattern = simple_pattern_create(simple_pattern_txt, " ,|\t\r\n\f\v", SIMPLE_PATTERN_EXACT);
  696. char equal = '\0';
  697. const char *s;
  698. for(s = simple_pattern_txt; *s ; s++) {
  699. if (*s == '=' || *s == ':') {
  700. equal = *s;
  701. break;
  702. }
  703. }
  704. bool ret = rrdlabels_match_simple_pattern_parsed(labels, pattern, equal);
  705. simple_pattern_free(pattern);
  706. return ret;
  707. }
  708. // ----------------------------------------------------------------------------
  709. // Log all labels
  710. static int rrdlabels_log_label_to_buffer_callback(const char *name, void *value, void *data) {
  711. BUFFER *wb = (BUFFER *)data;
  712. RRDLABEL *lb = (RRDLABEL *)value;
  713. buffer_sprintf(wb, "Label: %s: \"%s\" (", name, string2str(lb->label_value));
  714. size_t sources = 0;
  715. if(lb->label_source & RRDLABEL_SRC_AUTO) {
  716. buffer_sprintf(wb, "auto");
  717. sources++;
  718. }
  719. if(lb->label_source & RRDLABEL_SRC_CONFIG)
  720. buffer_sprintf(wb, "%snetdata.conf", sources++?",":"");
  721. if(lb->label_source & RRDLABEL_SRC_K8S)
  722. buffer_sprintf(wb, "%sk8s", sources++?",":"");
  723. if(lb->label_source & RRDLABEL_SRC_ACLK)
  724. buffer_sprintf(wb, "%saclk", sources++?",":"");
  725. if(!sources)
  726. buffer_strcat(wb, "unknown");
  727. buffer_strcat(wb, ")\n");
  728. return 1;
  729. }
  730. void rrdlabels_log_to_buffer(DICTIONARY *labels, BUFFER *wb) {
  731. dictionary_sorted_walkthrough_read(labels, rrdlabels_log_label_to_buffer_callback, wb);
  732. }
  733. // ----------------------------------------------------------------------------
  734. // rrdlabels_to_buffer()
  735. struct labels_to_buffer {
  736. BUFFER *wb;
  737. bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  738. void *filter_data;
  739. void (*name_sanitizer)(char *dst, const char *src, size_t dst_size);
  740. void (*value_sanitizer)(char *dst, const char *src, size_t dst_size);
  741. const char *before_each;
  742. const char *quote;
  743. const char *equal;
  744. const char *between_them;
  745. size_t count;
  746. };
  747. static int label_to_buffer_callback(const char *name, void *value, void *data) {
  748. struct labels_to_buffer *t = (struct labels_to_buffer *)data;
  749. RRDLABEL *lb = (RRDLABEL *)value;
  750. size_t n_size = (t->name_sanitizer ) ? ( RRDLABELS_MAX_NAME_LENGTH * 2 ) : 1;
  751. size_t v_size = (t->value_sanitizer) ? ( RRDLABELS_MAX_VALUE_LENGTH * 2 ) : 1;
  752. char n[n_size];
  753. char v[v_size];
  754. const char *nn = name, *vv = string2str(lb->label_value);
  755. if(t->name_sanitizer) {
  756. t->name_sanitizer(n, name, n_size);
  757. nn = n;
  758. }
  759. if(t->value_sanitizer) {
  760. t->value_sanitizer(v, string2str(lb->label_value), v_size);
  761. vv = v;
  762. }
  763. if(!t->filter_callback || t->filter_callback(name, string2str(lb->label_value), lb->label_source, t->filter_data)) {
  764. buffer_sprintf(t->wb, "%s%s%s%s%s%s%s%s%s", t->count++?t->between_them:"", t->before_each, t->quote, nn, t->quote, t->equal, t->quote, vv, t->quote);
  765. return 1;
  766. }
  767. return 0;
  768. }
  769. int rrdlabels_to_buffer(DICTIONARY *labels, BUFFER *wb, const char *before_each, const char *equal, const char *quote, const char *between_them, bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *filter_data, void (*name_sanitizer)(char *dst, const char *src, size_t dst_size), void (*value_sanitizer)(char *dst, const char *src, size_t dst_size)) {
  770. struct labels_to_buffer tmp = {
  771. .wb = wb,
  772. .filter_callback = filter_callback,
  773. .filter_data = filter_data,
  774. .name_sanitizer = name_sanitizer,
  775. .value_sanitizer = value_sanitizer,
  776. .before_each = before_each,
  777. .equal = equal,
  778. .quote = quote,
  779. .between_them = between_them,
  780. .count = 0
  781. };
  782. return dictionary_walkthrough_read(labels, label_to_buffer_callback, (void *)&tmp);
  783. }
  784. static int chart_label_store_to_sql_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
  785. RRDSET *st = (RRDSET *)data;
  786. sql_store_chart_label(st->chart_uuid, (int)ls, (char *)name, (char *)value);
  787. return 1;
  788. }
  789. void rrdset_update_rrdlabels(RRDSET *st, DICTIONARY *new_rrdlabels) {
  790. if(!st->state->chart_labels)
  791. st->state->chart_labels = rrdlabels_create();
  792. if (new_rrdlabels)
  793. rrdlabels_migrate_to_these(st->state->chart_labels, new_rrdlabels);
  794. // TODO - we should also cleanup sqlite from old new_rrdlabels that have been removed
  795. rrdlabels_walkthrough_read(st->state->chart_labels, chart_label_store_to_sql_callback, st);
  796. }
  797. // ----------------------------------------------------------------------------
  798. // rrdlabels unit test
  799. struct rrdlabels_unittest_add_a_pair {
  800. const char *pair;
  801. const char *expected_name;
  802. const char *expected_value;
  803. const char *name;
  804. const char *value;
  805. RRDLABEL_SRC ls;
  806. int errors;
  807. };
  808. int rrdlabels_unittest_add_a_pair_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
  809. struct rrdlabels_unittest_add_a_pair *t = (struct rrdlabels_unittest_add_a_pair *)data;
  810. t->name = name;
  811. t->value = value;
  812. t->ls = ls;
  813. if(strcmp(name, t->expected_name) != 0) {
  814. fprintf(stderr, "name is wrong, found \"%s\", expected \"%s\"", name, t->expected_name);
  815. t->errors++;
  816. }
  817. if(value == NULL && t->expected_value == NULL) {
  818. ;
  819. }
  820. else if(value == NULL || t->expected_value == NULL) {
  821. fprintf(stderr, "value is wrong, found \"%s\", expected \"%s\"", value?value:"(null)", t->expected_value?t->expected_value:"(null)");
  822. t->errors++;
  823. }
  824. else if(strcmp(value, t->expected_value) != 0) {
  825. fprintf(stderr, "values don't match, found \"%s\", expected \"%s\"", value, t->expected_value);
  826. t->errors++;
  827. }
  828. return 1;
  829. }
  830. int rrdlabels_unittest_add_a_pair(const char *pair, const char *name, const char *value) {
  831. DICTIONARY *labels = rrdlabels_create();
  832. int errors;
  833. fprintf(stderr, "rrdlabels_add_pair(labels, %s) ... ", pair);
  834. rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
  835. struct rrdlabels_unittest_add_a_pair tmp = {
  836. .pair = pair,
  837. .expected_name = name,
  838. .expected_value = value,
  839. .errors = 0
  840. };
  841. int ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
  842. errors = tmp.errors;
  843. if(ret != 1) {
  844. fprintf(stderr, "failed to get \"%s\" label", name);
  845. errors++;
  846. }
  847. if(!errors)
  848. fprintf(stderr, " OK, name='%s' and value='%s'\n", tmp.name, tmp.value?tmp.value:"(null)");
  849. else
  850. fprintf(stderr, " FAILED\n");
  851. rrdlabels_destroy(labels);
  852. return errors;
  853. }
  854. int rrdlabels_unittest_add_pairs() {
  855. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  856. int errors = 0;
  857. // basic test
  858. errors += rrdlabels_unittest_add_a_pair("tag=value", "tag", "value");
  859. errors += rrdlabels_unittest_add_a_pair("tag:value", "tag", "value");
  860. // test newlines
  861. errors += rrdlabels_unittest_add_a_pair(" tag = \t value \r\n", "tag", "value");
  862. // test : in values
  863. errors += rrdlabels_unittest_add_a_pair("tag=:value", "tag", ":value");
  864. errors += rrdlabels_unittest_add_a_pair("tag::value", "tag", ":value");
  865. errors += rrdlabels_unittest_add_a_pair(" tag = :value ", "tag", ":value");
  866. errors += rrdlabels_unittest_add_a_pair(" tag : :value ", "tag", ":value");
  867. errors += rrdlabels_unittest_add_a_pair("tag:5", "tag", "5");
  868. errors += rrdlabels_unittest_add_a_pair("tag:55", "tag", "55");
  869. errors += rrdlabels_unittest_add_a_pair("tag:aa", "tag", "aa");
  870. errors += rrdlabels_unittest_add_a_pair("tag:a", "tag", "a");
  871. // test empty values
  872. errors += rrdlabels_unittest_add_a_pair("tag", "tag", "[none]");
  873. errors += rrdlabels_unittest_add_a_pair("tag:", "tag", "[none]");
  874. errors += rrdlabels_unittest_add_a_pair("tag:\"\"", "tag", "[none]");
  875. errors += rrdlabels_unittest_add_a_pair("tag:''", "tag", "[none]");
  876. errors += rrdlabels_unittest_add_a_pair("tag:\r\n", "tag", "[none]");
  877. errors += rrdlabels_unittest_add_a_pair("tag\r\n", "tag", "[none]");
  878. // test UTF-8 in values
  879. errors += rrdlabels_unittest_add_a_pair("tag: country:Ελλάδα", "tag", "country:Ελλάδα");
  880. errors += rrdlabels_unittest_add_a_pair("\"tag\": \"country:Ελλάδα\"", "tag", "country:Ελλάδα");
  881. errors += rrdlabels_unittest_add_a_pair("\"tag\": country:\"Ελλάδα\"", "tag", "country:Ελλάδα");
  882. errors += rrdlabels_unittest_add_a_pair("\"tag=1\": country:\"Gre\\\"ece\"", "tag_1", "country:Gre_ece");
  883. errors += rrdlabels_unittest_add_a_pair("\"tag=1\" = country:\"Gre\\\"ece\"", "tag_1", "country:Gre_ece");
  884. errors += rrdlabels_unittest_add_a_pair("\t'LABE=L'\t=\t\"World\" peace", "labe_l", "World peace");
  885. errors += rrdlabels_unittest_add_a_pair("\t'LA\\'B:EL'\t=\tcountry:\"World\":\"Europe\":\"Greece\"", "la_b_el", "country:World:Europe:Greece");
  886. errors += rrdlabels_unittest_add_a_pair("\t'LA\\'B:EL'\t=\tcountry\\\"World\"\\\"Europe\"\\\"Greece\"", "la_b_el", "country/World/Europe/Greece");
  887. errors += rrdlabels_unittest_add_a_pair("NAME=\"VALUE\"", "name", "VALUE");
  888. errors += rrdlabels_unittest_add_a_pair("\"NAME\" : \"VALUE\"", "name", "VALUE");
  889. errors += rrdlabels_unittest_add_a_pair("NAME: \"VALUE\"", "name", "VALUE");
  890. return errors;
  891. }
  892. int rrdlabels_unittest_check_simple_pattern(DICTIONARY *labels, const char *pattern, bool expected) {
  893. fprintf(stderr, "rrdlabels_match_simple_pattern(labels, \"%s\") ... ", pattern);
  894. bool ret = rrdlabels_match_simple_pattern(labels, pattern);
  895. fprintf(stderr, "%s, got %s expected %s\n", (ret == expected)?"OK":"FAILED", ret?"true":"false", expected?"true":"false");
  896. return (ret == expected)?0:1;
  897. }
  898. int rrdlabels_unittest_simple_pattern() {
  899. fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
  900. int errors = 0;
  901. DICTIONARY *labels = rrdlabels_create();
  902. rrdlabels_add(labels, "tag1", "value1", RRDLABEL_SRC_CONFIG);
  903. rrdlabels_add(labels, "tag2", "value2", RRDLABEL_SRC_CONFIG);
  904. rrdlabels_add(labels, "tag3", "value3", RRDLABEL_SRC_CONFIG);
  905. errors += rrdlabels_unittest_check_simple_pattern(labels, "*", true);
  906. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag", false);
  907. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag*", true);
  908. errors += rrdlabels_unittest_check_simple_pattern(labels, "*1", true);
  909. errors += rrdlabels_unittest_check_simple_pattern(labels, "value*", false);
  910. errors += rrdlabels_unittest_check_simple_pattern(labels, "*=value*", true);
  911. errors += rrdlabels_unittest_check_simple_pattern(labels, "*:value*", true);
  912. errors += rrdlabels_unittest_check_simple_pattern(labels, "*2", true);
  913. errors += rrdlabels_unittest_check_simple_pattern(labels, "*2 *3", true);
  914. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag3 *2", true);
  915. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1 tag2", true);
  916. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1tag2", false);
  917. errors += rrdlabels_unittest_check_simple_pattern(labels, "invalid1 invalid2 tag3", true);
  918. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag1 tag4", false);
  919. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1=value1", true);
  920. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag1=value2", false);
  921. errors += rrdlabels_unittest_check_simple_pattern(labels, "tag*=value*", true);
  922. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag*=value*", false);
  923. errors += rrdlabels_unittest_check_simple_pattern(labels, "!tag2=something2 tag2=*2", true);
  924. rrdlabels_destroy(labels);
  925. return errors;
  926. }
  927. int rrdlabels_unittest_sanitize_value(const char *src, const char *expected) {
  928. char buf[RRDLABELS_MAX_VALUE_LENGTH + 1];
  929. size_t mblen = rrdlabels_sanitize_value(buf, src, RRDLABELS_MAX_VALUE_LENGTH);
  930. int err = 0;
  931. if(strcmp(buf, expected) != 0) err = 1;
  932. fprintf(stderr, "%s(%s): %s, expected '%s', got '%s', mblen = %zu, bytes = %zu\n", __FUNCTION__, src, (err==1)?"FAILED":"OK", expected, buf, mblen, strlen(buf));
  933. return err;
  934. }
  935. int rrdlabels_unittest_sanitization() {
  936. int errors = 0;
  937. errors += rrdlabels_unittest_sanitize_value("", "[none]");
  938. errors += rrdlabels_unittest_sanitize_value("1", "1");
  939. errors += rrdlabels_unittest_sanitize_value(" hello world ", "hello world");
  940. // 2-byte UTF-8
  941. errors += rrdlabels_unittest_sanitize_value(" Ελλάδα ", "Ελλάδα");
  942. errors += rrdlabels_unittest_sanitize_value("aŰbŲcŴ", "aŰbŲcŴ");
  943. errors += rrdlabels_unittest_sanitize_value("Ű b Ų c Ŵ", "Ű b Ų c Ŵ");
  944. // 3-byte UTF-8
  945. errors += rrdlabels_unittest_sanitize_value("‱", "‱");
  946. errors += rrdlabels_unittest_sanitize_value("a‱b", "a‱b");
  947. errors += rrdlabels_unittest_sanitize_value("a ‱ b", "a ‱ b");
  948. // 4-byte UTF-8
  949. errors += rrdlabels_unittest_sanitize_value("𩸽", "𩸽");
  950. errors += rrdlabels_unittest_sanitize_value("a𩸽b", "a𩸽b");
  951. errors += rrdlabels_unittest_sanitize_value("a 𩸽 b", "a 𩸽 b");
  952. // mixed multi-byte
  953. errors += rrdlabels_unittest_sanitize_value("Ű‱𩸽‱Ű", "Ű‱𩸽‱Ű");
  954. return errors;
  955. }
  956. int rrdlabels_unittest(void) {
  957. int errors = 0;
  958. errors += rrdlabels_unittest_sanitization();
  959. errors += rrdlabels_unittest_add_pairs();
  960. errors += rrdlabels_unittest_simple_pattern();
  961. fprintf(stderr, "%d errors found\n", errors);
  962. return errors;
  963. }