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

large file write causing errors #5439

Closed
jkthorne opened this issue Dec 22, 2017 · 9 comments
Closed

large file write causing errors #5439

jkthorne opened this issue Dec 22, 2017 · 9 comments

Comments

@jkthorne
Copy link
Contributor

while running this code

data = Random::PCG32.new(1_u64).random_bytes(1_000_000_000)
File.write("static/data", data.to_s)

I get the error Negative size

Negative size (ArgumentError)
  from /usr/lib/crystal/pointer.cr:0:7 in 'realloc'
  from /usr/lib/crystal/string/builder.cr:124:5 in 'resize_to_capacity'
  from /usr/lib/crystal/string/builder.cr:45:7 in 'write'
  from /usr/lib/crystal/io.cr:487:7 in 'write_utf8'
  from /usr/lib/crystal/string.cr:4139:5 in 'to_s'
  from /usr/lib/crystal/io.cr:192:5 in '<<'
  from /usr/lib/crystal/slice.cr:669:7 in 'to_s'
  from /usr/lib/crystal/object.cr:96:7 in 'to_s'
  from temp.cr:2:27 in '__crystal_main'
  from /usr/lib/crystal/crystal/main.cr:11:3 in '_crystal_main'
  from /usr/lib/crystal/crystal/main.cr:112:5 in 'main_user_code'
  from /usr/lib/crystal/crystal/main.cr:101:7 in 'main'
  from /usr/lib/crystal/crystal/main.cr:135:3 in 'main'
  from __libc_start_main
  from _start
@ysbaddaden
Copy link
Contributor

ysbaddaden commented Dec 22, 2017

You overflow the Int32::MAX of sizes in Crystal. This is a duplicate of another issue (not validating sizes) but I can't find it.

@bew
Copy link
Contributor

bew commented Dec 22, 2017

@ysbaddaden talking about #3209 ?

@ysbaddaden
Copy link
Contributor

Yes. Thought 1 billion bytes is smaller than Int32::MAX, so an overflow is weird.

@RX14
Copy link
Contributor

RX14 commented Dec 22, 2017

@ysbaddaden there's a data.to_s in there, which is both almost certainly wrong and also will take it far beyond 1GiB of data.

@RX14 RX14 closed this as completed Dec 22, 2017
@jkthorne
Copy link
Contributor Author

So if this is not at Int::MAX why is it overflowing?

@RX14
Copy link
Contributor

RX14 commented Dec 22, 2017

@wontruefree it is > Int::MAX.

your data.to_s is like "Slice[123, 12, 240, 100 ... ]", which is a vastly longer and more useless representation than the raw slice. That's why I said it's "almost certainly wrong" meaning I really don't think you meant to_s.

@jkthorne
Copy link
Contributor Author

@RX14 I was able to write write just the slice to the file and it worked fine. If a char is a byte how is writing a string more space then a slice of bytes?

@RX14
Copy link
Contributor

RX14 commented Dec 23, 2017

@wontruefree because the string isn't binary. to_s on slice literally returns a string like "Slice[123, 12, 240, 100]" just the same as calling inspect.

@jkthorne
Copy link
Contributor Author

I have been messing with this a little more I will have to read more about it.
Thanks

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

4 participants