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

@@ -1322,18 +1322,33 @@
13221322
13231323 /* Resolve a path to its canonical form for dedup. realpath() returns NULL
13241324 on missing files; in that case fall back to the literal path. */
1325+/* Issue #749: realpath/_fullpath + strdup can both fail under OOM,
1326+ leaving callers with a NULL pointer that goes to strcmp(NULL, ...)
1327+ and segfaults. Fall back to a static empty string so downstream
1328+ `strcmp(canonical, ...) == 0` consistently misses (the caller then
1329+ treats this as "not already included" and proceeds with the file
1330+ read, which fails through the normal LoadError path). */
13251331 static char *sp_canonical_path(const char *path) {
1332+ if (!path) { char *e = strdup(""); return e ? e : NULL; }
13261333 #ifdef _WIN32
13271334 char *real = _fullpath(NULL, path, 0);
13281335 #else
13291336 char *real = realpath(path, NULL);
13301337 #endif
1331- return real ? real : strdup(path);
1338+ if (real) return real;
1339+ char *d = strdup(path);
1340+ if (d) return d;
1341+ /* Last resort: return a static empty string to avoid NULL. The
1342+ buffer is read-only by callers (passed to strcmp, then freed by
1343+ the caller's free() -- so allocate a fresh empty string instead
1344+ of returning a string literal). */
1345+ return strdup("");
13321346 }
13331347
13341348 static int sp_path_already_included(const char *canonical) {
1349+ if (!canonical) return 0;
13351350 for (int i = 0; i < sp_included_count; i++) {
1336- if (strcmp(sp_included_paths[i], canonical) == 0) return 1;
1351+ if (sp_included_paths[i] && strcmp(sp_included_paths[i], canonical) == 0) return 1;
13371352 }
13381353 return 0;
13391354 }