Skip to content

[Bug]: Segmentation fault with EXPECT_* failure when using std::vector #4443

Description

@josearnal

Describe the issue

When looping through the elements of a vector and testing each value, if the vector is too long and the test fails, a segmentation fault will occur.

Here are two basic tests to reproduce the problem. The first one will fail but not seg fault. Thew later will seg fault.

#include "gtest/gtest.h"
#include<vector>

TEST(SegFaultDemonstration, ShortVector) {
    std::vector<double> low_values{0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9};
    for(int i=0;i<=low_values.size()-1;i++){
        EXPECT_GT(low_values[i], 1.0);
    }
}

TEST(SegFaultDemonstration, LongVector) {
    std::vector<double> low_values{0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.99};
    for(int i=0;i<=low_values.size()-1;i++){
        EXPECT_GT(low_values[i], 1.0);
    }
}

I've also encountered similar problems if the name of the vector is long, but I am having troubles making a minimal reproducible example for this case.

EDIT:

It turns out that merely initializing "long" vectors causes segmentation faults. What's going on here? For example, the following test will seg fault.

TEST(SegFaultDemonstration, twoVectors) {

    std::vector<double> vector1{0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.99};
    std::vector<double> vector2{0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.99};
}

Steps to reproduce the problem

Here's my CMakeLists.txt

# Note: CMake support is community-based. The maintainers do not use CMake
# internally.

cmake_minimum_required(VERSION 3.13)

project(googletest-distribution)
set(GOOGLETEST_VERSION 1.14.0)

if(NOT CYGWIN AND NOT MSYS AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL QNX)
  set(CMAKE_CXX_EXTENSIONS OFF)
endif()

enable_testing()

include(CMakeDependentOption)
include(GNUInstallDirs)

# Note that googlemock target already builds googletest.
option(BUILD_GMOCK "Builds the googlemock subproject" ON)
option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" ON)
option(GTEST_HAS_ABSL "Use Abseil and RE2. Requires Abseil and RE2 to be separately added to the build." OFF)

if(BUILD_GMOCK)
  add_subdirectory( googlemock )
else()
  add_subdirectory( googletest )
endif()
[jose@agrajag googletest]$ vi CMakeLists.txt 
[jose@agrajag googletest]$ cat CMakeLists.txt 
# Note: CMake support is community-based. The maintainers do not use CMake
# internally.

cmake_minimum_required(VERSION 3.13)

project(googletest-distribution)
set(GOOGLETEST_VERSION 1.14.0)

if(NOT CYGWIN AND NOT MSYS AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL QNX)
  set(CMAKE_CXX_EXTENSIONS OFF)
endif()

enable_testing()

include(CMakeDependentOption)
include(GNUInstallDirs)

# Note that googlemock target already builds googletest.
option(BUILD_GMOCK "Builds the googlemock subproject" ON)
option(INSTALL_GTEST "Enable installation of googletest. (Projects embedding googletest may want to turn this OFF.)" ON)
option(GTEST_HAS_ABSL "Use Abseil and RE2. Requires Abseil and RE2 to be separately added to the build." OFF)

if(BUILD_GMOCK)
  add_subdirectory( googlemock )
else()
  add_subdirectory( googletest )
endif()

What version of GoogleTest are you using?

1.14

What operating system and version are you using?

Linux: AlmaLinux 8.5 (Arctic Sphynx)

What compiler and version are you using?

gcc version 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)

What build system are you using?

cmake version 3.20.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions