Page 1 of 1

CARD_Init() crash

Posted: Tue Jul 12, 2011 4:01 am
by figgles
CARD_Init() takes two parameters: the game code and the company name. The documentation reads:

Code: Select all

Performs the initialization of the memory card subsystem.

Parameters:
gamecode 	pointer to a 4byte long string to specify the vendors game code. May be NULL
company 	pointer to a 2byte long string to specify the vendors company code. May be NULL
However, doing:

Code: Select all

char gamecode[4] = { 'a' , 'b', 'c', 'd' };
char company[2] = { '1', '2' };
CARD_Init(gamecode, company);
...triggers a crash. The problem is in card.c lines 2449 - 2450:

Code: Select all

if(gamecode && strlen(gamecode)<=4) memcpy(card_gamecode,gamecode,4);
if(company && strlen(company)<=2) memcpy(card_company,company,2);
Specifically, the problem is the use of strlen(). It appears then that CARD_Init() expects NULL terminated strings, but specifies explicitly the length in bytes of the string in the documentation. The use of the word 'bytes' as opposed to 'characters' and the lack of mention of a required NULL termination is a problem. Either the documentation or the implementation needs to change to match the other. I suggest simply rewording the documentation to read:

Code: Select all

gamecode 	pointer to a 4 character NULL-terminated string to specify the vendors game code. May be NULL.
company 	pointer to a 2 character NULL-terminated string to specify the vendors company code. May be NULL.

Re: CARD_Init() crash

Posted: Mon Jul 25, 2011 4:05 pm
by tueidj
Given that this is documentation for C code, I would have assumed "string" implied null termination.

Re: CARD_Init() crash

Posted: Mon Jul 25, 2011 4:11 pm
by WinterMute
To be fair I think we can probably dispense with the strlen - these functions basically use 4 and 2 byte arrays rather than actual strings.