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");
This commit is contained in:
Johannes Altmanninger 2020-12-25 09:25:08 +01:00
parent a205225b4e
commit 8fc9b9d61b

View File

@ -816,7 +816,7 @@ static void test_fd_monitor() {
item_maker_t(const item_maker_t &) = delete; item_maker_t(const item_maker_t &) = delete;
// Write 42 bytes to our write end. // Write 42 bytes to our write end.
void write42() { void write42() const {
char buff[42] = {0}; char buff[42] = {0};
(void)write_loop(writer.fd(), buff, sizeof buff); (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. // Items which will never receive data or be called back.
item_maker_t item_never(fd_monitor_item_t::kNoTimeout); 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 which should get no data, and time out.
item_maker_t item0_timeout(16 * usec_per_msec); 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 #endif
if (dst) { 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) { if (!mem) {
err(L"w2u: %s: MALLOC FAILED", descr); err(L"w2u: %s: MALLOC FAILED", descr);
return; return;
@ -2424,7 +2425,7 @@ struct pager_layout_testcase_t {
text.push_back(p.character); text.push_back(p.character);
} }
if (text != expected) { 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()); text.length(), text.c_str(), expected.length(), expected.c_str());
for (size_t i = 0; i < std::max(text.length(), expected.length()); i++) { 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, 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(); history_t(L"race_test").clear();
pid_t children[RACE_COUNT]; pid_t children[RACE_COUNT];
for (size_t i = 0; i < RACE_COUNT; i++) { for (pid_t &child : children) {
pid_t pid = fork(); pid_t pid = fork();
if (!pid) { if (!pid) {
// Child process. // Child process.
@ -4116,7 +4117,7 @@ void history_tests_t::test_history_races() {
exit_without_destructors(0); exit_without_destructors(0);
} else { } else {
// Parent process. // Parent process.
children[i] = pid; child = pid;
} }
} }
@ -4374,15 +4375,10 @@ void history_tests_t::test_history_formats() {
} else { } else {
// The results are in the reverse order that they appear in the bash history file. // 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) // We don't expect whitespace to be elided (#4908: except for leading/trailing whitespace)
const wchar_t *expected[] = {L"/** # see issue 7407", const wchar_t *expected[] = {
L"sleep 123", L"/** # see issue 7407", L"sleep 123", L"a && echo valid construct",
L"a && echo valid construct", L"final line", L"echo supsup", L"export XVAR='exported'",
L"final line", L"history --help", L"echo foo", NULL};
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"); history_t &test_history = history_t::history_with_name(L"bash_import");
test_history.populate_from_bash(f); test_history.populate_from_bash(f);
if (!history_equals(test_history, expected)) { 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; statement = tmp;
} }
} }
if (!statement) {
say(L"No decorated statement found in '%ls'", src.c_str());
return false;
}
// Return its decoration and command. // Return its decoration and command.
*out_deco = statement->decoration(); *out_deco = statement->decoration();