Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: Solve multiple sprintf() buffer overflows in dsinterval.c and yminterval.c#1156

Open
NotHimmel wants to merge 2 commits intoIvorySQL:masterIvorySQL/IvorySQL:masterfrom
NotHimmel:bugfix/05NotHimmel/IvorySQL:bugfix/05Copy head branch name to clipboard
Open

fix: Solve multiple sprintf() buffer overflows in dsinterval.c and yminterval.c#1156
NotHimmel wants to merge 2 commits intoIvorySQL:masterIvorySQL/IvorySQL:masterfrom
NotHimmel:bugfix/05NotHimmel/IvorySQL:bugfix/05Copy head branch name to clipboard

Conversation

@NotHimmel
Copy link
Collaborator

@NotHimmel NotHimmel commented Jan 21, 2026

Fix multiple sprintf() buffer overflows in dsinterval.c and yminterval.c

Summary by CodeRabbit

  • Bug Fixes
    • Improved memory-safety of interval formatting to prevent buffer overruns and crashes.
    • More robust and consistent interval output formatting (including fractional-second handling and trimming) while preserving existing display semantics.

✏️ Tip: You can customize this high-level summary in your review settings.

…interval.c

- Add buffer size parameter to AppendSeconds function
- Replace sprintf with snprintf for safer string formatting
- Fix buffer overflow vulnerabilities in interval data type functions

Fixes buffer overflow issues in INTERVAL DAY TO SECOND and INTERVAL YEAR TO MONTH data types
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 21, 2026

📝 Walkthrough

Walkthrough

Added buffer-size awareness to interval formatting: replaced internal sprintf calls with snprintf, added size_t bufsize parameters to AppendSeconds helpers, and propagated remaining-buffer calculations through call sites in dsinterval.c and yminterval.c (including an updated EncodeDsinterval signature).

Changes

Cohort / File(s) Summary
DS Interval formatting
contrib/ivorysql_ora/src/datatype/dsinterval.c
Added size_t bufsize parameter to AppendSeconds; changed EncodeDsinterval(..., char *str, size_t str_size, ...) signature. Replaced sprintf with snprintf and updated all call sites (e.g., dsinterval_out) to pass remaining buffer space (sizeof(buf) or str_size - (cp - str) - 1).
YM Interval formatting
contrib/ivorysql_ora/src/datatype/yminterval.c
Added size_t bufsize parameter to AppendSeconds and to internal Encode helper; replaced sprintf with snprintf and updated call sites to compute and pass remaining buffer space. No exported/public signatures changed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped through strings with careful paws,

Replaced wild sprintf with safer laws,
I pass the sizes, count each space,
No overflow now in any case,
🥕 — a vigilant Rabbit

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing sprintf() buffer overflows in two specific files (dsinterval.c and yminterval.c) by replacing unsafe sprintf calls with safer snprintf alternatives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
contrib/ivorysql_ora/src/datatype/dsinterval.c (1)

791-829: Fix remaining-buffer calculation; sizeof(str) can underflow and reintroduce overflow risk.

str is a char *, so sizeof(str) is pointer size. After cp advances, sizeof(str) - (cp - str) - 1 can underflow to a huge size_t, allowing snprintf to overrun the real buffer. It also truncates output when cp == str (size becomes 7/3). Pass the actual buffer size into EncodeDsinterval and compute remaining from that.

🐛 Proposed fix
-static void
-EncodeDsinterval(struct pg_tm * tm, fsec_t fsec, int style, char *str, int day_precision, int second_precision)
+static void
+EncodeDsinterval(struct pg_tm * tm, fsec_t fsec, int style, char *str,
+                 int day_precision, int second_precision, size_t bufsize)
 {
     char       *cp = str;
@@
-    snprintf(cp, sizeof(str) - (cp - str) - 1, "%0*d %02d:%02d:", day_precision, mday, hour, min);
+    snprintf(cp, bufsize - (cp - str), "%0*d %02d:%02d:", day_precision, mday, hour, min);
     cp += strlen(cp);
-    AppendSeconds(cp, sec, fsec, second_precision, true, sizeof(str) - (cp - str) - 1);
+    AppendSeconds(cp, sec, fsec, second_precision, true, bufsize - (cp - str));
@@
-EncodeDsinterval(tm, fsec, INTSTYLE_SQL_STANDARD, buf, day_precision, second_precision);
+EncodeDsinterval(tm, fsec, INTSTYLE_SQL_STANDARD, buf, day_precision, second_precision, sizeof(buf));
🤖 Fix all issues with AI agents
In `@contrib/ivorysql_ora/src/datatype/yminterval.c`:
- Around line 390-395: The call to AppendSeconds is using the original bufsize
instead of the remaining space, which can overflow; compute remaining = bufsize
- (cp - str) (ensure it is non-negative) and pass that remaining size to
AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true, remaining) so
AppendSeconds does not write past the end of str; update the invocation in
yminterval.c where snprintf advances cp and replace the final bufsize argument
accordingly.

Comment on lines +390 to +395
snprintf(cp, bufsize - (cp - str), "%c%d-%d %c%d %c%d:%02d:",
year_sign, abs(year), abs(mon),
day_sign, abs(mday),
sec_sign, abs(hour), abs(min));
cp += strlen(cp);
AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true, bufsize);
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Use remaining buffer size when appending seconds (overflow risk).

Here cp has already advanced, but AppendSeconds is passed the full bufsize, which lets it write past the end of str. This reintroduces the overflow risk.

🐛 Proposed fix
-AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true, bufsize);
+AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true, bufsize - (cp - str));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
snprintf(cp, bufsize - (cp - str), "%c%d-%d %c%d %c%d:%02d:",
year_sign, abs(year), abs(mon),
day_sign, abs(mday),
sec_sign, abs(hour), abs(min));
cp += strlen(cp);
AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true, bufsize);
snprintf(cp, bufsize - (cp - str), "%c%d-%d %c%d %c%d:%02d:",
year_sign, abs(year), abs(mon),
day_sign, abs(mday),
sec_sign, abs(hour), abs(min));
cp += strlen(cp);
AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true, bufsize - (cp - str));
🤖 Prompt for AI Agents
In `@contrib/ivorysql_ora/src/datatype/yminterval.c` around lines 390 - 395, The
call to AppendSeconds is using the original bufsize instead of the remaining
space, which can overflow; compute remaining = bufsize - (cp - str) (ensure it
is non-negative) and pass that remaining size to AppendSeconds(cp, sec, fsec,
MAX_INTERVAL_PRECISION, true, remaining) so AppendSeconds does not write past
the end of str; update the invocation in yminterval.c where snprintf advances cp
and replace the final bufsize argument accordingly.

Use str_size parameter instead of sizeof(str) to properly calculate
remaining buffer space, preventing output truncation.
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.

1 participant

Comments

Close sidebar
Morty Proxy This is a proxified and sanitized view of the page, visit original site.