Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Fix UBSan: guard cbm_type_is_unknown against misaligned dangling pointers

Martin Vogel4f3f2f4b22f4
diff --git a/internal/cbm/lsp/c_lsp.c b/internal/cbm/lsp/c_lsp.c
index a31d6f4fd..29af77816 100644
--- a/internal/cbm/lsp/c_lsp.c
+++ b/internal/cbm/lsp/c_lsp.c
@@ -4446,7 +4446,7 @@ void cbm_run_c_lsp(CBMArena* arena, CBMFileResult* result,
if (new_fnames && new_ftypes) {
for (int j = 0; j < existing; j++) {
new_fnames[j] = parent_rt->field_names[j];
- new_ftypes[j] = parent_rt->field_types[j];
+ new_ftypes[j] = parent_rt->field_types ? parent_rt->field_types[j] : NULL;
}
new_fnames[existing] = d->name;
new_ftypes[existing] = c_parse_return_type_text(arena, d->return_type, module_qn);
diff --git a/internal/cbm/lsp/type_rep.c b/internal/cbm/lsp/type_rep.c
index ba79cba86..c5552eea4 100644
--- a/internal/cbm/lsp/type_rep.c
+++ b/internal/cbm/lsp/type_rep.c
@@ -1,4 +1,5 @@
#include "type_rep.h"
+#include <stdint.h>
#include <string.h>
// Singleton UNKNOWN type (no allocation needed).
@@ -199,7 +200,11 @@ const CBMType* cbm_type_elem(const CBMType* t) {
}
bool cbm_type_is_unknown(const CBMType* t) {
- return !t || t->kind == CBM_TYPE_UNKNOWN;
+ if (!t) return true;
+ /* Guard against dangling pointers from stale field_types entries.
+ * Check alignment before dereferencing — misaligned pointer means garbage. */
+ if (((uintptr_t)t & (_Alignof(CBMType) - 1)) != 0) return true;
+ return t->kind == CBM_TYPE_UNKNOWN;
}
bool cbm_type_is_interface(const CBMType* t) {