Thread: Runtime error

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    10

    Runtime error

    There is an error of a runtime but I can't figure why it is. I assumed at first it was a syntax error but that is not the case. Well I just need to know what it is suppose to do:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <sys/time.h>
    
    typedef int int32;
    typedef char int8;
    
    void xorStringRounds(int8  *output, int8  *pbDataOne, const int8  *pbDataTwo, int32 length)
    {
    	int32 i = 0;
    
    	for(i=0; i<length; i++)
    	{
    		output[i] = (int8)(pbDataOne[i] ^ pbDataTwo[i]);
    	}
    
    	return;
    }
    
    void itoa( int32 num, int8  *alpha, int32 radix )
    {
    	if( radix == 10 )
    	{
    		sprintf(alpha, "%i", num);
    	}
    	else if( radix == 16 )
    	{
    		sprintf(alpha, "%X", num);
    	}
    }
    
    int8 *oldModString(int32 modifier, const int8  *pbDataOne, int32 length)
    {
    	int8  leading[3];
    	int32 ileading;
    	int8 * tempStr = NULL;
    	int8 * ret;
    	int32 i = 0;
    
    	itoa(modifier/2, leading, 10);
    	ileading = atoi(leading);
    	tempStr = (int8 *) malloc(8);
    	ret = (int8 *) malloc(length);
    	memset(tempStr, 0, 8);
    	tempStr[0] = 0;
    
    	if( (modifier+1)%2 == 0 ) {
    		tempStr[0] = (int8)((ileading<<4) + 8);
    	}
    	else {
    		tempStr[0] = (int8)(ileading<<4);
    	}
    
    	for(i=0; i<(length>>3); i++)
    	{
    		xorStringRounds(ret+i*8, tempStr, pbDataOne+i*8, 8);
    	}
    	free(tempStr);
    	return ret;
    }
    
    int main(int argc, char **argv) {
    	int8 data[32];
    
    	memset(data, 0x0A, sizeof(data));
    	int8 *resp = oldModString(0xFF, data, sizeof(data));
    
    	free(resp);
    
    	return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help if you told us WHAT runtime error you get..

    However, I'm suspicious that this is wrong:
    Code:
    	int8  leading[3];
    ...
    	itoa(modifier/2, leading, 10);
    	ileading = atoi(leading);
    modifier is 255, (0xff), which makes 127 after dividing, which is 3 digits. Your string leading can only hold 2 digits and the terminating zero. So what happens when you stuff three characters + terminating zero in there? Who knows, because it's undefined behaviour. Hopefully your computer hasn't got nuclear warheads attached

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    So Learner87 did you get the exact errors and its issues in the code. If yes, please post it so that it could help others too...

    korn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM