tesseract icon indicating copy to clipboard operation
tesseract copied to clipboard

Fix possible UB when accessing empty vector's data

Open nocun opened this issue 3 years ago • 4 comments

Make sure that the empty vector's data is never used, at() will throw an exception in that case.

This may allow compilers to better optimize code.

nocun avatar Jun 30 '21 16:06 nocun

I'd expect that code using at() is slower, so I am not sure whether this is really an improvement. Did you test code size and performance?

stweil avatar Jun 30 '21 16:06 stweil

I did not perform large scale tests so can't be sure if this is faster in all cases.

In my test case (single long line) this looks to be faster. Init() is not called that many times (about 30) and there should be virtually no exception related performance hit cause nothing is thrown.

nocun avatar Jun 30 '21 16:06 nocun

On the other hand it may be beneficial to do some testing on non-x86 platforms but I do not have those available atm.

Edit: actually this at() call is just after resize() which can also throw (on all platforms that tesseract supports I assume?) so the fact that there might be an exception should be a non-issue and it protects from UB.

nocun avatar Jun 30 '21 16:06 nocun

Probably std::vector::data() will help in this particular case.

danpla avatar Jun 24 '22 08:06 danpla