Changeset cf74c60a8f0eedfcc9fae71cfa789e16ce382092
- Timestamp:
- 06/23/2008 10:38:48 PM
(2 months ago)
- Author:
- mitchell <mitchell@frost.(none)>
- git-committer:
- mitchell <mitchell@frost.(none)> 1214285928 -0400
- git-parent:
[c97cbe81a1d91cffd70753dae8bbff4898fb4736]
- git-author:
- mitchell <mitchell@frost.(none)> 1214285928 -0400
- Message:
Check for buffer overflow before writing to lb so there's no segfaulting.
For now I'm ceasing callbacks when buffer overflows occur. In the future I'll
probably add a Ruby exception that will be thrown and can be caught to notify
the user of the file that wasn't parsed correctly.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| rb4f9575 |
rcf74c60 |
|
| 23 | 23 | lb->comment[0] = 0; |
|---|
| 24 | 24 | lb->blank_count = 0; |
|---|
| | 25 | lb->buffer_size = buffer_size; |
|---|
| 25 | 26 | } |
|---|
| 26 | 27 | |
|---|
| … | … | |
| 56 | 57 | * copies the passed in string (via delimiters) to the code buffer |
|---|
| 57 | 58 | * |
|---|
| | 59 | * Returns 1 on success, 0 on buffer overflow. Buffer overflows typically occur |
|---|
| | 60 | * for language syntax errors (e.g. unclosed strings or block comments) or |
|---|
| | 61 | * parser errors. |
|---|
| 58 | 62 | */ |
|---|
| 59 | | void language_breakdown_copy_code(LanguageBreakdown *lb, char *from, char *to) { |
|---|
| | 63 | int language_breakdown_copy_code(LanguageBreakdown *lb, char *from, char *to) { |
|---|
| 60 | 64 | from = first_non_blank(from, to); |
|---|
| | 65 | if (lb->code_cur + (to - from) > lb->code + lb->buffer_size) |
|---|
| | 66 | return 0; // overflow error |
|---|
| 61 | 67 | strncpy(lb->code_cur, from, to - from); |
|---|
| 62 | 68 | lb->code_cur += to - from; |
|---|
| 63 | 69 | *lb->code_cur = 0; |
|---|
| | 70 | return 1; |
|---|
| 64 | 71 | } |
|---|
| 65 | 72 | |
|---|
| … | … | |
| 69 | 76 | * copies the passed in string (via delimiters) to the comment buffer |
|---|
| 70 | 77 | * |
|---|
| | 78 | * Returns 1 on success, 0 on buffer overflow. Buffer overflows typically occur |
|---|
| | 79 | * for language syntax errors (e.g. unclosed strings or block comments) or |
|---|
| | 80 | * parser errors. |
|---|
| 71 | 81 | */ |
|---|
| 72 | | void language_breakdown_copy_comment(LanguageBreakdown *lb, char *from, char *to) { |
|---|
| | 82 | int language_breakdown_copy_comment(LanguageBreakdown *lb, char *from, char *to) { |
|---|
| 73 | 83 | from = first_non_blank(from, to); |
|---|
| | 84 | if (lb->comment_cur + (to - from) > lb->comment + lb->buffer_size) |
|---|
| | 85 | return 0; // overflow error |
|---|
| 74 | 86 | strncpy(lb->comment_cur, from, to - from); |
|---|
| 75 | 87 | lb->comment_cur += to - from; |
|---|
| 76 | 88 | *lb->comment_cur = 0; |
|---|
| | 89 | return 1; |
|---|
| 77 | 90 | } |
|---|
| rb4f9575 |
rcf74c60 |
|
| 15 | 15 | char *comment_cur; |
|---|
| 16 | 16 | int blank_count; |
|---|
| | 17 | int buffer_size; |
|---|
| 17 | 18 | } LanguageBreakdown; |
|---|
| 18 | 19 | |
|---|
| 19 | 20 | void language_breakdown_initialize(LanguageBreakdown *lb, char *name, int buffer_size); |
|---|
| 20 | | void language_breakdown_copy_code(LanguageBreakdown *lb, char *from, char *to); |
|---|
| 21 | | void language_breakdown_copy_comment(LanguageBreakdown *lb, char *from, char *to); |
|---|
| | 21 | int language_breakdown_copy_code(LanguageBreakdown *lb, char *from, char *to); |
|---|
| | 22 | int language_breakdown_copy_comment(LanguageBreakdown *lb, char *from, char *to); |
|---|
| 22 | 23 | |
|---|
| 23 | 24 | void language_breakdown_free(LanguageBreakdown *lb); |
|---|
| r83e15c9 |
rcf74c60 |
|
| 196 | 196 | * @param e The end position of the entity relative to the start of the buffer |
|---|
| 197 | 197 | * (non-inclusive). |
|---|
| | 198 | * TODO: instead of ignoring syntax errors that cause buffer overflows, consider |
|---|
| | 199 | * raising Ruby exceptions to catch and notify the user of. |
|---|
| 198 | 200 | */ |
|---|
| 199 | 201 | void ragel_parser_callback(const char *lang, const char *entity, int s, int e) { |
|---|
| 200 | 202 | LanguageBreakdown *lb = get_language_breakdown((char *) lang); |
|---|
| 201 | 203 | if (strcmp(entity, "lcode") == 0) { |
|---|
| 202 | | language_breakdown_copy_code(lb, parse_buffer + s, parse_buffer + e); |
|---|
| 203 | | ragel_parse_yield_line(lang, entity, s, e); |
|---|
| | 204 | if (language_breakdown_copy_code(lb, parse_buffer + s, parse_buffer + e)) |
|---|
| | 205 | ragel_parse_yield_line(lang, entity, s, e); |
|---|
| 204 | 206 | } else if (strcmp(entity, "lcomment") == 0) { |
|---|
| 205 | | language_breakdown_copy_comment(lb, parse_buffer + s, parse_buffer + e); |
|---|
| 206 | | ragel_parse_yield_line(lang, entity, s, e); |
|---|
| | 207 | if (language_breakdown_copy_comment(lb, parse_buffer + s, parse_buffer + e)) |
|---|
| | 208 | ragel_parse_yield_line(lang, entity, s, e); |
|---|
| 207 | 209 | } else if (strcmp(entity, "lblank") == 0) { |
|---|
| 208 | 210 | lb->blank_count++; |
|---|