Changeset cf74c60a8f0eedfcc9fae71cfa789e16ce382092

Show
Ignore:
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
  • ext/ohcount_native/language_breakdown.c

    rb4f9575 rcf74c60  
    2323  lb->comment[0] = 0; 
    2424  lb->blank_count = 0; 
     25  lb->buffer_size = buffer_size; 
    2526} 
    2627 
     
    5657 * copies the passed in string (via delimiters) to the code buffer 
    5758 * 
     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. 
    5862 */ 
    59 void language_breakdown_copy_code(LanguageBreakdown *lb, char *from, char *to) { 
     63int language_breakdown_copy_code(LanguageBreakdown *lb, char *from, char *to) { 
    6064  from = first_non_blank(from, to); 
     65  if (lb->code_cur + (to - from) > lb->code + lb->buffer_size) 
     66    return 0; // overflow error 
    6167  strncpy(lb->code_cur, from, to - from); 
    6268  lb->code_cur += to - from; 
    6369  *lb->code_cur = 0; 
     70  return 1; 
    6471} 
    6572 
     
    6976 * copies the passed in string (via delimiters) to the comment buffer 
    7077 * 
     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. 
    7181 */ 
    72 void language_breakdown_copy_comment(LanguageBreakdown *lb, char *from, char *to) { 
     82int language_breakdown_copy_comment(LanguageBreakdown *lb, char *from, char *to) { 
    7383  from = first_non_blank(from, to); 
     84  if (lb->comment_cur + (to - from) > lb->comment + lb->buffer_size) 
     85    return 0; // overflow error 
    7486  strncpy(lb->comment_cur, from, to - from); 
    7587  lb->comment_cur += to - from; 
    7688  *lb->comment_cur = 0; 
     89  return 1; 
    7790} 
  • ext/ohcount_native/language_breakdown.h

    rb4f9575 rcf74c60  
    1515  char *comment_cur; 
    1616  int blank_count; 
     17  int buffer_size; 
    1718} LanguageBreakdown; 
    1819 
    1920void 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); 
     21int language_breakdown_copy_code(LanguageBreakdown *lb, char *from, char *to); 
     22int language_breakdown_copy_comment(LanguageBreakdown *lb, char *from, char *to); 
    2223 
    2324void language_breakdown_free(LanguageBreakdown *lb); 
  • ext/ohcount_native/ragel_parser.c

    r83e15c9 rcf74c60  
    196196 * @param e The end position of the entity relative to the start of the buffer 
    197197 *   (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. 
    198200 */ 
    199201void ragel_parser_callback(const char *lang, const char *entity, int s, int e) { 
    200202  LanguageBreakdown *lb = get_language_breakdown((char *) lang); 
    201203  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); 
    204206  } 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); 
    207209  } else if (strcmp(entity, "lblank") == 0) { 
    208210    lb->blank_count++;