flyx / NimYAML

YAML implementation for Nim
https://nimyaml.org
Other
189 stars 36 forks source link

BUG: loading zero to unsigned integer generates a YamlConstructionError #123

Closed ggxa closed 2 years ago

ggxa commented 2 years ago

Reduced reproduction code:

import streams
import yaml

type x = object
  y : uint32  # any unsigned integer type here will cause the exception

var o = x(y: 0)

let w = newFileStream("o.yaml", fmWrite)
dump(o, w)
w.close()

let r = newFileStream("o.yaml")
load(r, o)         

Suggested fix:

diff --git a/yaml/serialization.nim b/yaml/serialization.nim
index 40d8a4e..2a2b8c1 100644
--- a/yaml/serialization.nim
+++ b/yaml/serialization.nim
@@ -238,9 +238,9 @@ proc constructObject*[T: DefiniteUIntTypes](
     {.raises: [YamlConstructionError, YamlStreamError].} =
   ## construct an unsigned integer value from a YAML scalar
   constructScalarItem(s, item, T):
-    if item.scalarContent[0] == '0' and item.scalarContent[1] in {'x', 'X'}:
+    if item.scalarContent[0] == '0' and item.scalarContent.len > 1 and item.scalarContent[1] in {'x', 'X'}:
       result = parseHex[T](s, item.startPos, item.scalarContent)
-    elif item.scalarContent[0] == '0' and item.scalarContent[1] in {'o', 'O'}:
+    elif item.scalarContent[0] == '0' and item.scalarContent.len > 1 and item.scalarContent[1] in {'o', 'O'}:
       result = parseOctal[T](s, item.startPos, item.scalarContent)
     else: result = T(parseBiggestUInt(item.scalarContent))