Commit 5667789a

5667789a2cbeMerged

Test User <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

Fix buffer overflow and incorrect .rb extension check in spinel_parse.c

Two issues in require resolution:

  1. Buffer overflow: strcat(full_path, ".rb") could overflow the 1024-byte buffer when the path was close to the limit. Add a length check before appending.

  2. Incorrect extension check: strstr(path, ".rb") matches ".rb" anywhere in the path (e.g. "lib/rb_helper/file"). Changed to check if the path actually ends with ".rb".

Same fix applied to both resolve_requires and resolve_plain_requires.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
5667789a2cbe
Parent
b2ae55818990

spinel_parse.cmodifiedView file

@@ -857,8 +857,11 @@
857857 /* Build full path */
858858 char full_path[1024];
859859 snprintf(full_path, sizeof(full_path), "%s/%s", dir, rel_path);
860- if (!strstr(full_path, ".rb"))
861- strcat(full_path, ".rb");
860+ {
861+ size_t fl = strlen(full_path);
862+ if (fl < sizeof(full_path) - 4 && (fl < 3 || strcmp(full_path + fl - 3, ".rb") != 0))
863+ strcat(full_path, ".rb");
864+ }
862865
863866 char *canonical = sp_canonical_path(full_path);
864867 char *content;
@@ -936,7 +939,11 @@
936939 snprintf(lib_name, sizeof(lib_name), "%.*s", (int)(end - start), start);
937940 char lib_path[1024];
938941 snprintf(lib_path, sizeof(lib_path), "%s/%s", lib_dir, lib_name);
939- if (!strstr(lib_path, ".rb")) strcat(lib_path, ".rb");
942+ {
943+ size_t fl = strlen(lib_path);
944+ if (fl < sizeof(lib_path) - 4 && (fl < 3 || strcmp(lib_path + fl - 3, ".rb") != 0))
945+ strcat(lib_path, ".rb");
946+ }
940947
941948 /* Same dedup as resolve_requires: a file pulled in via plain `require`
942949 must not be re-inlined if a previous `require` or `require_relative`