From 8fc9b9d61bb39ce7e431d705a93a9a7a3d1fb8d6 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Fri, 25 Dec 2020 09:25:08 +0100 Subject: [PATCH] Address some minor lints A mildly interesting one is the call to test_wchar2utf8 with a non-null pointer ("u1"/"dst") but 0 length. In this case we relied on malloc(0) returning non-null which is not guaranteed. src/fish_tests.cpp:1619:23: warning: Call to 'malloc' has an allocation size of 0 bytes [clang-analyzer-optin.portability.UnixAPI] mem = (char *)malloc(dlen); ^ test_wchar2utf8(w1, sizeof(w1) / sizeof(*w1), u1, 0, 0, 0, "invalid params, dst is not NULL"); --- src/fish_tests.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index d453fcfb9..3d822569d 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -816,7 +816,7 @@ static void test_fd_monitor() { item_maker_t(const item_maker_t &) = delete; // Write 42 bytes to our write end. - void write42() { + void write42() const { char buff[42] = {0}; (void)write_loop(writer.fd(), buff, sizeof buff); } @@ -826,7 +826,7 @@ static void test_fd_monitor() { // Items which will never receive data or be called back. item_maker_t item_never(fd_monitor_item_t::kNoTimeout); - item_maker_t item_hugetimeout(100000000llu * usec_per_msec); + item_maker_t item_hugetimeout(100000000LLU * usec_per_msec); // Item which should get no data, and time out. item_maker_t item0_timeout(16 * usec_per_msec); @@ -1616,7 +1616,8 @@ static void test_wchar2utf8(const wchar_t *src, size_t slen, const char *dst, si #endif if (dst) { - mem = (char *)malloc(dlen); + // We want to pass a valid pointer to wchar_to_utf8, so allocate at least one byte. + mem = (char *)malloc(dlen + 1); if (!mem) { err(L"w2u: %s: MALLOC FAILED", descr); return; @@ -2424,7 +2425,7 @@ struct pager_layout_testcase_t { text.push_back(p.character); } if (text != expected) { - std::fwprintf(stderr, L"width %zu got %zu<%ls>, expected %zu<%ls>\n", this->width, + std::fwprintf(stderr, L"width %d got %zu<%ls>, expected %zu<%ls>\n", this->width, text.length(), text.c_str(), expected.length(), expected.c_str()); for (size_t i = 0; i < std::max(text.length(), expected.length()); i++) { std::fwprintf(stderr, L"i %zu got <%lx> expected <%lx>\n", i, @@ -4107,7 +4108,7 @@ void history_tests_t::test_history_races() { history_t(L"race_test").clear(); pid_t children[RACE_COUNT]; - for (size_t i = 0; i < RACE_COUNT; i++) { + for (pid_t &child : children) { pid_t pid = fork(); if (!pid) { // Child process. @@ -4116,7 +4117,7 @@ void history_tests_t::test_history_races() { exit_without_destructors(0); } else { // Parent process. - children[i] = pid; + child = pid; } } @@ -4374,15 +4375,10 @@ void history_tests_t::test_history_formats() { } else { // The results are in the reverse order that they appear in the bash history file. // We don't expect whitespace to be elided (#4908: except for leading/trailing whitespace) - const wchar_t *expected[] = {L"/** # see issue 7407", - L"sleep 123", - L"a && echo valid construct", - L"final line", - L"echo supsup", - L"export XVAR='exported'", - L"history --help", - L"echo foo", - NULL}; + const wchar_t *expected[] = { + L"/** # see issue 7407", L"sleep 123", L"a && echo valid construct", + L"final line", L"echo supsup", L"export XVAR='exported'", + L"history --help", L"echo foo", NULL}; history_t &test_history = history_t::history_with_name(L"bash_import"); test_history.populate_from_bash(f); if (!history_equals(test_history, expected)) { @@ -4549,6 +4545,10 @@ static bool test_1_parse_ll2(const wcstring &src, wcstring *out_cmd, wcstring *o statement = tmp; } } + if (!statement) { + say(L"No decorated statement found in '%ls'", src.c_str()); + return false; + } // Return its decoration and command. *out_deco = statement->decoration();