-
Notifications
You must be signed in to change notification settings - Fork 183
Fix a bug that Prism.parse_stream may write too much data to internal buffer
#4160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kou
wants to merge
1
commit into
ruby:main
Choose a base branch
from
kou:fgets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+52
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -365,28 +365,50 @@ bool | |
| pm_source_stream_read(pm_source_t *source) { | ||
| pm_buffer_t *buffer = source->stream.buffer; | ||
|
|
||
| /* | ||
| * We should use rb_enc_mbmaxlen() of stream.external_encoding for | ||
| * the max character length but we don't want to do it here for | ||
| * performance. We can detect the max character length in all | ||
| * available encodings by the following command line in the Ruby | ||
| * source directory: | ||
| * | ||
| * grep -Erh "max (enc|byte) length" enc | | ||
| * grep -Eo '[0-9]+' | | ||
| * sort | | ||
| * tail -n1 # => 6 | ||
| */ | ||
| #define MAX_ENC_LEN 6 | ||
|
|
||
| #define LINE_SIZE 4096 | ||
| char line[LINE_SIZE]; | ||
|
|
||
| while (memset(line, '\n', LINE_SIZE), source->stream.fgets(line, LINE_SIZE, source->stream.stream) != NULL) { | ||
| size_t length = LINE_SIZE; | ||
| while (length > 0 && line[length - 1] == '\n') length--; | ||
| /* | ||
| * io.gets(size) may read size or more bytes when the last | ||
| * character is a multi-byte character. "+ MAX_ENC_LEN" is for the | ||
| * case. | ||
| */ | ||
| char line[LINE_SIZE + MAX_ENC_LEN]; | ||
|
|
||
| /* This must not be '\0' to detect the `\0` written by fgets(). */ | ||
| #define SENTINEL_CHAR 'x' | ||
|
|
||
| while (memset(line, SENTINEL_CHAR, LINE_SIZE + MAX_ENC_LEN), source->stream.fgets(line, LINE_SIZE, source->stream.stream) != NULL) { | ||
| size_t length = LINE_SIZE + MAX_ENC_LEN; | ||
| while (length > 0 && line[length - 1] != '\0') length--; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can keep using |
||
|
|
||
| if (length == LINE_SIZE) { | ||
| /* Remove '\0' written by fgets() */ | ||
| length -= 1; | ||
|
|
||
| /* Append the line to the buffer. */ | ||
| pm_buffer_append_string(buffer, line, length); | ||
|
|
||
| if (line[length - 1] != '\n') { | ||
| /* | ||
| * 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. | ||
| * If it doesn't end with a newline, then we'll 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 | ||
| * reading and return false. In most circumstances, this means we should | ||
|
|
@@ -426,7 +448,9 @@ pm_source_stream_read(pm_source_t *source) { | |
| } | ||
| } | ||
|
|
||
| #undef SENTINEL_CHAR | ||
| #undef LINE_SIZE | ||
| #undef MAX_ENC_LEN | ||
|
|
||
| source->stream.eof = true; | ||
| source->source = (const uint8_t *) pm_buffer_value(buffer); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can keep using
\nfor the sentinel character but I've changed to another character for easy to understand. If you don't like it, I'll revert it.