From ef57d54f80774701eee8bdb45185fb33e5ced473 Mon Sep 17 00:00:00 2001 From: Ting-Yuan Huang Date: Wed, 19 Oct 2016 18:15:30 -0700 Subject: [PATCH] adb: fix undefined behavior system/core/adb/shell_service_protocol_test.cpp:94:14: warning: Null passed to a callee that requires a non-null 1st parameter !memcmp(data, protocol->data(), data_length)); C99 requires memcmp() to take non-null pointers. Bug: none Test: clang-tidy doesn't complain Change-Id: I77b8d9373fa257a070ffc7fd318231c2c9ea84c3 --- adb/shell_service_protocol_test.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/adb/shell_service_protocol_test.cpp b/adb/shell_service_protocol_test.cpp index a826035ab..b0fa3edbb 100644 --- a/adb/shell_service_protocol_test.cpp +++ b/adb/shell_service_protocol_test.cpp @@ -86,9 +86,10 @@ sig_t ShellProtocolTest::saved_sigpipe_handler_ = nullptr; namespace { -// Returns true if the packet contains the given values. +// Returns true if the packet contains the given values. `data` can't be null. bool PacketEquals(const ShellProtocol* protocol, ShellProtocol::Id id, const void* data, size_t data_length) { + // Note that passing memcmp null is bad, even if data_length is 0. return (protocol->id() == id && protocol->data_length() == data_length && !memcmp(data, protocol->data(), data_length)); @@ -130,7 +131,8 @@ TEST_F(ShellProtocolTest, ZeroLengthPacket) { ASSERT_TRUE(write_protocol_->Write(id, 0)); ASSERT_TRUE(read_protocol_->Read()); - ASSERT_TRUE(PacketEquals(read_protocol_, id, nullptr, 0)); + char buf[1]; + ASSERT_TRUE(PacketEquals(read_protocol_, id, buf, 0)); } // Tests exit code packets.