Commit 753ee873
753ee8739c05Merged
Sam Ruby <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
File.expand_path: record the normalized length, not the reserved capacity
sp_file_expand_path sized the result string with sp_str_alloc(rawlen + 1) — a safe upper bound on capacity — but path normalization (collapsing ./ .. //) can produce fewer bytes, and the string's recorded length was left at rawlen + 1. The result therefore carried trailing NUL bytes past the real path: File.expand_path("x") returned a string whose byte length included a stray "\0".
Set the length to the actual written byte count (olen) before returning.
before: File.expand_path("x").length # => len+1, bytes end with "\0" after: File.expand_path("x").length # => len
Details
- Commit
- 753ee8739c05
- Parent
- 951d8bb8a048
lib/sp_runtime.hmodifiedView file
@@ -5440,6 +5440,12 @@
54405440
p = q;54415441
}54425442
out[olen] = 0;5443+
/* The result was sized to the pre-normalized rawlen+1 (a safe capacity5444+
bound), but normalization can shrink it, so the actual content is olen5445+
bytes. Record olen as the length; otherwise the string carries trailing5446+
NUL/garbage bytes past the path (e.g. File.expand_path("x") returns a5447+
string whose byte length includes a stray "\0"). */5448+
sp_str_set_len(out, olen);54435449
return out;54445450
}54455451