Skip to content

Refactor shell_batch.cpp #2342

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

Merged
merged 3 commits into from
Mar 20, 2023
Merged

Conversation

MeAreJeenius
Copy link
Contributor

No description provided.

@FeralChild64 FeralChild64 requested review from johnnovak and kcgen March 15, 2023 19:22
@FeralChild64 FeralChild64 added the cleanup Non-functional changes that simplify, improve maintainability, or squash warnings label Mar 15, 2023
Copy link
Member

@kcgen kcgen left a comment

Choose a reason for hiding this comment

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

Very nice cleanup, @MeAreJeenius !

Mostly minor comments.

My main concern is that we don't have regressions; batch processing is so variable and nuanced, I'm not sure if there are some test batch files out there that we can feed through this to make sure all the corner cases are hit (and that we're not dropping functionality).

@kcgen
Copy link
Member

kcgen commented Mar 15, 2023

@MeAreJeenius , are you on Linux or macOS? If so, can you run it through UBASAN:

meson setup -Dbuildtype=debug -Db_sanitize=address,undefined build/ubasan
meson compile -C build/ubasan

Binary: build/ubasan/dosbox

Also, set this to catch all ASAN issues:

export ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1:detect_leaks=1

With those in place, run the binary against a test suite of batch files. If there are any issues, the sanitizer instrumentation will report it in the console output.

@kcgen kcgen added the refactoring Code refactoring without any functional changes label Mar 15, 2023
@MeAreJeenius MeAreJeenius force-pushed the shell-batch-refactor branch from b047889 to f7157ea Compare March 16, 2023 04:55
@MeAreJeenius
Copy link
Contributor Author

MeAreJeenius commented Mar 16, 2023

@kcgen Good call on the sanitizers. No clue why I didn't use them sooner in these commits.

Turns out, strncpy doesn't guarantee a '\0' at the end like snprintf, so strlen would trigger a stack buffer overflow (fixed now).

Also found an initialization-order-fiasco right at startup.

@MeAreJeenius MeAreJeenius force-pushed the shell-batch-refactor branch from f7157ea to 8cb23d6 Compare March 17, 2023 00:14
Replace char buffers with std::string.

Split up into a few functions.

Simplify the control flow.

Remove the cycle hack, as it doesn't appear to do much.
Cycles can be set within batch files without it.

Properly identify labels and comments when preceeded by tabs,
equals signs, and spaces.

Truncate commands that are too long instead of crashing.
Later commits should refactor the function and the code that calls
it so that it uses a std::string instead of a fixed size
char buffer.
Break into several functions.

Simplify control flow.

Allow colon to be preceeded by spaces tabs and equals signs.
Run clang-format on the file.
Remove unnecessary initializers from the constructor.
Remove undeeded headers.
Rename char constants, and remove uncessary ones.
If file is not openable, give warning instead of error.
@MeAreJeenius MeAreJeenius force-pushed the shell-batch-refactor branch from 8cb23d6 to 88d173f Compare March 19, 2023 23:22
@MeAreJeenius
Copy link
Contributor Author

In addition to implementing the feedback, I reworked ExpandedBatchLine() to be simpler. Instead of the funky state machine thing I had going on, it's shorter and more straightforward.

ReadLine() also now trims the lines instead of just cutting off the newline, so now whitespace-only lines are skipped.

@kcgen
Copy link
Member

kcgen commented Mar 20, 2023

Very nice refactoring, @MeAreJeenius! I think we're clear to merge 🚀

(PS- MSYS2 on GitHub's side has broke FluidSynth's unit tests; this isn't due to any change on the repo's side, as it was previously running in the last main build here: https://github.com/dosbox-staging/dosbox-staging/actions/runs/4440895580)

@kcgen kcgen merged commit 1047e48 into dosbox-staging:main Mar 20, 2023
@MeAreJeenius MeAreJeenius deleted the shell-batch-refactor branch April 27, 2023 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Non-functional changes that simplify, improve maintainability, or squash warnings refactoring Code refactoring without any functional changes
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants