-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ptr char
implicitly converts to cstring, resulting in undefined behavior
#13790
Comments
Conversions from |
no, it's a pointless security hole that is just waiting to be exploited in some service one day. It has exact same undefined behavior, eg: when true:
const N=3
proc fun(a, b: ptr array[N, char]) =
echo a
proc main() =
var a = ['a','b', 'c']
var b = ['d','e', 'f']
fun(a.addr,b.addr)
main() with -d:danger you always print nimb c -r $timn_D/tests/nim/all/t10433.nim or in other situations it'd be the reverse; or maybe only in some architecture; or maybe in your released product all we need is explicit conversion via the There's zero downside to explicit conversion, only upsides; users can use explicit conversion when they know what they're doing, or even a converter if they don't want to bother, or perhaps working on a PR. |
and here's another "fun" gotcha illustrating dangers of implicit conversions: nim c -r --experimental:implicitDeref main proc fun[T](): int =
var a: array[2, T]
var b: ptr array[2, T] = a.addr
b.len
doAssert fun[int]() == 2
doAssert fun[int8]() == 2
doAssert fun[char]() == 2 # fails (and a O(1) operation became O(N)...) it reminds me of the classic mistake in C++ which was specializing |
@timotheecour You don't need to convince me any more about the danger of implicit conversions, especially not in this case. I am all on your side here. Just disable inplicit conversion to |
implicit cast from To solve this problem, Zig has another datatype: "array with sentinel value". In Nim, the (imagined) type equivalent to |
* fix =#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo * fixes ptr to cstring warnings[backport] * add fixes Co-authored-by: xflywind <[email protected]>
* fix =#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo * fixes ptr to cstring warnings[backport] * add fixes Co-authored-by: xflywind <[email protected]> (cherry picked from commit 06cd156)
…ing (#20761) * fix =#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo Co-authored-by: xflywind <[email protected]>
* fix =#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo * fixes ptr to cstring warnings[backport] * add fixes Co-authored-by: xflywind <[email protected]>
… to cstring (nim-lang#20761) * fix =nim-lang#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo Co-authored-by: xflywind <[email protected]>
* fix =nim-lang#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo * fixes ptr to cstring warnings[backport] * add fixes Co-authored-by: xflywind <[email protected]>
… to cstring (nim-lang#20761) * fix =nim-lang#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo Co-authored-by: xflywind <[email protected]>
* fix =nim-lang#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo * fixes ptr to cstring warnings[backport] * add fixes Co-authored-by: xflywind <[email protected]>
… to cstring (nim-lang#20761) * fix =nim-lang#13790 ptr char (+friends) should not implicitly convert to cstring * Apply suggestions from code review * first round; compiles on windows * nimPreviewSlimSystem * conversion is unsafe, cast needed * fixes more tests * fixes asyncnet * another try another error * last one * true * one more * why bugs didn't show at once * add `nimPreviewCstringConversion` switch * typo Co-authored-by: xflywind <[email protected]>
ptr char
implicitly converts to cstring, resulting in undefined behaviorptr array[N, char]
andptr UncheckedArray[N, char]
this introduces a weird special case in the language (eg
$a
doesn't compile for other types a: ptr[T] except for T=char) as well as weird bugs and undefined behavior in seemingly safe codeExample 1
Current Output
Expected Output
tests pass
ie, it should be illegal to do those implicit conversions from ptr char to cstring
Example 2
in case you may think it's a good idea to have implicit conversion from ptr char to cstring, consider this:
you get (different) garbage results when recompiling the same program:
Example 3
in other cases, you'll end up with buffer overflow bugs/attacks, since a
ptr char
could come from any source (eg ffi calls, malloc /createU
, etc) with nothing that should guarantee that it's 0 terminated.That problem is exacerbated when a
ptr char
field is nested somewhere deep inside an object and we are calling$
on itExample 4
likewise, implicit conversion from
ptr array[0..2, char]
tocstring
should be disallowed. It has the same caveats, eg:prints
and ditto for ptr UncheckedArray, eg:
Possible Solution
ptr char
,ptr[array[N, char]
etc)--legacy:implicitPtrCharToCstring
(adding to existing list--legacy:allowSemcheckedAstModification|checkUnsignedConversions
)Additional Information
The text was updated successfully, but these errors were encountered: