Bug 271227 - C++ ATF_REQUIRE_EQ cannot be used with static_cast<const char *>(NULL)
Summary: C++ ATF_REQUIRE_EQ cannot be used with static_cast<const char *>(NULL)
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: misc (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-05-03 14:00 UTC by Ed Maste
Modified: 2023-05-08 00:28 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ed Maste freebsd_committer freebsd_triage 2023-05-03 14:00:41 UTC
In contrib/atf/atf-c/macros.h we have:

#define ATF_REQUIRE_EQ(expected, actual) \
    ATF_REQUIRE_MSG((expected) == (actual), "%s != %s", #expected, #actual)

#define ATF_REQUIRE_STREQ(expected, actual) \
    ATF_REQUIRE_MSG(strcmp(expected, actual) == 0, "%s != %s (%s != %s)", \
                    #expected, #actual, expected, actual)

ATF_REQUIRE_EQ tests that the (arbitrary) arguments are equal, while ATF_REQUIRE_STREQ compares (and prints differing) strings.

In contrast, in contrib/atf/atf-c++/macros.hpp we have only ATF_REQUIRE_EQ:

#define ATF_REQUIRE_EQ(expected, actual) \
    do { \
        if ((expected) != (actual)) { \
            std::ostringstream atfu_ss; \
            atfu_ss << "Line " << __LINE__ << ": " \
                    << #expected << " != " << #actual \
                    << " (" << (expected) << " != " << (actual) << ")"; \
            atf::tests::tc::fail(atfu_ss.str()); \
        } \
    } while (false)

In lib/libnv/tests/cnv_tests.cc we have:

        ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie),
            static_cast<const char *>(NULL));

which GCC warns will pass NULL to a fn with a nonnull arg:

In file included from /usr/obj/tmp/cirrus-ci-build/amd64.amd64/tmp/usr/include/c++/v1/string:536:
In static member function 'static constexpr size_t std::__1::char_traits<char>::length(const char_type*)',
    inlined from 'std::__1::basic_ostream<char, _Traits>& std::__1::operator<<(basic_ostream<char, _Traits>&, const char*) [with _Traits = char_traits<char>]' at /usr/obj/tmp/cirrus-ci-build/amd64.amd64/tmp/usr/include/c++/v1/ostream:902:43,
    inlined from 'virtual void {anonymous}::atfu_tc_cnvlist_free_nvlist_array::body() const' at /tmp/cirrus-ci-build/lib/libnv/tests/cnv_tests.cc:1438:3:
/usr/obj/tmp/cirrus-ci-build/amd64.amd64/tmp/usr/include/c++/v1/__string/char_traits.h:218:30: warning: argument 1 null where non-null expected [-Wnonnull]
  218 |       return __builtin_strlen(__s);
      |              ~~~~~~~~~~~~~~~~^~~~~
Comment 1 Mariusz Zaborski freebsd_committer freebsd_triage 2023-05-07 12:30:07 UTC
Hey Ed,

How can I reproduce this?
I have tried to use g++14 and I don't see this warning/error.
Comment 2 Ed Maste freebsd_committer freebsd_triage 2023-05-08 00:28:00 UTC
(In reply to Mariusz Zaborski from comment #1)
It's reproducible with the Cirrus-CI job e.g. https://cirrus-ci.com/task/6329048180195328

It uses amd64-gcc12