-
Notifications
You must be signed in to change notification settings - Fork 359
Enforce nesting limit in scanner #329
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8566d43
Enforce nesting limit in scanner
ingydotnet bb1df20
Add block nesting limit in scanner
ingydotnet e1ed542
Check combined nesting depth limit
ingydotnet b9106ba
Update gitignore for new artifacts
ingydotnet 3658f34
Move nesting limit to scanner only
ingydotnet 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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| #include <yaml.h> | ||
|
|
||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #ifdef NDEBUG | ||
| #undef NDEBUG | ||
| #endif | ||
| #include <assert.h> | ||
|
|
||
| /* | ||
| * Test that the scanner enforces the maximum nesting depth. | ||
| */ | ||
|
|
||
| static int | ||
| scan_string(const char *input, size_t length) | ||
| { | ||
| yaml_parser_t parser; | ||
| yaml_token_t token; | ||
| int done = 0; | ||
| int error = 0; | ||
|
|
||
| assert(yaml_parser_initialize(&parser)); | ||
| yaml_parser_set_input_string(&parser, | ||
| (const unsigned char *)input, length); | ||
|
|
||
| while (!done) { | ||
| if (!yaml_parser_scan(&parser, &token)) { | ||
| error = 1; | ||
| break; | ||
| } | ||
| done = (token.type == YAML_STREAM_END_TOKEN); | ||
| yaml_token_delete(&token); | ||
| } | ||
|
|
||
| yaml_parser_delete(&parser); | ||
| return error; | ||
| } | ||
|
|
||
| int | ||
| main(void) | ||
| { | ||
| char *input; | ||
| int i; | ||
|
|
||
| /* Test 1: nesting beyond the default limit (1000) must fail. */ | ||
| { | ||
| int depth = 2000; | ||
| input = (char *)malloc(depth + 1); | ||
| assert(input); | ||
| memset(input, '[', depth); | ||
| input[depth] = '\0'; | ||
|
|
||
| printf("Test 1: %d nested '[' (exceeds limit) ... ", depth); | ||
| fflush(stdout); | ||
| assert(scan_string(input, depth) == 1); | ||
| printf("OK (rejected)\n"); | ||
| free(input); | ||
| } | ||
|
|
||
| /* Test 2: block nesting beyond the limit must fail. */ | ||
| { | ||
| /* | ||
| * Build a YAML string with 2000 levels of block mapping nesting: | ||
| * "a:\n b:\n c:\n d:\n..." — each level indented one more space. | ||
| */ | ||
| int depth = 2000; | ||
| int len = 0; | ||
| int pos = 0; | ||
|
|
||
| /* Each level: indent spaces + "X:\n" */ | ||
| for (i = 0; i < depth; i++) | ||
| len += i + 2 + 1; /* i spaces + "X:" + newline */ | ||
| input = (char *)malloc(len + 1); | ||
| assert(input); | ||
|
|
||
| for (i = 0; i < depth; i++) { | ||
| int j; | ||
| for (j = 0; j < i; j++) | ||
| input[pos++] = ' '; | ||
| input[pos++] = 'a' + (i % 26); | ||
| input[pos++] = ':'; | ||
| input[pos++] = '\n'; | ||
| } | ||
| input[pos] = '\0'; | ||
|
|
||
| printf("Test 2: %d levels block nesting (exceeds limit) ... ", depth); | ||
| fflush(stdout); | ||
| assert(scan_string(input, pos) == 1); | ||
| printf("OK (rejected)\n"); | ||
| free(input); | ||
| } | ||
|
|
||
| /* Test 3: flow nesting within the limit must succeed. */ | ||
| { | ||
| int depth = 500; | ||
| int len = depth * 2; | ||
| input = (char *)malloc(len + 1); | ||
| assert(input); | ||
| for (i = 0; i < depth; i++) | ||
| input[i] = '['; | ||
| for (i = 0; i < depth; i++) | ||
| input[depth + i] = ']'; | ||
| input[len] = '\0'; | ||
|
|
||
| printf("Test 2: %d nested '[]' (within limit) ... ", depth); | ||
| fflush(stdout); | ||
| assert(scan_string(input, len) == 0); | ||
| printf("OK (accepted)\n"); | ||
| free(input); | ||
| } | ||
|
|
||
| return 0; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.