Skip to content

Commit c278f4b

Browse files
committed
fix(python): drop always-false bounds from namespace import capture
The __all__ literal guard compared a uint32_t child count against SIZE_MAX / sizeof(char *), which is always false on LP64 and failed the GCC build (-Werror=type-limits). One bound now covers both limits on every platform: the names are counted in an int, and count * sizeof(*names) <= INT_MAX also fits a 32-bit size_t. Building one import fact moves into py_ns_import_fact, and the module-name child is skipped with ts_node_eq: for a plain import the module is the null node, which equals no child. The loop no longer tests `from` after the from-only prelude, which cppcheck 2.20 misread as always false (knownConditionTrueFalse). Emitted facts are unchanged. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent de1819e commit c278f4b

1 file changed

Lines changed: 44 additions & 40 deletions

File tree

‎internal/cbm/extract_imports.c‎

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,43 @@ static bool py_ns_class_safe(PyNamespaceBuilder *b, TSNode node) {
550550
return safe;
551551
}
552552

553+
/* One imported name of an import statement: `import a.b [as c]` binds the root
554+
* `a` (or the alias), `from m import x [as y]` binds `x` (or the alias), and
555+
* `from m import *` is a star. False for an unsupported child or a failed copy. */
556+
static bool py_ns_import_fact(PyNamespaceBuilder *b, TSNode child, bool from,
557+
const char *module_name, uint32_t level, CBMPyNamespaceFact *out) {
558+
const char *kind = ts_node_type(child);
559+
CBMPyNamespaceFact f = {.kind = from ? CBM_PY_NS_IMPORT_NAME : CBM_PY_NS_IMPORT_MODULE,
560+
.module_name = module_name,
561+
.relative_level = level};
562+
if (from && strcmp(kind, "wildcard_import") == 0) {
563+
f.kind = CBM_PY_NS_IMPORT_STAR;
564+
*out = f;
565+
return true;
566+
}
567+
bool alias = strcmp(kind, "aliased_import") == 0;
568+
if (!alias && strcmp(kind, "dotted_name") != 0 && strcmp(kind, "identifier") != 0)
569+
return false;
570+
TSNode name = alias ? ts_node_child_by_field_name(child, TS_FIELD("name")) : child;
571+
const char *imported = py_ns_name(b, name, !from);
572+
if (!imported)
573+
return false;
574+
if (from)
575+
f.member_name = imported;
576+
else
577+
f.module_name = imported;
578+
if (alias) {
579+
f.local_name = py_ns_name(b, ts_node_child_by_field_name(child, TS_FIELD("alias")), false);
580+
} else if (from) {
581+
f.local_name = imported;
582+
} else {
583+
f.local_name = py_ns_copy(b, imported, strcspn(imported, "."));
584+
f.flags = CBM_PY_NS_IMPORT_BINDS_ROOT;
585+
}
586+
*out = f;
587+
return f.local_name != NULL;
588+
}
589+
553590
static bool py_ns_import(PyNamespaceBuilder *b, TSNode node, bool from) {
554591
TSNode module = from ? ts_node_child_by_field_name(node, TS_FIELD("module_name")) : (TSNode){0};
555592
const char *module_name = NULL;
@@ -573,47 +610,12 @@ static bool py_ns_import(PyNamespaceBuilder *b, TSNode node, bool from) {
573610
if (ts_tree_cursor_goto_first_child(&cursor)) {
574611
do {
575612
TSNode child = ts_tree_cursor_current_node(&cursor);
576-
if (!ts_node_is_named(child) || (from && child.id == module.id) ||
613+
/* a plain import has no module node: the null node equals no child */
614+
if (!ts_node_is_named(child) || ts_node_eq(child, module) ||
577615
strcmp(ts_node_type(child), "comment") == 0)
578616
continue;
579-
const char *kind = ts_node_type(child);
580-
CBMPyNamespaceFact f = {.kind = from ? CBM_PY_NS_IMPORT_NAME : CBM_PY_NS_IMPORT_MODULE,
581-
.module_name = module_name,
582-
.relative_level = level};
583-
if (from && strcmp(kind, "wildcard_import") == 0) {
584-
f.kind = CBM_PY_NS_IMPORT_STAR;
585-
} else {
586-
bool alias = strcmp(kind, "aliased_import") == 0;
587-
TSNode name = alias ? ts_node_child_by_field_name(child, TS_FIELD("name")) : child;
588-
if (!alias && strcmp(kind, "dotted_name") != 0 && strcmp(kind, "identifier") != 0) {
589-
ok = false;
590-
break;
591-
}
592-
const char *imported = py_ns_name(b, name, !from);
593-
if (!imported) {
594-
ok = false;
595-
break;
596-
}
597-
if (from)
598-
f.member_name = imported;
599-
else
600-
f.module_name = imported;
601-
if (alias) {
602-
f.local_name =
603-
py_ns_name(b, ts_node_child_by_field_name(child, TS_FIELD("alias")), false);
604-
} else if (from) {
605-
f.local_name = imported;
606-
} else {
607-
size_t n = strcspn(imported, ".");
608-
f.local_name = py_ns_copy(b, imported, n);
609-
f.flags = CBM_PY_NS_IMPORT_BINDS_ROOT;
610-
}
611-
if (!f.local_name) {
612-
ok = false;
613-
break;
614-
}
615-
}
616-
if (!py_ns_push(b, f)) {
617+
CBMPyNamespaceFact f;
618+
if (!py_ns_import_fact(b, child, from, module_name, level, &f) || !py_ns_push(b, f)) {
617619
ok = false;
618620
break;
619621
}
@@ -630,8 +632,10 @@ static bool py_ns_all_literal(PyNamespaceBuilder *b, TSNode node, CBMPyNamespace
630632
const char *kind = ts_node_type(node);
631633
if (strcmp(kind, "list") != 0 && strcmp(kind, "tuple") != 0)
632634
return false;
635+
/* One bound for both limits, on every platform: the names are counted in an
636+
* int, and count * sizeof(*names) <= INT_MAX fits even a 32-bit size_t. */
633637
uint32_t count = ts_node_named_child_count(node);
634-
if (count > INT_MAX || (size_t)count > SIZE_MAX / sizeof(*fact->names)) {
638+
if (count > INT_MAX / sizeof(*fact->names)) {
635639
b->failure = CBM_PY_NS_FAILURE_LIMIT;
636640
return false;
637641
}

0 commit comments

Comments
 (0)