From a261760c59049b363515df9763b02ea9fd78e210 Mon Sep 17 00:00:00 2001 From: Philippe Symons Date: Wed, 28 May 2025 21:04:45 +0200 Subject: [PATCH] Fix regression ptgb::vector::resize() had a bug which caused it to incorrectly add/erase elements. The reason was a loop end condition that was modified during iterations of said loop. This end condition was intended to be static, but because it used a variable that was modified during the loop, it didn't repeat the intended number of times. Fix: before starting the for loop, calculate the end condition once. --- include/libstd_replacements.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/libstd_replacements.h b/include/libstd_replacements.h index 722484f..13e29c2 100644 --- a/include/libstd_replacements.h +++ b/include/libstd_replacements.h @@ -100,7 +100,8 @@ namespace ptgb { if(newSize < count_) { - for(size_t i=0; i < (count_ - newSize); ++i) + const size_t num_erase = (count_ - newSize); + for(size_t i=0; i < num_erase; ++i) { pop_back(); } @@ -108,7 +109,8 @@ namespace ptgb else if(newSize > count_) { reserve(newSize); - for(size_t i=0; i < (newSize - count_); ++i) + const size_t num_fill = (newSize - count_); + for(size_t i=0; i < num_fill; ++i) { push_back(value); }