The International Simutrans Forum

Development => Patches & Projects => Incorporated Patches and Solved Bug Reports => Topic started by: wackdone on July 04, 2012, 04:12:39 PM

Title: bug in vector_tpl<T> (patch is included)
Post by: wackdone on July 04, 2012, 04:12:39 PM
Hi all!

There is a bug (a mistake?) in tpl/vector_tpl.h in r5804.


void vector_tpl<T>::store_at(const uint 32 pos, cosnt T& elem)
    ...
    resize((pos & 0xFFFFFFF7) + 8);


No! the right mask value is 0xFFFFFFF8.
(== ((~0) << 3))

example case: size==8 and pos==8,
the method tries to append new entry at the bottom of vector with expansion,
but the 'size' variable doesn't change in current code.

I found it while writing new extension for original Simutrans.
This problem is too tiny (only 4bits inverted in source file) but serious,
and hard to encounter, I think.

patch file attached with this article, is using '#if' directive.
It's no use for applying on main trunk. Just modify only one character.
(Sorry, I had missed in attaching a patch file. The second file has same contents with first one.)

Best Regards.
Title: Re: bug in vector_tpl<T> (patch is included)
Post by: Dwachs on July 04, 2012, 06:34:32 PM
Thanks for spotting! At all places, where store_at is called, the vector was already appropriately resize()'d. So this error never took effect.

Fixed in 5806. Size is doubled until it fits the index.

Title: Re: bug in vector_tpl<T> (patch is included)
Post by: wackdone on July 04, 2012, 08:19:51 PM
Thank you for quick respone.
I also checked the use of store_at() by grep'ng source codes.
I saw this problem was coverd by limited usage of this method, as you say.

Thanks for updating repos, too.
Title: Re: bug in vector_tpl<T> (patch is included)
Post by: IgorEliezer on July 05, 2012, 12:44:25 AM
O wow, nice way to introduce yourself to the community.

And welcome to the community. :D