0

I made a text adventure with a load/save option. In the folder I created the game it worked fine. And even if I do a release version of my game it all works how it should.

But I have to give this project to my professor. On my PC the saving and loading works fine but on my laptop or a copy of my projectfolder, i get the errormessage "a buffer overrun has accured". So what's my fault here?

My Code to load/save:

bool save()
{
    char sFileText[1024];
    char sFileName[80];
    char sFileDirectory[80];

     GetCurrentDirectory(512, sFileDirectory); 

    sprintf(sFileName, "%s\\save_game.ini", sFileDirectory);

    sprintf(sFileText, "[settings]\n\rsp_loop=%i\n\rroom_iSelect=%i\n\riSelect=%i\n\rraum=%i\n\rtor402=%i\n\rwegiSelect=%i\n\rwegweiser=%i\n\rlight=%i\n\ropen_door=%i\n\rmesser=%i\n\rmesserwurf=%i\n\rkamera=%i\n\rstein=%i\n\ralive=%i\n\rpfeil=%i\n\rluke=%i\n\rkuehlschrank=%i\n\rpulver_schmand=%i\n\rteddy=%i\n\rseil=%i\n\rgiftklinge=%i\n\r",
         sp_loop,
         room_iSelect,
         iSelect,
         raum,
         tor402,
         wegiSelect,
         wegweiser,
         light,
         open_door,
         messer,
         messerwurf,
         kamera,
         stein,
         alive,
         pfeil,
         luke,
         kuehlschrank,
         pulver_schmand,
         teddy,
         seil,
         giftklinge
        );


    int cur_char;
    FILE *out_file;

    out_file = fopen(sFileName, "w");
    if (!out_file == NULL) 
    {
        for (cur_char = 0; cur_char < strlen(sFileText); ++cur_char) 
        {
            fputc(sFileText[cur_char], out_file);
        }
    }
    else
    {
        return false;
    }

    fclose(out_file);
    return true;
}

bool open()
{
    char sFileText[1024];
    char sFileName[12];
    char sFileGameSettingsIni[80];

    sprintf(sFileName, "save_game.ini");



    GetCurrentDirectory(512, sFileGameSettingsIni); 

    sprintf(sFileGameSettingsIni, "%s\\%s", sFileGameSettingsIni, sFileName);

    sp_loop = GetPrivateProfileInt("settings", "sp_loop", 512, sFileGameSettingsIni);

    raum = GetPrivateProfileInt("settings", "raum", 512, sFileGameSettingsIni);
    tor402 = GetPrivateProfileInt("settings", "tor402",512, sFileGameSettingsIni);
    wegiSelect = GetPrivateProfileInt("settings", "wegiSelect", 512, sFileGameSettingsIni);
    wegweiser = GetPrivateProfileInt("settings", "wegweiser", 512, sFileGameSettingsIni);
    light = GetPrivateProfileInt("settings", "light", 512, sFileGameSettingsIni);
    open_door = GetPrivateProfileInt("settings", "open_door",512, sFileGameSettingsIni);
    messer = GetPrivateProfileInt("settings", "messer", 512, sFileGameSettingsIni);
    messerwurf = GetPrivateProfileInt("settings", "messerwurf", 512, sFileGameSettingsIni);
    kamera = GetPrivateProfileInt("settings", "kamera", 512, sFileGameSettingsIni);
    stein = GetPrivateProfileInt("settings", "stein", 512,sFileGameSettingsIni);
    alive = GetPrivateProfileInt("settings", "alive", 512,sFileGameSettingsIni);
    pfeil = GetPrivateProfileInt("settings", "pfeil", 512, sFileGameSettingsIni);
    luke = GetPrivateProfileInt("settings", "luke", 512,  sFileGameSettingsIni);
    kuehlschrank = GetPrivateProfileInt("settings", "kuehlschrank", 512, sFileGameSettingsIni);
    pulver_schmand = GetPrivateProfileInt("settings", "pulver_schmand", 512, sFileGameSettingsIni);
    teddy = GetPrivateProfileInt("settings", "teddy", 512, sFileGameSettingsIni);
    seil = GetPrivateProfileInt("settings", "seil", 512, sFileGameSettingsIni);
    giftklinge = GetPrivateProfileInt("settings", "giftklinge",512, sFileGameSettingsIni);

    return true;
}
1
  • should be tagged C instead of c++ I reckon. Probably more code needed too.
    – matiu
    Jan 11, 2012 at 22:20

3 Answers 3

1

You define sFileName as sFileName[12] and then attempt to write 14 bytes to it (13 and a NULL). It also looks like the buffer you are passing to GetCurrentDirectory() isn't big enough. You are stating the buffer is 512 bytes in length, when you only define it as 80 characters wide.

The return value of GetCurrentDirectory() will inform you of how large the buffer needs to be.

1

A possible cause is incorrect use of GetCurrentDirectory:

char sFileDirectory[80];

GetCurrentDirectory(512, sFileDirectory);

This says that sFileDirectory can hold a maximum of 512 characters: it can hold a maximum of 80 only (including NULL terminator) so this could easily write beyond the end of sFileDirectory.

Change to:

GetCurrentDirectory(sizeof(sFileDirectory) - 1, sFileDirectory); /* Check the return value. */

It may work on your PC due to shorter current directory, where on a different PC it could be stored in a directory with a path greater than 79 characters.

Another problem is:

char sFileName[12];

sprintf(sFileName, "save_game.ini");

"save_game.ini" is 13 characters long. Change to:

char sFileName[14];

sprintf(sFileName, "save_game.ini");

or:

const char* sFileName = "save_game.ini";

Also consider using _snprintf instead of sprintf.

0
0

sFileDirectory it's pretty small compared to the buffer size for GetCurrentDirectory. Try to enlanrge to fit the same size. It's probably different on release machine.

1
  • i found a pretty simple solution... instead of writing sFileName[12]; i wrote sFileName[] = "save_game.ini"; Now this works fine! Thx a lot to all! Jan 11, 2012 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.