summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2024-07-17 15:30:03 -0400
committergit <[email protected]>2024-07-17 19:44:32 +0000
commite77e4aa608a12ea59cefc87abafd72fb2b0c0b9a (patch)
tree96ed90556004047807a8899a8e3c4e146eb2e5e8
parent0fe816f3808cdf647ac549a8ddb2e0540320b890 (diff)
[ruby/prism] Have parse_stream handle NUL bytes
https://github.com/ruby/prism/commit/4a41d298c8
-rw-r--r--prism/extension.c4
-rw-r--r--prism/prism.c9
-rw-r--r--test/prism/api/parse_stream_test.rb8
3 files changed, 16 insertions, 5 deletions
diff --git a/prism/extension.c b/prism/extension.c
index d0bc5e7f0c..64affd4001 100644
--- a/prism/extension.c
+++ b/prism/extension.c
@@ -856,8 +856,8 @@ parse_stream_fgets(char *string, int size, void *stream) {
return NULL;
}
- const char *cstr = StringValueCStr(line);
- size_t length = strlen(cstr);
+ const char *cstr = RSTRING_PTR(line);
+ long length = RSTRING_LEN(line);
memcpy(string, cstr, length);
string[length] = '\0';
diff --git a/prism/prism.c b/prism/prism.c
index 427ac49e3d..2d6cccffba 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -21696,18 +21696,21 @@ pm_parse_stream_read(pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t
#define LINE_SIZE 4096
char line[LINE_SIZE];
- while (fgets(line, LINE_SIZE, stream) != NULL) {
- size_t length = strlen(line);
+ while (memset(line, '\n', LINE_SIZE), fgets(line, LINE_SIZE, stream) != NULL) {
+ size_t length = LINE_SIZE;
+ while (length > 0 && line[length - 1] == '\n') length--;
- if (length == LINE_SIZE && line[length - 1] != '\n') {
+ if (length == LINE_SIZE) {
// If we read a line that is the maximum size and it doesn't end
// with a newline, then we'll just append it to the buffer and
// continue reading.
+ length--;
pm_buffer_append_string(buffer, line, length);
continue;
}
// Append the line to the buffer.
+ length--;
pm_buffer_append_string(buffer, line, length);
// Check if the line matches the __END__ marker. If it does, then stop
diff --git a/test/prism/api/parse_stream_test.rb b/test/prism/api/parse_stream_test.rb
index 0edee74cc2..1c068c617c 100644
--- a/test/prism/api/parse_stream_test.rb
+++ b/test/prism/api/parse_stream_test.rb
@@ -69,5 +69,13 @@ module Prism
assert result.success?
assert_equal 4, result.value.statements.body.length
end
+
+ def test_nul_bytes
+ io = StringIO.new("1 # \0\0\0 \n2 # \0\0\0\n3")
+ result = Prism.parse_stream(io)
+
+ assert result.success?
+ assert_equal 3, result.value.statements.body.length
+ end
end
end