Skip to content

Commit 8d93378

Browse files
committed
lib: ctraces: upgrade to v0.3.0
Signed-off-by: Eduardo Silva <[email protected]>
1 parent caf63dd commit 8d93378

File tree

5 files changed

+172
-16
lines changed

5 files changed

+172
-16
lines changed

lib/ctraces/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ endif()
2626

2727
# CTraces Version
2828
set(CTR_VERSION_MAJOR 0)
29-
set(CTR_VERSION_MINOR 2)
30-
set(CTR_VERSION_PATCH 7)
29+
set(CTR_VERSION_MINOR 3)
30+
set(CTR_VERSION_PATCH 0)
3131
set(CTR_VERSION_STR "${CTR_VERSION_MAJOR}.${CTR_VERSION_MINOR}.${CTR_VERSION_PATCH}")
3232

3333
# Define __FILENAME__ consistently across Operating Systems

lib/ctraces/include/ctraces/ctr_id.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ int ctr_id_cmp(struct ctrace_id *cid1, struct ctrace_id *cid2);
3939
size_t ctr_id_get_len(struct ctrace_id *cid);
4040
void *ctr_id_get_buf(struct ctrace_id *cid);
4141
cfl_sds_t ctr_id_to_lower_base16(struct ctrace_id *cid);
42+
struct ctrace_id *ctr_id_from_base16(cfl_sds_t id);
4243

4344
#endif

lib/ctraces/src/ctr_decode_msgpack.c

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,14 @@ static int unpack_link_trace_id(mpack_reader_t *reader, size_t index, void *ctx)
236236
int result;
237237
cfl_sds_t value;
238238

239-
result = ctr_mpack_consume_binary_tag(reader, &value);
239+
result = ctr_mpack_consume_string_or_nil_tag(reader, &value);
240240

241-
if (result == CTR_MPACK_SUCCESS) {
242-
context->link->trace_id = ctr_id_create(value, cfl_sds_len(value));
241+
if (result == CTR_MPACK_SUCCESS && value != NULL) {
242+
context->link->trace_id = ctr_id_from_base16(value);
243+
244+
if (context->link->trace_id == NULL) {
245+
result = CTR_MPACK_CORRUPT_INPUT_DATA_ERROR;
246+
}
243247

244248
cfl_sds_destroy(value);
245249
}
@@ -253,11 +257,14 @@ static int unpack_link_span_id(mpack_reader_t *reader, size_t index, void *ctx)
253257
int result;
254258
cfl_sds_t value;
255259

256-
result = ctr_mpack_consume_binary_or_nil_tag(reader, &value);
260+
result = ctr_mpack_consume_string_or_nil_tag(reader, &value);
257261

258-
if (result == CTR_MPACK_SUCCESS &&
259-
value != NULL) {
260-
context->link->span_id = ctr_id_create(value, cfl_sds_len(value));
262+
if (result == CTR_MPACK_SUCCESS && value != NULL) {
263+
context->link->span_id = ctr_id_from_base16(value);
264+
265+
if (context->link->span_id == NULL) {
266+
result = CTR_MPACK_CORRUPT_INPUT_DATA_ERROR;
267+
}
261268

262269
cfl_sds_destroy(value);
263270
}
@@ -339,13 +346,23 @@ static int unpack_link(mpack_reader_t *reader, size_t index, void *ctx)
339346
static int unpack_span_trace_id(mpack_reader_t *reader, size_t index, void *ctx)
340347
{
341348
struct ctr_msgpack_decode_context *context = ctx;
349+
struct ctrace_id *decoded_id;
342350
int result;
343351
cfl_sds_t value;
344352

345-
result = ctr_mpack_consume_binary_or_nil_tag(reader, &value);
353+
result = ctr_mpack_consume_string_or_nil_tag(reader, &value);
346354

347355
if (result == CTR_MPACK_SUCCESS && value != NULL) {
348-
ctr_span_set_trace_id(context->span, value, cfl_sds_len(value));
356+
decoded_id = ctr_id_from_base16(value);
357+
358+
if (decoded_id != NULL) {
359+
ctr_span_set_trace_id_with_cid(context->span, decoded_id);
360+
361+
ctr_id_destroy(decoded_id);
362+
}
363+
else {
364+
result = CTR_MPACK_CORRUPT_INPUT_DATA_ERROR;
365+
}
349366

350367
cfl_sds_destroy(value);
351368
}
@@ -356,13 +373,23 @@ static int unpack_span_trace_id(mpack_reader_t *reader, size_t index, void *ctx)
356373
static int unpack_span_span_id(mpack_reader_t *reader, size_t index, void *ctx)
357374
{
358375
struct ctr_msgpack_decode_context *context = ctx;
376+
struct ctrace_id *decoded_id;
359377
int result;
360378
cfl_sds_t value;
361379

362-
result = ctr_mpack_consume_binary_or_nil_tag(reader, &value);
380+
result = ctr_mpack_consume_string_or_nil_tag(reader, &value);
363381

364382
if (result == CTR_MPACK_SUCCESS && value != NULL) {
365-
ctr_span_set_span_id(context->span, value, cfl_sds_len(value));
383+
decoded_id = ctr_id_from_base16(value);
384+
385+
if (decoded_id != NULL) {
386+
ctr_span_set_span_id_with_cid(context->span, decoded_id);
387+
388+
ctr_id_destroy(decoded_id);
389+
}
390+
else {
391+
result = CTR_MPACK_CORRUPT_INPUT_DATA_ERROR;
392+
}
366393

367394
cfl_sds_destroy(value);
368395
}
@@ -373,13 +400,23 @@ static int unpack_span_span_id(mpack_reader_t *reader, size_t index, void *ctx)
373400
static int unpack_span_parent_span_id(mpack_reader_t *reader, size_t index, void *ctx)
374401
{
375402
struct ctr_msgpack_decode_context *context = ctx;
403+
struct ctrace_id *decoded_id;
376404
int result;
377405
cfl_sds_t value;
378406

379-
result = ctr_mpack_consume_binary_or_nil_tag(reader, &value);
407+
result = ctr_mpack_consume_string_or_nil_tag(reader, &value);
380408

381409
if (result == CTR_MPACK_SUCCESS && value != NULL) {
382-
ctr_span_set_parent_span_id(context->span, value, cfl_sds_len(value));
410+
decoded_id = ctr_id_from_base16(value);
411+
412+
if (decoded_id != NULL) {
413+
ctr_span_set_parent_span_id_with_cid(context->span, decoded_id);
414+
415+
ctr_id_destroy(decoded_id);
416+
}
417+
else {
418+
result = CTR_MPACK_CORRUPT_INPUT_DATA_ERROR;
419+
}
383420

384421
cfl_sds_destroy(value);
385422
}

lib/ctraces/src/ctr_encode_msgpack.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,24 @@ static void pack_instrumentation_scope(mpack_writer_t *writer, struct ctrace_ins
175175

176176
static void pack_id(mpack_writer_t *writer, struct ctrace_id *id)
177177
{
178+
cfl_sds_t encoded_id;
179+
180+
178181
if (id) {
179-
mpack_write_bin(writer, id->buf, cfl_sds_len(id->buf));
182+
encoded_id = ctr_id_to_lower_base16(id);
183+
184+
if (encoded_id != NULL) {
185+
mpack_write_cstr(writer, encoded_id);
186+
187+
cfl_sds_destroy(encoded_id);
188+
}
189+
else {
190+
/* we should be able to report this but at the moment
191+
* we are not.
192+
*/
193+
194+
mpack_write_nil(writer);
195+
}
180196
}
181197
else {
182198
mpack_write_nil(writer);

lib/ctraces/src/ctr_id.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,105 @@ cfl_sds_t ctr_id_to_lower_base16(struct ctrace_id *cid)
151151

152152
return out;
153153
}
154+
155+
/* This function returns CFL_TRUE on success and CFL_FALSE on
156+
* failure.
157+
*/
158+
static int decode_hex_digit(char *digit)
159+
{
160+
if (*digit >= '0' && *digit <= '9') {
161+
*digit -= '0';
162+
}
163+
else if (*digit >= 'a' && *digit <= 'f') {
164+
*digit -= 'a';
165+
*digit += 10;
166+
}
167+
else if (*digit >= 'A' && *digit <= 'F') {
168+
*digit -= 'A';
169+
*digit += 10;
170+
}
171+
else {
172+
return CFL_FALSE;
173+
}
174+
175+
return CFL_TRUE;
176+
}
177+
178+
struct ctrace_id *ctr_id_from_base16(cfl_sds_t id)
179+
{
180+
size_t output_index;
181+
size_t input_index;
182+
cfl_sds_t decoded_id;
183+
struct ctrace_id *result_id;
184+
int result;
185+
size_t length;
186+
char digit;
187+
char value;
188+
189+
if (id == NULL) {
190+
return NULL;
191+
}
192+
193+
length = cfl_sds_len(id);
194+
195+
if (length < 2) {
196+
return NULL;
197+
}
198+
199+
if ((length % 2) != 0) {
200+
return NULL;
201+
}
202+
203+
decoded_id = cfl_sds_create_size(length / 2);
204+
205+
if (decoded_id == NULL) {
206+
return NULL;
207+
}
208+
209+
output_index = 0;
210+
input_index = 0;
211+
value = 0;
212+
213+
/* This loop consumes one character per iteration,
214+
* on each iteration it verifies that the character
215+
* corresponds to the base16 charset and then
216+
* it subtracts the correct base to get a number
217+
* ranging from 0 to 16.
218+
* Then the accumulator is left shifted 4 bits and
219+
* the current value is bitwise ORed to its value.
220+
* If the character index is odd then the accumulator
221+
* value is appended to the decoded id buffer and
222+
* reinitialized to be used on the next iteration.
223+
*/
224+
225+
while (input_index < length) {
226+
digit = id[input_index];
227+
result = decode_hex_digit(&digit);
228+
229+
if (!result) {
230+
break;
231+
}
232+
233+
digit &= 0xF;
234+
value <<= 4;
235+
value |= digit;
236+
237+
if ((input_index % 2) == 1) {
238+
decoded_id[output_index++] = value;
239+
value = 0;
240+
}
241+
242+
input_index++;
243+
}
244+
245+
if (result) {
246+
result_id = ctr_id_create(decoded_id, length / 2);
247+
}
248+
else {
249+
result_id = NULL;
250+
}
251+
252+
cfl_sds_destroy(decoded_id);
253+
254+
return result_id;
255+
}

0 commit comments

Comments
 (0)