Skip to content
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

Obtaining string address results in IndexError #7860

Closed
endragor opened this issue May 22, 2018 · 6 comments
Closed

Obtaining string address results in IndexError #7860

endragor opened this issue May 22, 2018 · 6 comments

Comments

@endragor
Copy link
Contributor

Commit: c12726d

This code now produces IndexError, even though I think it shouldn't?

var s = ""
echo unsafeAddr(s[0])
@simonkrauter
Copy link
Contributor

From the changelog:

Accessing the binary zero terminator in Nim's native strings is now invalid. Internally a Nim string still has the trailing zero for zero-copy interoperability with cstring. Compile your code with the new switch --laxStrings:on if you need a transition period.

@Araq
Copy link
Member

Araq commented May 22, 2018

This always was an edge-case, not that this code never worked for seq. This code should be written as cstring(s).

@endragor
Copy link
Contributor Author

@trustable-code there is no accessing in the sample I provided, so the changelog doesn't explain the behaviour.

I'd expect taking address not to inject any runtime checks, especially when it's unsafeAddr.

@Araq
Copy link
Member

Araq commented May 24, 2018

I'd expect taking address not to inject any runtime checks, especially when it's unsafeAddr.

I agree and this will be done, but actually it never was possible. For strings it only worked because s[0] used to be no IndexError.

@zah
Copy link
Member

zah commented May 24, 2018

Isn't the problem here that we don't offer a separate call returning the starting address of the string? I don't see why the combination unsafeAddr(s[X]) should be blessed when s[X] doesn't denote a legal location. Having a starting address allows you to do any kind of pointer arithmetic yourself.

@krux02
Copy link
Contributor

krux02 commented Nov 1, 2018

As Araq mentioned, you should use cstring as the conversion function.

@krux02 krux02 closed this as completed Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants