Browse Source

Validate fixed-size strings

windows-imgui
Macoy Madson 2 months ago
parent
commit
db62fa7da0
  1. 17
      src/Introspection.cake
  2. 2
      test/Errors.cakedata

17
src/Introspection.cake

@ -248,6 +248,9 @@
(var field-type-element-size (<> (in std vector) Token))
(cond
;; A somewhat special case: repurpose element-size where a fixed char array is one element
((= 0 (strcmp determined-type-string "introspect-type-fixed-size-string"))
(tokenize-push field-type-element-size (token-splice (+ 2 core-type-token))))
(type-is-string
(tokenize-push field-type-element-size (sizeof char)))
(is-override-pointer ;; Assume pointers are the same size
@ -833,8 +836,18 @@
((= (path field > type) introspect-type-fixed-size-string)
(var str-write (* char)
(offset-pointer-to-type struct-out value-offset (* char)))
;; TODO Validate size
(strncpy str-write in-string value-length))
(when (>= value-length (path field > element-size))
(fprintf stderr "error: struct %s field %s has length %d, which cannot fit value of length %d" \
" and a null terminator. Value: "
(path struct-metadata > name)
(path field > name)
(type-cast (path field > element-size) int)
(type-cast value-length int))
(print-string-range in-string (+ in-string value-length) true)
(return false))
(strncpy str-write in-string value-length)
;; Ensure null terminator gets written
(set (at value-length str-write) 0))
((= (path field > type) introspect-type-string)
;; Don't allocate for empty strings
(unless value-length

2
test/Errors.cakedata

@ -1,5 +1,5 @@
(my-struct :name "Test struct"
:other-name "Other name"
:other-name "--------------------------------"
:yet-another-name "Another name"
:optional-name ""
:value 42

Loading…
Cancel
Save