views:

186

answers:

3

I have a shared structure, and inside it a request structure:

struct shared_data {
    pthread_mutex_t th_mutex_queue;

    struct request_queue {
        int min;
        int max;
        char d_name[DIR_SIZE];
        pid_t pid;
        int t_index;
    } request_queue[BUFSIZE];

    int count;

    int data_buffer_allocation[BUFSIZE];
    int data_buffers[BUFSIZE][100];
};

Then I prepare a request;

struct shared_data *sdata_ptr;
...
...
sdata_ptr->request_queue[index].pid = pid;
strcpy(sdata_ptr->request_queue[index].d_name, dir_path_name);
sdata_ptr->request_queue[index].min = min;
sdata_ptr->request_queue[index].max = max;

And the compiler warns me that I'm doing an incompatible implicit declaration in the strcpy function. I guess that's a problem with the pointers, but isn't what I wrote above supposed to be true?

+2  A: 

It doesn't appear to be a problem with the nested struct, at least without seeing more code (i.e. what is dir_path_name).

This might be a long shot, but did you include string.h for the compiler to see the declaration of strcpy?

Eli Bendersky
wow, I really didn't.
Halo
+2  A: 

"implicit declaration" warnings usually mean you didn't include the proper header, which, for strcpy, is string.h.

Instead of strcpy, you should use strlcpy, which lets you limit the number of characters copied, preventing buffer overflow.

outis
+1  A: 

Halo, you might be missing to include string.h as some of the other posters have said. Also, the string copying you are doing on that field will cause a buffer overflow if dir_path_name is larger than d_name.

I'd go out of a limb and suggest you do the following:

  1. explicitly check if the size of the source buffer is less than or equals than the destination (and bug the hell out if it is not),
  2. use memcpy instead of of the str*cpy
  3. you memset the remainder of sdata_ptr->request_queue[index].d_name to 0 (play safe)

Using your code sample, it would go as follows:

/* dir_path_name should be null-terminated for this to work */
size_t path_len = strlen(dir_path_name);
char * dest = sdata_ptr->request_queue[index].d_name;
size_t n = strlen(dest);

if( path_len >= n )
{
  ... err out, no workie
}

memcpy(dest, dir_path_name, n );

if( path_len < n )
{
  memset((dest+n),0,n-path_len);
}

Hope it helps.

luis.espinal
thanks, I can assume it won't be longer than 250 characters.
Halo