pharo-project / pharo

Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk.
http://pharo.org
Other
1.2k stars 355 forks source link

'' allButFirst fails because ByteString does not understand basicNew #17073

Open winterstream opened 3 weeks ago

winterstream commented 3 weeks ago

Bug description

'' allButFirst fails with the error "PrimitiveFailed: primitive #basicNew: in ByteString class failed".

This happens because the new: method calls basicNew: when the provided ByteString is empty.

A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

Evaluate

'' allButFirst

in the Playground.

Expected behavior

An empty string should be returned.

Expected development cost

20 minutes.

welcome[bot] commented 3 weeks ago

Thanks for opening your first issue! Please check the CONTRIBUTING documents for some tips about which information should be provided. You can find information of how to do a Pull Request here: https://github.com/pharo-project/pharo/wiki/Contribute-a-fix-to-Pharo

GitHub
Contribute a fix to Pharo
Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk. - pharo-project/pharo
jordanmontt commented 3 weeks ago

The problem is not that ByteString does not understand allButFirst. In fact, all objects understand it since it's implemented in Behavior. The problem is that allButFirst calls copyFrom: n + 1 to: self size. (in this case n=1 since you don't want to copy the first element). And self size is zero (empty string). So, it tries to allocate a new ByteString object of size -1. And that causes the primitive to fail. The same problem happens if one does #() allButFirst.

But yes, this is a bug in my opinion. I'd say that the problem is the method SequenceableCollection>>#copyFrom: start to: stop that tries to allocate a collection of negative size.

If one does OrderedCollection empty allButFirst it correctly returns an empty collection. This is because OrderedCollection re-implements the copyFrom: start to: stop method.