Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utf-8 check, C++ initial version #463

Merged
merged 4 commits into from
Jun 4, 2021

Conversation

P-D-E
Copy link
Contributor

@P-D-E P-D-E commented May 29, 2021

Thank you for submitting a pull request and becoming a contributor to the Vega Strike Core Engine.

Please answer the following:

Code Changes:

Issues:

Purpose:

  • What is this pull request trying to do? Preventing crashes from loading non-utf8 saved games via pre-emptive checks, alerting the user when it happens. Serving as a place for discussion of the issue, whatever direction the changes will go.
  • What release is this for? 0.8.x, 0.9.x
  • Is there a project or milestone we should apply this to? 0.8.x, 0.9.x

@P-D-E P-D-E mentioned this pull request May 29, 2021
2 tasks
Copy link
Contributor

@stephengtuggy stephengtuggy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of things. Looks good overall. The bit shifting is well documented, which I appreciate.

FWIW, I think savegame.cpp/.h is a perfectly fine place to put this functionality.

Thanks for the PR, @P-D-E !

engine/src/savegame.cpp Show resolved Hide resolved
engine/src/savegame.cpp Outdated Show resolved Hide resolved
@@ -174,6 +180,99 @@ void SaveFileCopy( const char *src, const char *dst )
}
}

class Utf8Checker {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit late to the party, but I wonder if you could have used some boost facility instead.
Something like https://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/charset_handling.html. It's possible to ask it to throw an exception. Other than that, LGTM.

Copy link
Contributor

@stephengtuggy stephengtuggy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this code a lot. I'm ready to give it my approval. What do you guys say?

@BenjamenMeyer BenjamenMeyer mentioned this pull request Jun 3, 2021
2 tasks
Copy link
Member

@BenjamenMeyer BenjamenMeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally this looks good; one minor change to move to the smart pointer object and we should be good to go

{
std::string path = GetSaveDir() + filename;
std::vector<BYTE> savegame = readFile(path);
std::unique_ptr<Utf8Checker> check(new Utf8Checker);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: does this need to be a pointer to an instance? Or can it just be a local instance?

Utf8Checker check;
...
if (check.validUtf8(...

If we don't need to allocate, then let's not. If there's a reason we need to, then okay; but I think we may not need to - stack allocation should be sufficient.

NOTE: This wasn't quite clear before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it can, @vegastrike/code-developers do you all agree on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could just be a local instance instead of a pointer, but personally, I don't think that it matters much one way or the other.

I say let's merge this PR as is.

Copy link
Contributor

@ermo ermo Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the Utf8Checker class instance is only ever used/accessed inside the scope of the isUtf8SaveGame function, then it makes sense to just use it directly as a local instance since it doesn't need to be passed around to other consumers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we could save some time by leaving the code the way it is here, but I see that that is not the way this is going. So sure, let's change it to use the UTF8Checker directly, as a local instance.

@P-D-E P-D-E linked an issue Jun 4, 2021 that may be closed by this pull request
@P-D-E P-D-E added this to the 0.8.x milestone Jun 4, 2021
@P-D-E P-D-E marked this pull request as ready for review June 4, 2021 09:22
@BenjamenMeyer
Copy link
Member

BenjamenMeyer commented Jun 4, 2021

please port this over to the 0.8.x branch.

@BenjamenMeyer BenjamenMeyer merged commit 561ae73 into vegastrike:master Jun 4, 2021
@P-D-E P-D-E deleted the Utf8_check_savegame_version branch June 4, 2021 17:01
@ermo
Copy link
Contributor

ermo commented Jun 4, 2021

please port this over to the 0.8.x branch.

@BenjamenMeyer: Who is supposed to do this?

@BenjamenMeyer
Copy link
Member

@ermo ideally the same person that did the PR; but anyone can do it:

$ git checkout 0.8.x
$ git checkout -b port_utf-8-heck
$ git cherry-pick <commit hash 1>
$ git cherry-pick <commit hash ...>
$ git cherry-pick <commit hash N>
$ git push origin port_utf-8-heck

Or something like that...some adjustments might need to be made, but that's the general process

@ermo
Copy link
Contributor

ermo commented Jun 6, 2021

Please port this over to the 0.8.x branch.

Who is supposed to do this?

Ideally the same person that did the PR; but anyone can do it:

$ git checkout 0.8.x
$ git checkout -b port_utf-8-heck
$ git cherry-pick <commit hash 1>
$ git cherry-pick <commit hash ...>
$ git cherry-pick <commit hash N>
$ git push origin port_utf-8-heck

Or something like that...some adjustments might need to be made, but that's the general process

@P-D-E: Would you mind taking a look at porting it to the 0.8.x branch since you're familiar with the code? 🥳

@P-D-E
Copy link
Contributor Author

P-D-E commented Jun 6, 2021

@ermo Benjamen and I talked about it and agreed that he'll take care of it 👍

This was referenced Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Py3 engine segfaults when using a savefile from a py2 engine
5 participants