Jump to content

Need some help with bug-fixing Spior's downloader bot


Recommended Posts

Hey guys, 

 

Let me just start off by saying that I'm a far from experienced coder. One of the ways I'm trying to learn practical C++ is by looking at @@spior's JK3files download bot and trying to understand the code. The more I look at it and experiment with stuff the more I think I understand how it works. If I figure it out fully I plan to try to fix the bugs that the downloader bot has been plagued with for so long now. Here's the source, if anyone's forgotten where it is: https://bitbucket.org/spior/jk3files/src

 

Anyway, the first issue I'm trying to tackle is an instant crash I get when trying to download all of the Jedi Outcast mods from JK3files (using the bot's "JK2" parameter). The bot that I'm running I compiled directly from the source and I'm running it through the Visual Studio debugger. This is the error the debugger gives me: 

 

Unhandled exception at 0x1003d785 (msvcr100d.dll) in jk3files.exe: 0xC0000005: Access violation writing location 0x0128f140.

 

I also happened to notice that @@eezstreet mentions this particular bug in his "Debugging like a pro" tutorial at the end. Apparently this bug occurs because the code using a reference to a null pointer. 

 

Sure enough, in main.cpp I found a pointer set to NULL: 

 

file_t *files= NULL;
And then it's referenced later in jk3_func.cpp:

 

void J3F_GetData(
	const char *page, 
	char urls[][MAX_URL_LEN], int *num_url, 
	file_t *files, int *num_file, 
	int *num_page, int *curr, 
	int *num_files, long *num_dl, double *num_GB
	)

I think I understand the error but I'm not sure what I would have to do to fix it. Would I have to change the pointer to something other than a null? I've tried experimenting with some stuff but usually it just results in compiler errors.

Link to comment

In this case, the NULL pointer being assigned there is okay. Also, the first one will not have any effect on the second since they are in a different scope.

I suggest running the actual program itself in the Visual Studio debugger. It'll show you the exact line of code that crashes.

Link to comment

I have ran it in the Visual Studio 2010 debugger and the access violation error is the only thing the debugger window displays. Immediately after the error it points me with a yellow arrow to a line in the strncpy.asm file, which can't be the problem because it's code written by Microsoft. Perhaps the error occurs where the strncpy function is used in the bot code?

Link to comment

pass through the code? Meaning start the program and break it then press f10 until it crashes.

I'm really sorry I left you with this mess, but that fucking site would have killed me. It only worked when it wanted to D:

Ugh you can IM me for specifics or something, I guess since it's my code I should be able to understand it better hopefully

Link to comment

The yellow arrow in strncpy tells you a lot: A strncpy (or literally, "string copy n-characters") copies a number of characters from one memory pointer to another.

To get more detailed information on where this is occurring, open the Call Stack view. It'll show you the chain of functions that got you to the crash.

Remember the nature of the Access Violation. Either the first or second argument is winding up being NULL. These are the source and destination - it's trying to either read a string at 0x00 or write to 0x00, both of which are invalid.

Link to comment

I would hazard a guess that the stncopy's parameters are wrong and it is probably attempting to copy a value out of bounds that is causing your problem.  Also I just want to mention that if its c++ the new standard says you should use nullptr instead of NULL where possible.  Just fyi.

JKG Developer

Link to comment

I would hazard a guess that the stncopy's parameters are wrong and it is probably attempting to copy a value out of bounds that is causing your problem.  Also I just want to mention that if its c++ the new standard says you should use nullptr instead of NULL where possible.  Just fyi.

 

The yellow arrow in strncpy tells you a lot: A strncpy (or literally, "string copy n-characters") copies a number of characters from one memory pointer to another.

To get more detailed information on where this is occurring, open the Call Stack view. It'll show you the chain of functions that got you to the crash.

Remember the nature of the Access Violation. Either the first or second argument is winding up being NULL. These are the source and destination - it's trying to either read a string at 0x00 or write to 0x00, both of which are invalid.

 

Another day in the life of a coder? Interesting...

Link to comment

I think I'm closer to the error. In the call stack window, right under the entry that points to the strncpy.asm file, a green arrow points me to this line of code: 

 

strncpy(files[*num_file].dir, page, strstr(page, ";")-page);
It's within the J3F_GetData function. When the J3F_GetData function is called in main.cpp, it has 3 null parameters at the end.

 

J3F_GetData(urls[curr_url], urls, &num_url, files, &num_file, &num_page, &curr_url, NULL, NULL, NULL);
Maybe these three null parameters at the end are the problem? Still not entirely sure what to do.
Link to comment

I think I'm closer to the error. In the call stack window, right under the entry that points to the strncpy.asm file, a green arrow points me to this line of code: 

 

strncpy(files[*num_file].dir, page, strstr(page, ";")-page);

 

 

Something on this line is pointing to invalid memory and you're trying to read from it.

 

You basically need to figure out which of these is invalid:

  • files
  • num_file
  • *num_file
  • files[*num_file]
  • files[*num_file].dir
  • page
  • strstr(page, ";")-page
I just noticed your error message in the first post mentions an access violation while writing not reading.

 

Access violation writing location 0x0128f140.
This part of the exception is the crucial bit.

 

This narrows down the problem to the destination of your strncpy call being invalid. Your strncpy destination argument is files[*num_file].dir. The value of this variable (0x0128f140, from the error message) is bogus, and strncpy was unable to write to the destination this value was supposed to represent.

Link to comment

I think I'm closer to the error. In the call stack window, right under the entry that points to the strncpy.asm file, a green arrow points me to this line of code: 

 

strncpy(files[*num_file].dir, page, strstr(page, ";")-page);
It's within the J3F_GetData function. When the J3F_GetData function is called in main.cpp, it has 3 null parameters at the end.

 

J3F_GetData(urls[curr_url], urls, &num_url, files, &num_file, &num_page, &curr_url, NULL, NULL, NULL);
Maybe these three null parameters at the end are the problem? Still not entirely sure what to do.

 

In my experience, usually this happens when one of the variables being passed is off by one.  (A common mistake is misordering elements in an array so that they are off by one from their actual values, eg: position 0 is the first element, NOT position 1.  At this point passing a value that is off by one, because you assume 1 is the beginning makes things break).

JKG Developer

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...