Skip to content

Commit deb3cb1

Browse files
authored
Merge pull request #81 from imakris/codex/find-and-fix-dangling-pointers-and-references
Fix GET_PRS temporary lifetime bug in logging
2 parents a7a6142 + bb08548 commit deb3cb1

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

include/glatter/glatter_def.h

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,34 +1433,34 @@ void glatter_bind_owner_to_current_thread(void)
14331433
#endif
14341434
}
14351435

1436-
typedef struct
1437-
{
1438-
char data[3*16+2];
1439-
}
1440-
Printable;
1441-
1442-
1436+
/* Format up to 16 bytes of storage as a printable hex string. The
1437+
* logging helpers pass the resulting pointer outside this helper, so we
1438+
* maintain a small TLS ring buffer to keep the string alive long enough
1439+
* for the log handler to consume it without dangling. */
14431440
GLATTER_INLINE_OR_NOT
1444-
Printable get_prs(size_t sz, void* obj)
1441+
const char* get_prs(size_t sz, const void* obj)
14451442
{
1446-
Printable ret;
1447-
memset(&ret, 0, sizeof ret);
1448-
const unsigned char *bytes = (const unsigned char*)obj;
1449-
size_t cap = sizeof(ret.data);
1443+
enum { GLATTER_PRS_SLOTS = 8, GLATTER_PRS_CAP = 3*16 + 4 };
1444+
static GLATTER_THREAD_LOCAL char glatter_prs_buf[GLATTER_PRS_SLOTS][GLATTER_PRS_CAP];
1445+
static GLATTER_THREAD_LOCAL unsigned glatter_prs_index;
1446+
1447+
char* out = glatter_prs_buf[glatter_prs_index++ % GLATTER_PRS_SLOTS];
1448+
size_t cap = GLATTER_PRS_CAP;
14501449
size_t pos = 0;
14511450

14521451
if (cap == 0) {
1453-
return ret;
1452+
return "";
14541453
}
14551454

1456-
ret.data[pos++] = '[';
1455+
out[pos++] = '[';
14571456

1457+
const unsigned char* bytes = (const unsigned char*)obj;
14581458
if (sz > 16) {
14591459
sz = 16;
14601460
}
14611461

1462-
if (sz > 0) {
1463-
int n = snprintf(ret.data + pos, cap - pos, "%02x", bytes[0]);
1462+
if (sz > 0 && pos < cap) {
1463+
int n = snprintf(out + pos, cap - pos, "%02x", bytes[0]);
14641464
if (n < 0) {
14651465
n = 0;
14661466
}
@@ -1470,7 +1470,7 @@ Printable get_prs(size_t sz, void* obj)
14701470
else {
14711471
pos += (size_t)n;
14721472
for (size_t i = 1; i < sz && pos < cap; ++i) {
1473-
n = snprintf(ret.data + pos, cap - pos, " %02x", bytes[i]);
1473+
n = snprintf(out + pos, cap - pos, " %02x", bytes[i]);
14741474
if (n < 0) {
14751475
break;
14761476
}
@@ -1484,16 +1484,15 @@ Printable get_prs(size_t sz, void* obj)
14841484
}
14851485

14861486
if (pos < cap - 1) {
1487-
ret.data[pos++] = ']';
1487+
out[pos++] = ']';
14881488
}
1489-
else
1490-
if (cap >= 2) {
1491-
ret.data[cap - 2] = ']';
1489+
else if (cap >= 2) {
1490+
out[cap - 2] = ']';
14921491
pos = cap - 1;
14931492
}
14941493

1495-
ret.data[pos] = '\0';
1496-
return ret;
1494+
out[pos] = '\0';
1495+
return out;
14971496
}
14981497

14991498

@@ -1684,7 +1683,7 @@ void glatter_dbg_return(const char* fmt, ...)
16841683
#endif
16851684

16861685
#define GET_PRS(v)\
1687-
(get_prs(sizeof(v), (void*)(&(v))).data)
1686+
(get_prs(sizeof(v), (const void*)(&(v))))
16881687

16891688

16901689
#if defined(__llvm__) || defined (__clang__)

0 commit comments

Comments
 (0)