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

List.count returns a negative value for huge lists #1143

Open
PureFox48 opened this issue Feb 27, 2023 · 2 comments
Open

List.count returns a negative value for huge lists #1143

PureFox48 opened this issue Feb 27, 2023 · 2 comments

Comments

@PureFox48
Copy link
Contributor

Although, if you have enough memory, Wren allows you to create lists with up to UINT32_MAX (4,294,967,295) elements, I noticed today that if you go above INT32_MAX (2,147,483,647) then List.count returns a negative value:

var list = List.filled(2.pow(31) - 1, 0)
System.print(list.count)
list.add(1)
System.print(list.count)

/* output
2147483647
-2147483648
*/ 

I tracked this down to list_count returning list->elements.count which is an int and, on my system (Ubuntu 22.04, x64, GCC), that's a 32 bit signed integer.

On the face of it the type of count (a field of ValueBuffer) should really be uint32_t though as this is something which is defined deep in the bowels of the VM code, I'm not sure what repercussions there would be if we changed it. It might affect negative indexing perhaps.

Of course, it's not a big problem in practice but it's something to bear in mind if you need to create huge lists for some purpose.

@mhermier
Copy link
Contributor

Ideally it should even be size_t, unless we add some restriction on allocation of sizes. Anyway there is barely any checks for allocation failures. So even size_t will have some other problem in such cases.

@PureFox48
Copy link
Contributor Author

Yeah, I wondered about size_t though not planning on doing a PR. Just wanted to make folks aware of the problem, at least for now.

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

2 participants