ropes: make lib & compiler frmt more similar, fix out-of-bounds accesses in lib ropes
This commit is contained in:
parent
91f42a2943
commit
aafbe5c866
2 changed files with 30 additions and 15 deletions
|
|
@ -264,8 +264,22 @@ proc `%`*(frmt: TFormatStr, args: openArray[PRope]): PRope =
|
||||||
while true:
|
while true:
|
||||||
j = j * 10 + ord(frmt[i]) - ord('0')
|
j = j * 10 + ord(frmt[i]) - ord('0')
|
||||||
inc(i)
|
inc(i)
|
||||||
if (i > length - 1) or frmt[i] notin {'0'..'9'}: break
|
if (i >= length) or frmt[i] notin {'0'..'9'}: break
|
||||||
num = j
|
num = j
|
||||||
|
if j > high(args) + 1:
|
||||||
|
errorHandler(rInvalidFormatStr, $(j))
|
||||||
|
else:
|
||||||
|
add(result, args[j-1])
|
||||||
|
of '{':
|
||||||
|
inc(i)
|
||||||
|
var j = 0
|
||||||
|
while i < length and frmt[i] in {'0'..'9'}:
|
||||||
|
j = j * 10 + ord(frmt[i]) - ord('0')
|
||||||
|
inc(i)
|
||||||
|
num = j
|
||||||
|
if i < length and frmt[i] == '}': inc(i)
|
||||||
|
else: errorHandler(rInvalidFormatStr, $(frmt[i]))
|
||||||
|
|
||||||
if j > high(args) + 1:
|
if j > high(args) + 1:
|
||||||
errorHandler(rInvalidFormatStr, $(j))
|
errorHandler(rInvalidFormatStr, $(j))
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -315,16 +315,17 @@ proc `%`*(frmt: string, args: openArray[Rope]): Rope {.
|
||||||
while true:
|
while true:
|
||||||
j = j * 10 + ord(frmt[i]) - ord('0')
|
j = j * 10 + ord(frmt[i]) - ord('0')
|
||||||
inc(i)
|
inc(i)
|
||||||
if frmt[i] notin {'0'..'9'}: break
|
if (i >= length) or frmt[i] notin {'0'..'9'}: break
|
||||||
add(result, args[j-1])
|
add(result, args[j-1])
|
||||||
of '{':
|
of '{':
|
||||||
inc(i)
|
inc(i)
|
||||||
var j = 0
|
var j = 0
|
||||||
while frmt[i] in {'0'..'9'}:
|
while i < length and frmt[i] in {'0'..'9'}:
|
||||||
j = j * 10 + ord(frmt[i]) - ord('0')
|
j = j * 10 + ord(frmt[i]) - ord('0')
|
||||||
inc(i)
|
inc(i)
|
||||||
if frmt[i] == '}': inc(i)
|
if i < length and frmt[i] == '}': inc(i)
|
||||||
else: raise newException(ValueError, "invalid format string")
|
else: raise newException(ValueError, "invalid format string")
|
||||||
|
|
||||||
add(result, args[j-1])
|
add(result, args[j-1])
|
||||||
else: raise newException(ValueError, "invalid format string")
|
else: raise newException(ValueError, "invalid format string")
|
||||||
var start = i
|
var start = i
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue