google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.53k stars 10.09k forks source link

[FR]: Documentation: InitGoogleTest(): explicitely warn users about requiring 'nullptr' as the last element of argv #4434

Closed Romop5 closed 9 months ago

Romop5 commented 9 months ago

Does the feature exist in the most recent commit?

No

Why do we need this feature?

In your implementation of the argument parser, your code expects argv, passed to InitGoogleTest(), to contain nullptr as a last element of the array (technically, argv[argc] = nullptr;). For instance, this is the case in the following code: https://github.com/google/googletest/blob/8495449f075543dbac005d6c33118e58ad770d75/googletest/src/gtest.cc#L6657C1-L6658C70

As long as I have read the code and docs correctly, there isn't any explicit warning on that anywhere in the definition or docs of API, so I only found out the hard way, by trying to debug SIGSEGV occurring in our unit test runners.

After using search engine for a while, now I understand that this requirement comes from C++ standard of main() function and its argument, and possibly from your expectation that the arguments of main() are directly passed to InitGoogleTest.

According to standard, argv always contains nullptr as argc-th element, but this fact is not so well-known, and one can easily get mistaken when trying to wrap argv prior to passing to InitGoogleTest.

C++11's Draft: [basic.start.main] ... The value of argv[argc] shall be 0. ... https://timsong-cpp.github.io/cppwp/n3337/basic.start.main#2

Describe the proposal.

I propose to add a warning/note about expecting argv to contain a "null" element (argv[argc] = nullptr;) after the following paragraph:

https://github.com/google/googletest/blob/8495449f075543dbac005d6c33118e58ad770d75/docs/reference/testing.md?plain=1#L1320

You can also add a reference to C++ standard for reader to understand the origin of this seemingly arbitrary requirement.

Is the feature specific to an operating system, compiler, or build system version?

No, it's a standard property of C++'s main() function.