Jump to content
  • 0

3CeAM With Visual Studio 2015 Hidden Issue


Rytech

Question


  • Group:  Members
  • Topic Count:  27
  • Topics Per Day:  0.01
  • Content Count:  319
  • Reputation:   198
  • Joined:  11/14/11
  • Last Seen:  

Hello. Im seeking some help for a strange issue that has appeared recently and I tried many things to resolve the issue but can't figure out the issue. Was told its best to post here even tho its not rAthena related.

For the past 10 years ive used Visual Studio 2008 on my windows XP system to work on the development of my project 3CeAM and never used anything newer due to needing a newer system to support a newer OS and VS 2010's IntelliSense was broken on C code. But now I got a newer system with Windows 8.1 (finally) and Visual Studio 2015. Loading the VS 2010 Solution in it updated it to a VS 2015 solution and also updated the project files. I had to change a few things to resolve some compile errors. I added a check to cbasetypes.h for the snprintf

//////////////////////////////////////////////////////////////////////////
// some redefine of function redefines for some Compilers
//////////////////////////////////////////////////////////////////////////
#if defined(_MSC_VER) || defined(__BORLANDC__)
#define strcasecmp			stricmp
#define strncasecmp			strnicmp
#define strncmpi			strnicmp
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf			_snprintf
#endif
#if defined(_MSC_VER) && _MSC_VER < 1400
#define vsnprintf			_vsnprintf
#endif
#else
#define strcmpi				strcasecmp
#define stricmp				strcasecmp
#define strncmpi			strncasecmp
#define strnicmp			strncasecmp
#endif
#if defined(_MSC_VER) && _MSC_VER > 1200
#define strtoull			_strtoui64
#endif

I added _WINSOCK_DEPRECATED_NO_WARNINGS to the preprocessor flags, and set the ignore default libraries to blank. And then compiled the login, char, and map. They compile fine with a warning or 2 which isnt anything to worry about. So I start up the server, connect, and mess around in-game for a while. Everything seems perfectly fine. But then I found this bug that never existed on VS 2008 compiles.

When logging out, everything goes as it should and character data is saved. But if I do certain things, like change from 1st job to a 2nd job, die once, or start a Taekwon mission, and then log out, the char server crashes. From what I can figure out these things affect something called registry variables. So the above things changes registry variables on the character like jobchange_level, PC_DIE_COUNTER, and TK_MISSION_ID. Loading in the crash to VS's debug shows this....

1128912492_Debug1.thumb.png.c2a974edaf2d51476efb818ae1a989aa.png

If I add registry variable data for jobchange_level to the save data and then try to log on, the map server will crash on login and show this....

1520457798_Debug3.thumb.png.151476850c6398f5c9be4a4ab124bc88.png

I tried running some server exe's on this system that were compiled in VS 2008 and they save/load the registry variable data with no issues. So clearly something is going on in VS 2015 but I can't figure out what the cause of the issue is. The only clue seen in these pics is the negative packet length??? I could try to find the cause of this and fix it but im wondering if this is a sign of a deeper problem that could likely cause other hidden problems I haven't found yet. If anyone can help with fixing this problem I would be very thankful.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  351
  • Reputation:   263
  • Joined:  09/08/13
  • Last Seen:  

@Rytech

int i,j,p,len;

This line reserves memory on the stack for the variables. But it doesn't initialize them.

So, they contain values after the execution of previous functions("garbage data").

There is a possibility that function "sscanf" will not assign the value to the variable "len", but you use it without any validation.

Different compilers generate different machine code. In this case, "len" variable can contain different values. 

For example, when we compile server by using VS2008 - the uninitialized "len" variable is equal "2" and it doesn't cause a crash.

But when we use VS2015 - the uninitialized "len" variable is equal "100500" and it causes a crash.

You should press the button "Run it (F8)" to execute code:

Clang (len == 0) https://rextester.com/TUUGW99405
VC++ (len == random value) https://rextester.com/DBNPV42856

As you can see, code is the same - the results are different for different compilers.

It isn't a problem of compilator. It is the problem of the code. 

Edited by Functor
  • MVP 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  27
  • Topics Per Day:  0.01
  • Content Count:  319
  • Reputation:   198
  • Joined:  11/14/11
  • Last Seen:  

@Functor

Thank you for the reply. I changed the first line so that len = 0 to make the code safer and it fixed the crash issue but the saved registry variables didn't load on login and now lost on logout. So sscanf isnt doing its job here for some reason. Im kinda rusty at the moment but how can this be fixed and how do I set a validation?

Link to comment
Share on other sites

  • 0

  • Group:  Developer
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  292
  • Reputation:   199
  • Joined:  05/03/13
  • Last Seen:  

You can refer to this: http://www.cplusplus.com/reference/cstdio/sscanf/

Look at the return value description in order to get an idea on how to validate the result. 

Edited by Normynator
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  27
  • Topics Per Day:  0.01
  • Content Count:  319
  • Reputation:   198
  • Joined:  11/14/11
  • Last Seen:  

I tried a validate check and a ShowDebug code to see what it was coming out to and it wasn't 2 for sure. It was -1??? I removed it and tried something different. I looked into the code some more and it turns out that the sscanf doesn't appear to be the issue. I added a few lines of debug code to check whats being received in the WBUFP(buf,p) part of the code.

1545534949_Debug5.thumb.png.fdb282d0cbef1b73e3b7f196ebbf0d96.png

When logging out with a jobchange_level value of 50 sent, this is what appeared....

1227104349_Debug4.png.f3f153329a977e8c3849ab1dacabec9f.png

That doesn't look right. So I tried the same test on a compile through VS 2008 and the result is completely different and looks right.

1619170388_Debug6.PNG.0cfb2c215415274f4fb2f6c533f88b89.PNG

Now I need to figure out why this is happening. Gurrrrr.

Edited by Rytech
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  351
  • Reputation:   263
  • Joined:  09/08/13
  • Last Seen:  

@Rytech If you will not solve it in 2 days - you should create archive with:

Quote

- "src" foldez
- "save" folder
- "map-server.exe" + "map-server.pdb" 
- "char-server.exe" + "char-server.pdb" 
- "login-server.exe" + "login-server.pdb"

upload to the file hosting and post link here. I will try to find time to check it.

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  351
  • Reputation:   263
  • Joined:  09/08/13
  • Last Seen:  

2 hours ago, Rytech said:

That doesn't look right.

It is the result of your masking of the problem. ? I mean:

10 hours ago, Rytech said:

make the code safer and it fixed the crash issue

 

P.S. Try to change from "%31c%n" to "%31s%n" + add validation the result of the execution "sscanf" function and value of "len" variable. Keep in mind that execution of a "%n" directive does not increment the assignment count returned at the completion of execution of the sscanf function.

Edited by Functor
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  27
  • Topics Per Day:  0.01
  • Content Count:  319
  • Reputation:   198
  • Joined:  11/14/11
  • Last Seen:  

OMFG IT WORKED!!!! Spent over a week trying to fix this bug. T_T

So if I change the %31c part to a s it shows on the char-server console what I see on the VS 2008 compile. Its saving as jobchange_level,50ÍÍÍÍÍÍÍÍÍÍÍÍÍ after that fix and I had to edit the sscanf below it on the %255c part to make it properly saves as jobchange_level,50. My mind is blown by how a code done by someone back in the 2000's worked fine until now needing a simple letter change. Now to fix the map server crash when logging back in but I SHOULD beable to fix it if its a similar issue. I will also add the validation part in while im at it.

Thank you so so much for the help. I don't normally like to ask for help but im glad I did. I will post again if I run into another difficult issue. Also is there anything else I should look for in all of the code that could be a possible issue with VS 2015 to fix?

Edited by Rytech
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  351
  • Reputation:   263
  • Joined:  09/08/13
  • Last Seen:  

Example - https://rextester.com/DGDTO79874

#include  <stdio.h>

int main(void)
{
    char buffer_1[32];
    char buffer_2[32];
    
    unsigned int len_1 = 0;
    unsigned int len_2 = 0;
    
    memset(buffer_1, '\0', sizeof(buffer_1));
    memset(buffer_2, '\0', sizeof(buffer_2));
    
    int result_1 = sscanf("hello_1", "%31c%n", buffer_1, &len_1);
    int result_2 = sscanf("hello_2", "%31s%n", buffer_2, &len_2);
     
    printf("%s - %u - result %d\n", buffer_1, len_1, result_1);
    printf("%s - %u - result %d\n", buffer_2, len_2, result_2);
    
    return 0;
}

The result:

Quote

hello_1 - 0 - result -1
hello_2 - 7 - result 1

As you can see if "sscanf" fails - it returns "-1". So, it is important to check the result.

 

11 minutes ago, Rytech said:

Also is there anything else I should look for in all of the code that could be a possible issue with VS 2015 to fix?

If I find something - I will let you know.

Edited by Functor
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...