Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am writing a c code and I made a struct as shown below and also 'typedef'ed it to have easy access.

C++
struct files
{
    char* Name;
    int size;
    time_t time;
    int Fileblock[32];
};

typedef struct files file;


I have allocated space for commands in the following way:

#define BUFFERSIZE 64
int buffsize= BUFFERSIZE;
int i=0;
char **commands = malloc(sizeof(char)* buffsize);


Then I set values in my launch function as under
C++
int launch(char *line)
{
    commands[i] = strtok(line," ");
    printf("%s\n", commands[0]);
    i++;
    while ((commands[i] = strtok(NULL, " ")) != NULL)
    {
        printf("%s\n", commands[i]);
        i++;
    }
    file fnow;
    
    fnow.Name = commands[1]; 
    //printf("Getting status for -%s-\n", commands[1] );
    status = stat(commands[1], &buf);
    //printf("the status given by stat on putting test.txt is %d\n", status);
    fnow.size = buf.st_size;
}


And then I pass the fnow object to the function.

C++
goput(fnow);


now, when I print the Name of the file, it prints correctly, however, when I try to take the size of the file it gives me zero.

It automatically appends the Name of the file with some random character like a '?' or '-' if I hardcode a value like below, it works fine.
C++
fnow.Name=commands[1]; 
//printf("Getting status for -%s-\n", commands[1] );
status = stat("foo.txt", &buf);
//printf("the status given by stat on putting test.txt is %d\n",status);
fnow.size = buf.st_size;


How do I avoid that?

What I have tried:

So far, I am pretty much lost. I need ideas on how can I avoid this kind of thing, because I have to work using a dynamic input which is given by the user.
Posted
Updated 5-May-16 19:56pm
v4
Comments
Patrice T 6-May-16 1:30am    
Please complete the code so that we can see the logic of your code.
Use Improve question to update your question.
Shivangi_K 6-May-16 1:37am    
well that's pretty much it. if you go to the line fnow.size=buf.st_size, it gives me a size of zero. However, when I hardcode the file name it gives me the size of the file.
Patrice T 6-May-16 1:52am    
launch uses variables command and i that come from nowhere.
Complete the code so that it is possible to compile.
Shivangi_K 6-May-16 1:58am    
yes I have included that information now.

1 solution

You are probably experiencing a case of buffer overrun, but it is hard to tell because you have left out certain information.

For example

* How is the variable commands declared?
It is probably an array of char*, but you need to show that?

Also with this statement, you are not assigning the value of commands[1], but the address.
C++
fnow.Name = commands[1];


Try either to allocate memory for Name
C++
fnow.Name = (char*)calloc(strlen(commands[1]) + 1, sizeof(char));
strcpy(fnow.Name, commands[1]);

Don't forget to use free(fnow.Name) when you are done.

You can also change from
C++
char* Name to char Name[25] // or whatever a proper length would be

This is a very simple test to see if you suffer from buffer overrun.
 
Share this answer
 
Comments
Shivangi_K 6-May-16 1:57am    
I updated the question with the desired information. I also tried allocating the space, it is still not working.
Shivangi_K 6-May-16 2:01am    
I increased my buffer from 64 to 500 and it worked. Does it mean I was not allocating enough space for all of them variables in the struct?
George Jonsson 6-May-16 2:21am    
Well with this statement char** commands = malloc(sizeof(char)* buffsize); you allocate space for 64 char's, but I assume you want allocate space for 64 char*.
The difference is that the size of a char is 1 byte and the size of a char* is 4 bytes.
So 64 * sizeof(char) = 64 and 64 * sizeof(char*) = 256
Also better to use calloc(), as the memory will be set to zero form start.
So try
char** commands = (char**)calloc(buffsize, sizeof(char*));
Shivangi_K 6-May-16 2:32am    
that makes sense, thanks :)
George Jonsson 6-May-16 2:37am    
You are welcome.
Don't forget to call free(commands) to release the memory when you are done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900