From 6de0844daca1927260bf41dfd91400b9a37175a2 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Tue, 8 Jul 2014 23:31:37 -0400 Subject: [PATCH] check for wraparound of unsigned int used to keep track of position in source --- tokenize.c | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/tokenize.c b/tokenize.c index 3783540..eece2e5 100644 --- a/tokenize.c +++ b/tokenize.c @@ -49,7 +49,7 @@ static const token_t right_paren = { }; static inline char * -string_head(uint32_t n, +string_head(uint16_t n, char *in, char *out) { /* out must be large enough to store the number of characters @@ -148,15 +148,15 @@ peek_token(token_stream *tokens) { return tokens->tokens[len-1]; } -static inline uint32_t +static inline uint16_t match_int(source_t source, - uint32_t begin, - const uint32_t length) { + uint16_t begin, + const uint16_t length) { /* Return false if there is no match * otherwise return the position of the end of the match + 1 */ - uint32_t i = begin; - uint32_t test; + uint16_t i = begin; + uint16_t test; assert(source != NULL); assert(length > 0); @@ -174,10 +174,10 @@ match_int(source_t source, return i; } -static inline uint32_t +static inline uint16_t match_float(source_t source, - uint32_t begin, - const uint32_t length) { + uint16_t begin, + const uint16_t length) { /* Return false if there is no match * otherwise: * if there is a leading decimal point and then a valid int match: @@ -191,7 +191,7 @@ match_float(source_t source, * return false * ALWAYS returns the position + 1 to avoid confusion with false (which is a valid index) */ - uint32_t i, leading_int_match, trailing_int_match; + uint16_t i, leading_int_match, trailing_int_match; assert(source != NULL); assert(length > 0); @@ -226,10 +226,10 @@ match_float(source_t source, return false; } -static inline uint32_t +static inline uint16_t match_identifier(source_t source, - uint32_t begin, - const uint32_t length) { + uint16_t begin, + const uint16_t length) { /* Return false if there is no match * if there is a match for any characters that are not: @@ -241,7 +241,7 @@ match_identifier(source_t source, * if there is nothing else to match: * return false */ - uint32_t i = begin; + uint16_t i = begin; assert(source != NULL); assert(length > 0); @@ -259,11 +259,11 @@ match_identifier(source_t source, return i; } -static inline uint32_t +static inline uint16_t match_symbol(source_t source, - uint32_t begin, - const uint32_t length) { - uint32_t i; + uint16_t begin, + const uint16_t length) { + uint16_t i; assert(source != NULL); assert(length > 0); @@ -282,8 +282,8 @@ match_symbol(source_t source, } static inline void -extract_token(uint32_t position, - uint32_t begin, +extract_token(uint16_t position, + uint16_t begin, source_t source, char *token_val) { assert(position > begin); @@ -294,15 +294,15 @@ extract_token(uint32_t position, token_stream tokenize(source_t source, - uint32_t begin, - const uint32_t length) { + uint16_t begin, + const uint16_t length) { /* * Remember to free everything from this struct * for example, token_stack.tokens will not necessarily be * equal to tokens after this function has run * */ - uint32_t position = begin; + uint16_t position = begin; char *current_token_val; token_stream token_stack; token_val_t current_token; @@ -425,6 +425,10 @@ tokenize(source_t source, push_token(&token_stack, make_token(current_token, IDENTIFIER)); /* Matched an identifier */ } + else if (position <= begin) { + printf("Source is too large to read\n"); + exit(EXIT_FAILURE); + } else { printf("Unmatched token\n"); exit(EXIT_FAILURE);