Commit 62b1437b
62b1437b1c78Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(parse): sp_canonical_path never returns NULL (#749)
realpath/_fullpath + strdup can both fail under OOM, leaving callers with a NULL canonical pointer that then went to strcmp(NULL, ...) for the dedup check -- segfault under memory pressure.
Fallback path: try realpath; on NULL, try strdup(path); on NULL, return strdup(""). Callers free the returned buffer, so the empty string can't be a string literal.
Belt-and-suspenders: sp_path_already_included also guards each side of the strcmp against NULL, so a partially-corrupted sp_included_paths table (from a failed strdup at insert time) can't crash the lookup either.
Closes #749
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 62b1437b1c78
- Parent
- f44f2542825a
spinel_parse.cmodifiedView file
/* Resolve a path to its canonical form for dedup. realpath() returns NULL on missing files; in that case fall back to the literal path. *//* Issue #749: realpath/_fullpath + strdup can both fail under OOM, leaving callers with a NULL pointer that goes to strcmp(NULL, ...) and segfaults. Fall back to a static empty string so downstream `strcmp(canonical, ...) == 0` consistently misses (the caller then treats this as "not already included" and proceeds with the file read, which fails through the normal LoadError path). */static char *sp_canonical_path(const char *path) { if (!path) { char *e = strdup(""); return e ? e : NULL; }#ifdef _WIN32 char *real = _fullpath(NULL, path, 0);#else char *real = realpath(path, NULL);#endif return real ? real : strdup(path); if (real) return real; char *d = strdup(path); if (d) return d; /* Last resort: return a static empty string to avoid NULL. The buffer is read-only by callers (passed to strcmp, then freed by the caller's free() -- so allocate a fresh empty string instead of returning a string literal). */ return strdup("");}static int sp_path_already_included(const char *canonical) { if (!canonical) return 0; for (int i = 0; i < sp_included_count; i++) { if (strcmp(sp_included_paths[i], canonical) == 0) return 1; if (sp_included_paths[i] && strcmp(sp_included_paths[i], canonical) == 0) return 1; } return 0;}