Enable clang-tidy for libsysutils.

Enable the same warnings used elsewhere in the tree, and fix the
two warnings it found (two safe uses of strcpy and one use of
atoi).

Test: builds, boots
Test: atest libsysutils_tests
Test: atest --test-mapping system/netd
Change-Id: I8e5d04da20cc4127439ad9fbda0e4e64f0e671d3
This commit is contained in:
Lorenzo Colitti 2019-04-10 23:04:41 +09:00
parent 2ddb8df2ef
commit d0e4938dfe
2 changed files with 27 additions and 10 deletions

View file

@ -26,6 +26,19 @@ cc_library {
],
export_include_dirs: ["include"],
tidy: true,
tidy_checks: [
"-*",
"cert-*",
"clang-analyzer-security*",
"android-*",
],
tidy_checks_as_errors: [
"cert-*",
"clang-analyzer-security*",
"android-*",
],
}
cc_test {

View file

@ -39,9 +39,12 @@
const int LOCAL_QLOG_NL_EVENT = 112;
const int LOCAL_NFLOG_PACKET = NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET;
#include <android-base/parseint.h>
#include <log/log.h>
#include <sysutils/NetlinkEvent.h>
using android::base::ParseInt;
NetlinkEvent::NetlinkEvent() {
mAction = Action::kUnknown;
memset(mParams, 0, sizeof(mParams));
@ -301,8 +304,9 @@ bool NetlinkEvent::parseNfPacketMessage(struct nlmsghdr *nh) {
raw = (char*)nlAttrData(payload);
}
char* hex = (char*) calloc(1, 5 + (len * 2));
strcpy(hex, "HEX=");
size_t hexSize = 5 + (len * 2);
char* hex = (char*)calloc(1, hexSize);
strlcpy(hex, "HEX=", hexSize);
for (int i = 0; i < len; i++) {
hex[4 + (i * 2)] = "0123456789abcdef"[(raw[i] >> 4) & 0xf];
hex[5 + (i * 2)] = "0123456789abcdef"[raw[i] & 0xf];
@ -474,23 +478,20 @@ bool NetlinkEvent::parseNdUserOptMessage(const struct nlmsghdr *nh) {
struct nd_opt_rdnss *rndss_opt = (struct nd_opt_rdnss *) opthdr;
const uint32_t lifetime = ntohl(rndss_opt->nd_opt_rdnss_lifetime);
// Construct "SERVERS=<comma-separated string of DNS addresses>".
static const char kServerTag[] = "SERVERS=";
static const size_t kTagLength = strlen(kServerTag);
// Construct a comma-separated string of DNS addresses.
// Reserve sufficient space for an IPv6 link-local address: all but the
// last address are followed by ','; the last is followed by '\0'.
static const size_t kMaxSingleAddressLength =
INET6_ADDRSTRLEN + strlen("%") + IFNAMSIZ + strlen(",");
const size_t bufsize = kTagLength + numaddrs * kMaxSingleAddressLength;
const size_t bufsize = numaddrs * kMaxSingleAddressLength;
char *buf = (char *) malloc(bufsize);
if (!buf) {
SLOGE("RDNSS option: out of memory\n");
return false;
}
strcpy(buf, kServerTag);
size_t pos = kTagLength;
struct in6_addr *addrs = (struct in6_addr *) (rndss_opt + 1);
size_t pos = 0;
for (int i = 0; i < numaddrs; i++) {
if (i > 0) {
buf[pos++] = ',';
@ -508,7 +509,8 @@ bool NetlinkEvent::parseNdUserOptMessage(const struct nlmsghdr *nh) {
mSubsystem = strdup("net");
asprintf(&mParams[0], "INTERFACE=%s", ifname);
asprintf(&mParams[1], "LIFETIME=%u", lifetime);
mParams[2] = buf;
asprintf(&mParams[2], "SERVERS=%s", buf);
free(buf);
} else if (opthdr->nd_opt_type == ND_OPT_DNSSL) {
// TODO: support DNSSL.
} else {
@ -634,7 +636,9 @@ bool NetlinkEvent::parseAsciiNetlinkMessage(char *buffer, int size) {
else if (!strcmp(a, "change"))
mAction = Action::kChange;
} else if ((a = HAS_CONST_PREFIX(s, end, "SEQNUM=")) != nullptr) {
mSeq = atoi(a);
if (!ParseInt(a, &mSeq)) {
SLOGE("NetlinkEvent::parseAsciiNetlinkMessage: failed to parse SEQNUM=%s", a);
}
} else if ((a = HAS_CONST_PREFIX(s, end, "SUBSYSTEM=")) != nullptr) {
mSubsystem = strdup(a);
} else if (param_idx < NL_PARAMS_MAX) {