summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTomohiro <gpuppur@gmail.com>2021-12-25 18:31:35 +0900
committerGitHub <noreply@github.com>2021-12-25 10:31:35 +0100
commitfdbec969d8601af0f666426b523e8ebba644ab56 (patch)
tree4505f2705c26d26a46f0d63c512fcab94b6accd0 /lib
parentfa96e56ad06f0562b5d608cc896a9b17c9512958 (diff)
downloadNim-fdbec969d8601af0f666426b523e8ebba644ab56.tar.gz
Fix #19107 (#19286) [backport]
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/strformat.nim12
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/pure/strformat.nim b/lib/pure/strformat.nim
index 746f01ace..40a33951c 100644
--- a/lib/pure/strformat.nim
+++ b/lib/pure/strformat.nim
@@ -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
@@ -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:
@@ -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)
@@ -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: