about summary refs log tree commit diff stats
path: root/src/utils/twtstr.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-09 22:00:08 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-09 22:00:08 +0200
commitfb36a13cbe53a3b3ea60271b8d04a7c525e9d538 (patch)
treea5a5263a0a3d769952c7eaceabb1ee91eab2ad7a /src/utils/twtstr.nim
parent1769bc42e23ab5bddcb8a216f268b821bf364961 (diff)
downloadchawan-fb36a13cbe53a3b3ea60271b8d04a7c525e9d538.tar.gz
Fix some type confusion with colors, fix crash in pager
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r--src/utils/twtstr.nim41
1 files changed, 37 insertions, 4 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index c41a7e57..dd51eb2d 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -368,7 +368,6 @@ func japaneseNumber*(i: int): string =
     dec n
 
 # Implements https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#signed-integers
-#TODO TODO TODO handle overflow defects
 func parseInt32*(s: string): Option[int32] =
   var sign: int32 = 1
   var i = 0
@@ -382,8 +381,13 @@ func parseInt32*(s: string): Option[int32] =
   var integer = int32(decValue(s[i]))
   inc i
   while i < s.len and isDigit(s[i]):
+    if unlikely(integer != 0 and high(int32) div 10 < integer):
+      return none(int32) # overflow
     integer *= 10
-    integer += int32(decValue(s[i]))
+    let c = int32(decValue(s[i]))
+    if unlikely(high(int32) - c < integer):
+      return none(int32) # overflow
+    integer += c
     inc i
   return some(sign * integer)
 
@@ -400,11 +404,35 @@ func parseInt64*(s: string): Option[int64] =
   var integer = int64(decValue(s[i]))
   inc i
   while i < s.len and isDigit(s[i]):
+    if unlikely(integer != 0 and high(int64) div 10 < integer):
+      return none(int64) # overflow
     integer *= 10
-    integer += int64(decValue(s[i]))
+    let c = int64(decValue(s[i]))
+    if unlikely(high(int64) - c < integer):
+      return none(int64) # overflow
+    integer += c
     inc i
   return some(sign * integer)
 
+func parseUInt8*(s: string): Option[uint8] =
+  var i = 0
+  if i < s.len and s[i] == '+':
+    inc i
+  if i == s.len or s[i] notin AsciiDigit:
+    return none(uint8)
+  var integer = uint8(decValue(s[i]))
+  inc i
+  while i < s.len and isDigit(s[i]):
+    if unlikely(integer != 0 and high(uint8) div 10 < integer):
+      return none(uint8) # overflow
+    integer *= 10
+    let c = uint8(decValue(s[i]))
+    if unlikely(high(uint8) - c < integer):
+      return none(uint8) # overflow
+    integer += uint8(c)
+    inc i
+  return some(integer)
+
 func parseUInt32*(s: string): Option[uint32] =
   var i = 0
   if i < s.len and s[i] == '+':
@@ -414,8 +442,13 @@ func parseUInt32*(s: string): Option[uint32] =
   var integer = uint32(decValue(s[i]))
   inc i
   while i < s.len and isDigit(s[i]):
+    if unlikely(integer != 0 and high(uint32) div 10 < integer):
+      return none(uint32) # overflow
     integer *= 10
-    integer += uint32(decValue(s[i]))
+    let c = uint32(decValue(s[i]))
+    if unlikely(high(uint32) - c < integer):
+      return none(uint32) # overflow
+    integer += c
     inc i
   return some(integer)