Skip to content

Commit

Permalink
Fix savestates. Still don't understand quite how this broke though, i…
Browse files Browse the repository at this point in the history
…t should work. Fixes #9365
  • Loading branch information
hrydgard committed Feb 27, 2017
1 parent 5de190a commit 0e83a4c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Common/ChunkFile.cpp
Expand Up @@ -29,8 +29,9 @@ PointerWrapSection PointerWrap::Section(const char *title, int minVer, int ver)
char marker[16] = {0};
int foundVersion = ver;

truncate_cpy(marker, title);
if (!ExpectVoid(marker, sizeof(marker))) {
strncpy(marker, title, sizeof(marker));
if (!ExpectVoid(marker, sizeof(marker)))

This comment has been minimized.

Copy link
@unknownbrackets

unknownbrackets Mar 13, 2017

Collaborator

marker is not always being truncated here (by strncpy.) It's just a fixed-length string.

Example: 1234567890abcdef will get truncated to 1234567890abcde\0 by truncate_cpy, but not by strncpy. It's always comparing by exact length, so the null terminator isn't needed... I'm not confident this was intentional originally (darn confusing strncpy), but this is the reason it broke.

For the other two (GitVersion and title), I think truncate_cpy is more correct, as they are being null terminated always.

-[Unknown]

This comment has been minimized.

Copy link
@hrydgard

hrydgard Mar 13, 2017

Author Owner

That's indeed it. Thanks, I'll go ahead and add a comment.

{
// Might be before we added name markers for safety.
if (foundVersion == 1 && ExpectVoid(&foundVersion, sizeof(foundVersion)))
DoMarker(title);
Expand Down Expand Up @@ -283,11 +284,13 @@ CChunkFileReader::Error CChunkFileReader::SaveFile(const std::string &filename,
header.Revision = REVISION_CURRENT;
header.ExpectedSize = (u32)sz;
header.UncompressedSize = (u32)sz;
truncate_cpy(header.GitVersion, gitVersion);
strncpy(header.GitVersion, gitVersion, 32);
header.GitVersion[31] = '\0';

// Setup the fixed-length title.
char titleFixed[128];
truncate_cpy(titleFixed, title.c_str());
strncpy(titleFixed, title.c_str(), sizeof(titleFixed));
titleFixed[sizeof(titleFixed) - 1] = '\0';

// Write to file
if (compress) {
Expand Down

0 comments on commit 0e83a4c

Please sign in to comment.