Skip to content

Fixed buffer overrun; removed the last two uses of sprintf#24143

Merged
opencv-pushbot merged 1 commit intoopencv:4.xfrom
seanm:sprintf4
Aug 18, 2023
Merged

Fixed buffer overrun; removed the last two uses of sprintf#24143
opencv-pushbot merged 1 commit intoopencv:4.xfrom
seanm:sprintf4

Conversation

@seanm
Copy link
Copy Markdown
Contributor

@seanm seanm commented Aug 11, 2023

Prefer snprintf, which can never overflow.

In one case I cheated and used strcpy, because I cannot figure out the buffer size at that point in the code.

{
ptr = fs->resizeWriteBuffer( ptr, len + 9 );
sprintf( ptr, "<!-- %s -->", comment );
snprintf( ptr, len + 9, "<!-- %s -->", comment );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like it should be + 10 (+ '\0` char).

Including buffer allocation above (it is a bug).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Patch updated...

@opencv-alalek opencv-alalek added this to the 4.9.0 milestone Aug 16, 2023
@opencv-alalek opencv-alalek added backport is needed Label for maintainers. Authors of PR can ignore this affected: 3.4 labels Aug 16, 2023
Fixed an off-by-1 buffer resize, the space for the null termination was forgotten.

Prefer snprintf, which can never overflow (if given the right size).

In one case I cheated and used strcpy, because I cannot figure out the buffer size at that point in the code.
@seanm seanm changed the title Removed the last two uses of sprintf Fixed buffer overrun; removed the last two uses of sprintf Aug 17, 2023
Copy link
Copy Markdown
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

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

Thank you 👍

@opencv-alalek opencv-alalek self-assigned this Aug 17, 2023
@opencv-pushbot opencv-pushbot merged commit ca0bd70 into opencv:4.x Aug 18, 2023
@asmorkalov asmorkalov mentioned this pull request Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

affected: 3.4 backport is needed Label for maintainers. Authors of PR can ignore this bug category: core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants