Nim icon indicating copy to clipboard operation
Nim copied to clipboard

`dataField` in cgen depends on undefined behavior

Open arnetheduck opened this issue 6 years ago • 1 comments

https://github.com/nim-lang/Nim/blob/160a03464310d8a9e1ffaf83d73904df3439df78/compiler/cgen.nim#L249

Like lenExpr this should check for null of the seq/string - in practice, it kind of works because mainly what happens is an address calculation but it's a problem for analysis tools as well as simply because it's UB - practically, instead of returning null, it returns the offset of the data field, poisoning any future checks that might rely on null being the marker for "empty".

From a perf point of view, it shouldn't matter - the field is mostly used in conjunction with lenExpr, so the duplicate check will be elided.

arnetheduck avatar Dec 07 '18 15:12 arnetheduck

See #19066 for an example. Would be nice to fix this to suppress ubsan errors (specifically, -fsanitize=null).

In case it is useful: __attribute__((no_sanitize("null"))) can suppress the error.

MaskRay avatar Sep 04 '22 18:09 MaskRay

fixed by https://github.com/nim-lang/Nim/pull/20795

ringabout avatar Sep 29 '23 05:09 ringabout