Browse Source

fix(vector): resize should check m_capacity

greatbridf 11 months ago
parent
commit
2cb5366997
2 changed files with 8 additions and 11 deletions
  1. 1 1
      CMakeLists.txt
  2. 7 10
      gblibstdc++/include/vector

+ 1 - 1
CMakeLists.txt

@@ -7,7 +7,7 @@ set(CMAKE_CXX_LINK_EXECUTABLE
     "<CMAKE_LINKER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
     "<CMAKE_LINKER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
 
 
 set(CMAKE_ASM_FLAGS "-m32")
 set(CMAKE_ASM_FLAGS "-m32")
-set(C_CXX_FLAGS "-nostdinc -m32 -nostdlib -W -Wall -Wextra -Wno-builtin-declaration-mismatch -Wno-format -fverbose-asm -fno-exceptions -ffreestanding -fno-pic -mstack-protector-guard=global")
+set(C_CXX_FLAGS "-nostdinc -m32 -nostdlib -W -Wall -Wextra -Wno-stringop-overflow -Wno-builtin-declaration-mismatch -Wno-format -fverbose-asm -fno-exceptions -ffreestanding -fno-pic -mstack-protector-guard=global")
 set(CMAKE_C_FLAGS "${C_CXX_FLAGS} -Werror=implicit-int -Werror=implicit-function-declaration -Werror=strict-aliasing")
 set(CMAKE_C_FLAGS "${C_CXX_FLAGS} -Werror=implicit-int -Werror=implicit-function-declaration -Werror=strict-aliasing")
 set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -fno-use-cxa-atexit -fno-rtti")
 set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -fno-use-cxa-atexit -fno-rtti")
 set(CMAKE_CXX_LINK_FLAGS "")
 set(CMAKE_CXX_LINK_FLAGS "")

+ 7 - 10
gblibstdc++/include/vector

@@ -111,13 +111,10 @@ private:
     // make m_capacity >= n >= m_size
     // make m_capacity >= n >= m_size
     constexpr void _pre_resize(size_type n)
     constexpr void _pre_resize(size_type n)
     {
     {
-        if (n < m_size) {
-            while (n < m_size)
-                pop_back();
-        }
-        else if (n > m_size) {
-            reserve(n);
-        }
+        while (n < m_size)
+            pop_back();
+
+        reserve(n);
     }
     }
 
 
 public:
 public:
@@ -155,7 +152,7 @@ public:
         , m_size(std::exchange(other.m_size, 0))
         , m_size(std::exchange(other.m_size, 0))
         , m_capacity(std::exchange(other.m_capacity, 0))
         , m_capacity(std::exchange(other.m_capacity, 0))
         , m_alloc(std::move(other.m_alloc)) {}
         , m_alloc(std::move(other.m_alloc)) {}
-    
+
     constexpr vector(vector&& other, const Allocator& alloc)
     constexpr vector(vector&& other, const Allocator& alloc)
         : vector(alloc)
         : vector(alloc)
     {
     {
@@ -326,7 +323,7 @@ public:
 
 
     constexpr void shrink_to_fit()
     constexpr void shrink_to_fit()
     {
     {
-        if (m_size != m_capacity)
+        if (m_size < m_capacity)
             _reallocate_safe(m_size);
             _reallocate_safe(m_size);
     }
     }
     constexpr void clear() noexcept
     constexpr void clear() noexcept
@@ -337,7 +334,7 @@ public:
     {
     {
         size_type idx = pos - m_data;
         size_type idx = pos - m_data;
 
 
-        if (!pos)
+        if (!m_data)
             reserve(1);
             reserve(1);
 
 
         if (m_size == m_capacity)
         if (m_size == m_capacity)