Thread: Checking for oversights

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Checking for oversights

    As some of you already know I'm making my own compiler, I just wrote a function for adding defines to a dedicated temporary file and just need to know if I made any oversights:
    Code:
    int next_word(
    	file_pos_t *file_pos,
    	register _Bool stop_at_newline );
    int add_define( lint_t *lint, file_pos_t *file_pos ) {
    	int ret = next_word( file_pos, 1 );
    	FILE *out = lint->files[CODE_DEFTAB];
    	char *text = &(file_pos->text[file_pos->pos]);
    	int pos, len;
    	long len_pos;
    	(void)fseek( out, 0, SEEK_END );
    	if ( ret != 0 ) return ret;
    	/* NAME */
    	if ( fwrite( &file_pos->len, sizeof(int), 1, out ) < sizeof(int) )
    		return ENOMEM;
    	if ( fwrite( text, 1, file_pos->len, out ) < file_pos->len )
    		return ENOMEM;
    	if ( text[len-1] == '(' ) {
    		len = file_pos->len;
    		len_pos = ftell( out );
    		while ( text[len-1] != ')' ) {
    			ret = next_word( file_pos, 0 );
    			if ( ret != 0 ) return ret;
    			len += file_pos->len;
    			if ( fwrite( text, 1, file_pos->len, out ) < file_pos->len )
    				return ENOMEM;
    		}
    		(void)fseek( out, len_pos, SEEK_SET );
    		(void)fwrite( &len, sizeof(int), 1, out );
    		(void)fseek( out, 0, SEEK_END );
    	}
    	ret = next_word( file_pos, 1 );
    	len = 0;
    	pos = file_pos->pos;
    	text = &file_pos->text[pos];
    	if ( ret == '\n' )
    		goto add_define_write_len;
    	while ( 1 ) {
    		len = file_pos->pos - pos;
    		len += file_pos->len;
    		if ( text[len-1] == '\\' ) {
    			ret = next_word( file_pos, 1 );
    			continue;
    		}
    		if ( ret == '\n' )
    			break;
    	}
    	add_define_write_len:
    	if ( fwrite( &len, sizeof(int), 1, out ) < sizeof(int) )
    		return ENOMEM;
    	if ( len > 0 ) {
    		if ( fwrite( text, 1, len, out ) < len )
    			return ENOMEM;
    	}
    	return 0;
    }
    No rush for responses as I'm taking a rather lax attitude to the project in the first place

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You haven't checked to see if "out" is not NULL before using it.

    Instead of using the goto, why not just...
    Code:
        while ( ret != '\n' ) {
            len = file_pos->pos - pos;
            len += file_pos->len;
            if ( text[len-1] == '\\' ) {
                ret = next_word( file_pos, 1 );
            }        
        }
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Click_here View Post
    You haven't checked to see if "out" is not NULL before using it.
    That's because it's already been checked by the function that creates it and skips the call to the function that calls this function if it fails, in future when I start porting this to my main project (since this is the experimental project) I will probably add the check as "one in a trillion chance" but for now it is uneeded.
    Quote Originally Posted by Click_here View Post
    Instead of using the goto, why not just...
    Code:
        while ( ret != '\n' ) {
            len = file_pos->pos - pos;
            len += file_pos->len;
            if ( text[len-1] == '\\' ) {
                ret = next_word( file_pos, 1 );
            }        
        }
    I'm certainly considering attempting that way but due to how I wrote the next_word() function I have a feeling I may miss something that is supposed to be part of the define, I want to check all my logic on that front before I switch to that

    Anyway the kind of oversight I meant was in the information capture part for each define, I normally use simple defines but for all I know there may be more complex defines that this won't be sufficient for, it's that sort of oversight I was looking for, to be honest it would be nice to have a list of the different kinds of defines that can occur so that I can put it in a test_define.c or something to push the limits of the function and check the output placed in the temporary file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking For 1.#INF and 1.#IND
    By Gokhan in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2007, 02:48 PM
  2. Checking an App
    By Wanted420 in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2003, 03:48 AM
  3. checking for win in tic tac toe
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2002, 01:31 PM
  4. just checking again...
    By matheo917 in forum C++ Programming
    Replies: 0
    Last Post: 01-22-2002, 08:00 PM
  5. just checking....
    By matheo917 in forum C++ Programming
    Replies: 0
    Last Post: 01-22-2002, 07:43 PM

Tags for this Thread