diff options
author | Gabriel Biren <gbiren@google.com> | 2022-03-31 00:08:02 +0000 |
---|---|---|
committer | Gabriel Biren <gbiren@google.com> | 2022-03-31 00:10:29 +0000 |
commit | 11d4ab8f615496869c7f6455f103de6298ac127d (patch) | |
tree | 60f700766f28a6ee1b3740bfb2fd9a8bf27d6e13 | |
parent | 89345a4d7da9c6a7b7ae43b4a2aedd3c0354ffff (diff) |
Replace instances of strncpy and sprintf in the wifi
vendor HAL with strlcpy and snprintf.
Bug: 218897284
Test: m
Change-Id: I0a62d1c5043cabcf6a513b8b082ad1d7a1ea451c
-rw-r--r-- | wifi/1.6/default/hidl_struct_util.cpp | 8 | ||||
-rw-r--r-- | wifi/1.6/default/wifi_chip.cpp | 26 |
2 files changed, 18 insertions, 16 deletions
diff --git a/wifi/1.6/default/hidl_struct_util.cpp b/wifi/1.6/default/hidl_struct_util.cpp index 45459e2cd3..2112b260f3 100644 --- a/wifi/1.6/default/hidl_struct_util.cpp +++ b/wifi/1.6/default/hidl_struct_util.cpp @@ -1881,7 +1881,7 @@ bool convertHidlNanDataPathInitiatorRequestToLegacy( "ifaceName too long"; return false; } - strncpy(legacy_request->ndp_iface, hidl_request.ifaceName.c_str(), IFNAMSIZ + 1); + strlcpy(legacy_request->ndp_iface, hidl_request.ifaceName.c_str(), IFNAMSIZ + 1); legacy_request->ndp_cfg.security_cfg = (hidl_request.securityConfig.securityType != NanDataPathSecurityType::OPEN) ? legacy_hal::NAN_DP_CONFIG_SECURITY @@ -1958,7 +1958,7 @@ bool convertHidlNanDataPathInitiatorRequest_1_6ToLegacy( "ifaceName too long"; return false; } - strncpy(legacy_request->ndp_iface, hidl_request.ifaceName.c_str(), IFNAMSIZ + 1); + strlcpy(legacy_request->ndp_iface, hidl_request.ifaceName.c_str(), IFNAMSIZ + 1); legacy_request->ndp_cfg.security_cfg = (hidl_request.securityConfig.securityType != NanDataPathSecurityType::OPEN) ? legacy_hal::NAN_DP_CONFIG_SECURITY @@ -2039,7 +2039,7 @@ bool convertHidlNanDataPathIndicationResponseToLegacy( "ifaceName too long"; return false; } - strncpy(legacy_request->ndp_iface, hidl_request.ifaceName.c_str(), IFNAMSIZ + 1); + strlcpy(legacy_request->ndp_iface, hidl_request.ifaceName.c_str(), IFNAMSIZ + 1); legacy_request->ndp_cfg.security_cfg = (hidl_request.securityConfig.securityType != NanDataPathSecurityType::OPEN) ? legacy_hal::NAN_DP_CONFIG_SECURITY @@ -2114,7 +2114,7 @@ bool convertHidlNanDataPathIndicationResponse_1_6ToLegacy( "ifaceName too long"; return false; } - strncpy(legacy_request->ndp_iface, hidl_request.ifaceName.c_str(), IFNAMSIZ + 1); + strlcpy(legacy_request->ndp_iface, hidl_request.ifaceName.c_str(), IFNAMSIZ + 1); legacy_request->ndp_cfg.security_cfg = (hidl_request.securityConfig.securityType != NanDataPathSecurityType::OPEN) ? legacy_hal::NAN_DP_CONFIG_SECURITY diff --git a/wifi/1.6/default/wifi_chip.cpp b/wifi/1.6/default/wifi_chip.cpp index 6cc12358e5..7f0e97df7f 100644 --- a/wifi/1.6/default/wifi_chip.cpp +++ b/wifi/1.6/default/wifi_chip.cpp @@ -131,7 +131,7 @@ std::string getPredefinedP2pIfaceName() { if (strncmp(buffer.data(), P2P_MGMT_DEVICE_PREFIX, strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { /* Get the p2p parent interface name from p2p device interface name set * in property */ - strncpy(p2pParentIfname, buffer.data() + strlen(P2P_MGMT_DEVICE_PREFIX), + strlcpy(p2pParentIfname, buffer.data() + strlen(P2P_MGMT_DEVICE_PREFIX), strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX)); if (property_get(kActiveWlanIfaceNameProperty, primaryIfaceName.data(), nullptr) == 0) { return buffer.data(); @@ -217,14 +217,15 @@ bool removeOldFilesInternal() { // Helper function for |cpioArchiveFilesInDir| bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name, size_t file_name_len) { - std::array<char, 32 * 1024> read_buf; - ssize_t llen = - sprintf(read_buf.data(), "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X", - kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid, st.st_gid, - static_cast<int>(st.st_nlink), static_cast<int>(st.st_mtime), - static_cast<int>(st.st_size), major(st.st_dev), minor(st.st_dev), - major(st.st_rdev), minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0); - if (write(out_fd, read_buf.data(), llen) == -1) { + const int buf_size = 32 * 1024; + std::array<char, buf_size> read_buf; + ssize_t llen = snprintf( + read_buf.data(), buf_size, "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X", + kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid, st.st_gid, + static_cast<int>(st.st_nlink), static_cast<int>(st.st_mtime), + static_cast<int>(st.st_size), major(st.st_dev), minor(st.st_dev), major(st.st_rdev), + minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0); + if (write(out_fd, read_buf.data(), llen < buf_size ? llen : buf_size - 1) == -1) { PLOG(ERROR) << "Error writing cpio header to file " << file_name; return false; } @@ -282,10 +283,11 @@ size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) { // Helper function for |cpioArchiveFilesInDir| bool cpioWriteFileTrailer(int out_fd) { - std::array<char, 4096> read_buf; + const int buf_size = 4096; + std::array<char, buf_size> read_buf; read_buf.fill(0); - if (write(out_fd, read_buf.data(), - sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0) + 4) == -1) { + ssize_t llen = snprintf(read_buf.data(), 4096, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0); + if (write(out_fd, read_buf.data(), (llen < buf_size ? llen : buf_size - 1) + 4) == -1) { PLOG(ERROR) << "Error writing trailing bytes"; return false; } |