Apache DLL file - unexpected error - confused newbie

Discussion in 'C++' started by Greg M, May 24, 2004.

  1. Greg M

    Greg M Guest

    Hello, I am using Visual C++ Studio 6.0 to compile a module i have written
    for apache under windows xp.
    The code compiles perfectly, however when used by the dll, the code fails.
    This causes my webdav client to fail..

    Please could you help me by telling me what im doing wrong.

    Greg Miell

    FILE: fullrw.c

    /*CUT*/

    long dirs_size(const char *curdir)
    {
    struct _finddata_t c_file;
    long hFile;
    long totalsize=0;
    char *curdirr;
    char *filedir;
    curdirr= (char*) curdir;
    strcpy(curdirr, "\\*.*");

    if( (hFile = _findfirst( curdirr, &c_file )) == -1L )
    {}
    else
    {

    if (c_file.attrib & _A_SUBDIR){

    if ((strcmp(c_file.name,"."))==0) {
    } else {
    if ((strcmp(c_file.name,".."))==0) {
    } else {
    filedir = (char*) curdir;
    strcpy(filedir,"\\");
    strcpy(filedir,c_file.name);
    totalsize += dirs_size(filedir);
    //printf("Dir %d\n",totalsize);
    }
    }
    }else{
    totalsize += c_file.size;
    //printf("Files %d\n",c_file.size);
    };

    while( _findnext( hFile, &c_file ) == 0 )
    {

    if (c_file.attrib & _A_SUBDIR){


    if ((strcmp( c_file.name, "."))==0) {
    } else {
    if ((strcmp(c_file.name,".."))==0) {
    } else {

    if ((strcmp(c_file.name,".DAV"))==0) {
    //printf("DAV");
    } else {
    filedir = (char*) curdir;
    strcpy(filedir,"\\");
    strcpy(filedir,c_file.name);
    totalsize += dirs_size((const char*) filedir);
    return 0;
    }
    //printf("Dir %d\n",totalsize);
    }
    }
    }else{
    totalsize += c_file.size;
    //printf("Files %d\n",c_file.size);
    };

    }
    _findclose( hFile );
    }
    return totalsize;
    }

    /*EOF*/
     
    Greg M, May 24, 2004
    #1
    1. Advertising

  2. "Greg M" <> wrote in message
    news:40b1b484$0$20508$...
    > Hello, I am using Visual C++ Studio 6.0 to compile a module i have written
    > for apache under windows xp.
    > The code compiles perfectly, however when used by the dll, the code fails.
    > This causes my webdav client to fail..
    >
    > Please could you help me by telling me what im doing wrong.


    Not using pointer correctly, see below.

    >
    > Greg Miell
    >
    > FILE: fullrw.c
    >
    > /*CUT*/
    >
    > long dirs_size(const char *curdir)
    > {
    > struct _finddata_t c_file;
    > long hFile;
    > long totalsize=0;
    > char *curdirr;
    > char *filedir;
    > curdirr= (char*) curdir;
    > strcpy(curdirr, "\\*.*");


    curdirr is an initialised pointer, yet you are copying characters to
    wherever it points to, this will crash your program.

    >
    > if( (hFile = _findfirst( curdirr, &c_file )) == -1L )


    It there any reason you can't just say

    if( (hFile = _findfirst( "\\*.*", &c_file )) == -1L )

    Seems a lot easier.

    > {}
    > else
    > {
    >
    > if (c_file.attrib & _A_SUBDIR){
    >
    > if ((strcmp(c_file.name,"."))==0) {
    > } else {
    > if ((strcmp(c_file.name,".."))==0) {
    > } else {
    > filedir = (char*) curdir;
    > strcpy(filedir,"\\");
    > strcpy(filedir,c_file.name);


    Again filedir is an uninitialised pointer, or rather curdir is, but you've
    make filedir and curdir point to the same garbage location. It's not enough
    to declare a pointer you must also make it point somewhere.

    Time for some revision on pointer usage I think. Or better still use C++
    strings, You can use C++ strings pretty much like you are trying to use
    pointers.

    john
     
    John Harrison, May 24, 2004
    #2
    1. Advertising

  3. >
    > curdirr is an initialised pointer, yet you are copying characters to
    > wherever it points to, this will crash your program.
    >


    Apologies, you've confused me with your variables called currdir and curdir,
    if nothing else you should name your variables better.

    You are also violating const correctness, which might be the cause of your
    problem. Try getting some code to compile without using any char* casts.

    You are also failing to check that you have enough room in curdir when you
    copy characters to it, that's more likely to be the cause of your problems.

    Best solution is to use C++ strings. You avoid all this pointer nonsense.

    john
     
    John Harrison, May 24, 2004
    #3
  4. [snips]

    On Mon, 24 May 2004 09:38:26 +0100, Greg M wrote:

    > long dirs_size(const char *curdir)
    > {
    > struct _finddata_t c_file;
    > long hFile;
    > long totalsize=0;
    > char *curdirr;
    > char *filedir;
    > curdirr= (char*) curdir;
    > strcpy(curdirr, "\\*.*");


    No. No, no, no, no, no.

    curdirr is now just an alias for curdir, which is explicitly defined by
    the function header as being *non modifiable*. Which means calling code
    can safely rely on the function *not* to attempt to do things like write
    to it. Except you promptly do exactly that, in direct violation of the
    promise made by the function header.

    Either make the parameter modifiable and document exactly what the caller
    needs to do (allocate 256 bytes, for example), or leave it as a const and
    don't try to write to it.

    Note that you have an explicit cast in there = (char *). As a general
    rule of thumb, casts indicate you're doing something wrong. They do have
    their uses, but if you find yourself needing to use one, you also need to
    *really* be sure that using it is the Right Thing.
     
    Kelsey Bjarnason, Jun 7, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anushi
    Replies:
    5
    Views:
    9,002
  2. Greg M
    Replies:
    0
    Views:
    310
    Greg M
    May 24, 2004
  3. Vaquar
    Replies:
    1
    Views:
    237
    Vaquar
    Mar 20, 2007
  4. H. Simpson
    Replies:
    4
    Views:
    343
    H. Simpson
    Aug 3, 2004
  5. Selmar
    Replies:
    2
    Views:
    199
    krakle
    Jul 21, 2004
Loading...

Share This Page