an opinion about this code [crash!]

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
R2k
Posts: 16
Joined: Fri Mar 17, 2006 10:49 am
Location: México

an opinion about this code [crash!]

Post by R2k »

well ... i was trying and trying and trying and well ..

Code: Select all

int createConsole(char* origin){
    int result=0;
    char* realpath='\0';
    sprintf&#40;realpath,origin&#41;; <------------------------------Crash Here!
    strcat&#40;realpath,"/img/console_bg.png"&#41;; <----------------and here
    printf&#40;"path&#58; %s", realpath&#41;;
    //printf&#40;"Helloo!!!!!!!!"&#41;;
    result=readPNG&#40;realpath&#41;;
    //printf&#40;"result=%i",result&#41;;
    return&#40;result&#41;;
&#125;
compiles and links OK, but when i run it crash in the sprintf but is the same with strcat , strcpy, snprintf ( i'm using newlib ).

i want to know your opinion about this ....

i know a solution for this !!! but ... it's odd ...
PSP FW v2.6 owner
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

You can't initialize a string like this

char* realpath='\0';

What this does, is just initialize a pointer to a char struct and by setting it to '\0' you make it null pointer. Accessing this will no matter what crash your program.
Use this instead:

char realpath[256];
realpath = '\0';
hubevolution
Posts: 32
Joined: Wed Mar 17, 2004 6:59 pm

Post by hubevolution »

in general ... you need to allocate enough memory for the realpath variable to contain the maximum lenght possible expected for origin variable.

and realpath must be defined like :

Code: Select all

 char realpath&#91;MAX_ORIGIN_LENGTH&#93;;
in this case a path can be 255 max as raphael correctly stated :)
__count
Posts: 22
Joined: Thu Mar 23, 2006 8:40 pm

Post by __count »

Raphael wrote: char realpath[256];
realpath = '\0';
That won't compile.
Either do

*realpath = '\0';

or

realpath[0] = '\0';
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

__count wrote:
Raphael wrote: char realpath[256];
realpath = '\0';
That won't compile.
Either do

*realpath = '\0';

or

realpath[0] = '\0';
Jup, right, forgot that [0] in a hurry :) thanks
R2k
Posts: 16
Joined: Fri Mar 17, 2006 10:49 am
Location: México

Post by R2k »

tnx every1. i solved my prob. hehehehe
PSP FW v2.6 owner
Post Reply