In C++ mode, when GNULIB_NAMESPACE is defined, the programmer expects Gnulib-invented symbols to be in that namespace, not in the default namespace.
2026-02-23 Bruno Haible <[email protected]> strnul: Respect GNULIB_NAMESPACE. * lib/string.in.h (strnul): Use _GL_BEGIN_NAMESPACE, _GL_END_NAMESPACE. * tests/test-strnul-c++2.cc: New file, based on tests/test-strnul.c. * modules/strnul-c++-tests (Files): Add it. (Makefile.am): Arrange to compile and run test-strnul-c++2. diff --git a/lib/string.in.h b/lib/string.in.h index 33422a06e7..9997bd58e7 100644 --- a/lib/string.in.h +++ b/lib/string.in.h @@ -1260,11 +1260,13 @@ _GL_STRING_INLINE const char *gl_strnul (const char *string) } # endif # ifdef __cplusplus +_GL_BEGIN_NAMESPACE template <typename T> T strnul (T); template <> inline const char *strnul<const char *> (const char *s) { return gl_strnul (s); } template <> inline char *strnul< char *> ( char *s) { return const_cast<char *>(gl_strnul (s)); } +_GL_END_NAMESPACE # else # if (defined __GNUC__ && __GNUC__ + (__GNUC_MINOR__ >= 9) > 4 && !defined __cplusplus) \ || (defined __clang__ && __clang_major__ >= 3) \ diff --git a/modules/strnul-c++-tests b/modules/strnul-c++-tests index 2931df1cf1..94edd72ae2 100644 --- a/modules/strnul-c++-tests +++ b/modules/strnul-c++-tests @@ -1,5 +1,6 @@ Files: tests/test-strnul-c++.cc +tests/test-strnul-c++2.cc Status: c++-test @@ -11,7 +12,8 @@ configure.ac: Makefile.am: if ANSICXX -TESTS += test-strnul-c++ -check_PROGRAMS += test-strnul-c++ +TESTS += test-strnul-c++ test-strnul-c++2 +check_PROGRAMS += test-strnul-c++ test-strnul-c++2 test_strnul_c___SOURCES = test-strnul-c++.cc +test_strnul_c__2_SOURCES = test-strnul-c++2.cc endif diff --git a/tests/test-strnul-c++2.cc b/tests/test-strnul-c++2.cc new file mode 100644 index 0000000000..c7122bf5aa --- /dev/null +++ b/tests/test-strnul-c++2.cc @@ -0,0 +1,38 @@ +/* Test of strnul() function in C++ mode. + Copyright (C) 2026 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +#define GNULIB_NAMESPACE gnulib +#include <config.h> + +/* Specification. */ +#include <string.h> + +#include "macros.h" + +int +main () +{ + const char ro[] = "foo"; + char rw[] = "foo"; + + const char *ro_nul = GNULIB_NAMESPACE::strnul (ro); + char *rw_nul = GNULIB_NAMESPACE::strnul (rw); + + ASSERT (ro_nul - ro == 3); + ASSERT (rw_nul - rw == 3); + + return test_exit_status; +}
