Apache DLL file - unexpected error - confused newbie

G

Greg M

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*/
 
J

John Harrison

Greg M said:
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
 
J

John Harrison

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
 
K

Kelsey Bjarnason

[snips]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top