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

Fix #19107 Add bounds check to strformat #19286

Merged
merged 1 commit into from
Dec 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/pure/strformat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ template formatValue(result: var string; value: cstring; specifier: string) =
result.add value

proc strformatImpl(f: string; openChar, closeChar: char): NimNode =
template missingCloseChar =
error("invalid format string: missing closing character '" & closeChar & "'")

if openChar == ':' or closeChar == ':':
error "openChar and closeChar must not be ':'"
var i = 0
Expand Down Expand Up @@ -618,6 +621,8 @@ proc strformatImpl(f: string; openChar, closeChar: char): NimNode =
let start = i
inc i
i += f.skipWhitespace(i)
if i == f.len:
missingCloseChar
if f[i] == closeChar or f[i] == ':':
result.add newCall(bindSym"add", res, newLit(subexpr & f[start ..< i]))
else:
Expand All @@ -627,6 +632,9 @@ proc strformatImpl(f: string; openChar, closeChar: char): NimNode =
subexpr.add f[i]
inc i

if i == f.len:
missingCloseChar

var x: NimNode
try:
x = parseExpr(subexpr)
Expand All @@ -639,10 +647,10 @@ proc strformatImpl(f: string; openChar, closeChar: char): NimNode =
while i < f.len and f[i] != closeChar:
options.add f[i]
inc i
if i == f.len:
missingCloseChar
if f[i] == closeChar:
inc i
else:
doAssert false, "invalid format string: missing '}'"
result.add newCall(formatSym, res, x, newLit(options))
elif f[i] == closeChar:
if i<f.len-1 and f[i+1] == closeChar:
Expand Down