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

Replace usages of sprintf with snprintf #3658

Merged
merged 2 commits into from
Dec 20, 2023
Merged

Replace usages of sprintf with snprintf #3658

merged 2 commits into from
Dec 20, 2023

Conversation

nickbianco
Copy link
Member

@nickbianco nickbianco commented Dec 19, 2023

Brief summary of changes

As the title suggests, snprintf replaces all usages of sprintf, which can be dangerous because it can potentially output more characters than can fit in the allocation size of the buffer string. The change suppresses a gcc compiler warning (which was the main motivation for the PR).

Testing I've completed

Looking for feedback on...

CHANGELOG.md (choose one)

  • no need to update because...not user facing.

This change is Reviewable

@nickbianco nickbianco requested a review from aymanhab December 19, 2023 22:34
Copy link
Member

@aymanhab aymanhab left a comment

Choose a reason for hiding this comment

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

One minor comment, otherwise LGTM

Reviewed 11 of 11 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @nickbianco)


OpenSim/Common/IO.cpp line 286 at r1 (raw file):

    if(_GFormatForDoubleOutput) {
        snprintf(_DoubleFormat, 256, "%%g");
    } else if(_Scientific) {

Considering this number comes from a different file/header, it may make sense to define this as const integer BUFFER_LENGTH and refer to it by name to ensure they don't get out of sync.

Copy link
Member Author

@nickbianco nickbianco left a comment

Choose a reason for hiding this comment

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

Ready for review.

Reviewable status: 10 of 12 files reviewed, 1 unresolved discussion (waiting on @aymanhab)


OpenSim/Common/IO.cpp line 286 at r1 (raw file):

Previously, aymanhab (Ayman Habib) wrote…

Considering this number comes from a different file/header, it may make sense to define this as const integer BUFFER_LENGTH and refer to it by name to ensure they don't get out of sync.

Good call, done. I added the constant and made the existing constant also constexpr.

Copy link
Member

@aymanhab aymanhab left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @nickbianco)

@aymanhab aymanhab merged commit 88da27e into main Dec 20, 2023
6 checks passed
@aymanhab aymanhab deleted the sprintf_to_snprintf branch December 20, 2023 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants