Skip to content

Fix buffer overflow in plot.cc: replace sprintf with snprintf#14

Merged
highperformancecoder merged 3 commits into
masterfrom
copilot/fix-code-scanning-alerts
Jul 18, 2026
Merged

Fix buffer overflow in plot.cc: replace sprintf with snprintf#14
highperformancecoder merged 3 commits into
masterfrom
copilot/fix-code-scanning-alerts

Conversation

Copilot AI commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

CodeQL alert #370 (cpp/overrunning-write, critical): two sprintf calls in src/plot.cc write into a 30-byte stack buffer with no size bound, risking overflow.

Change

  • src/plot.cc — replace unbounded sprintf with snprintf(label, sizeof(label), ...) for both the Pango and fallback label formatting paths.
// Before
sprintf(label, "%d×10<sup>%d</sup>", lead, omag);

// After
snprintf(label, sizeof(label), "%d×10<sup>%d</sup>", lead, omag);

The format string "%d×10<sup>%d</sup>" can produce up to ~38 bytes (multi-byte UTF-8 ×, HTML tags, large integers) against a 30-byte buffer — snprintf caps the write at sizeof(label).


This change is Reviewable

Copilot AI changed the title [WIP] Fix code scanning alert #370 Fix buffer overflow in plot.cc: replace sprintf with snprintf Jul 18, 2026
@highperformancecoder

Copy link
Copy Markdown
Owner

Prefer to increase the buffer size to be large enough.

@highperformancecoder highperformancecoder left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please just increase the buffer size to be sufficiently large

Copilot AI commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

Prefer to increase the buffer size to be large enough.

Done — increased label from 30 to 64 bytes and reverted to sprintf (commit Increase label buffer from 30 to 64 bytes). The longest format string ("%d×10<sup>%d</sup>") can produce at most ~31 bytes, so 64 is well clear. CodeQL confirms 0 alerts.

@highperformancecoder
highperformancecoder marked this pull request as ready for review July 18, 2026 08:19
Copilot AI review requested due to automatic review settings July 18, 2026 08:19
@highperformancecoder
highperformancecoder merged commit 9c33ebd into master Jul 18, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to address a critical CodeQL buffer-overrun finding in src/plot.cc related to formatting log-axis labels into a fixed-size stack buffer.

Changes:

  • Increased the stack buffer used by logAxisLabel() from 30 bytes to 64 bytes in src/plot.cc.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/plot.cc
string logAxisLabel(double x)
{
char label[30];
char label[64];
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.

3 participants