From b8863f6a03868c6817e9fc08c79dff8a6f95ed0b Mon Sep 17 00:00:00 2001 From: Avichal Rakesh Date: Thu, 12 Jan 2023 18:15:09 -0800 Subject: [PATCH] camera: Fix prefix length path for External Camera HAL The default External Camera HAL accidentally used `sizeof` on a `char *` to get the length of the string. This "worked" because the string in question "/dev/" has length of 5, while on a 32-bit system `char *` has a size of 4. This difference of 1 was handled a few lines later. However on 64-bit system `sizeof(char *)` returns 8 which breaks the logic later in code. As `sizeof` was being used as a proxy for strlen, this CL converts the uses C++'s std::char_traits::length to determine the string lengths at compile time. Bug: 265168485 Test: Manually tested that the calculated length is correct after the fix. Change-Id: I92481a2bcb74fdbbb483ef552168debf07b2d68a --- .../default/ExternalCameraProviderImpl_2_4.cpp | 17 +++++++++-------- .../default/ExternalCameraProviderImpl_2_7.cpp | 9 +++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/camera/provider/2.4/default/ExternalCameraProviderImpl_2_4.cpp b/camera/provider/2.4/default/ExternalCameraProviderImpl_2_4.cpp index 4fc743748a..04db7f33ee 100644 --- a/camera/provider/2.4/default/ExternalCameraProviderImpl_2_4.cpp +++ b/camera/provider/2.4/default/ExternalCameraProviderImpl_2_4.cpp @@ -18,15 +18,16 @@ //#define LOG_NDEBUG 0 #include -#include -#include +#include #include #include -#include -#include "ExternalCameraProviderImpl_2_4.h" +#include +#include +#include #include "ExternalCameraDevice_3_4.h" #include "ExternalCameraDevice_3_5.h" #include "ExternalCameraDevice_3_6.h" +#include "ExternalCameraProviderImpl_2_4.h" namespace android { namespace hardware { @@ -41,10 +42,10 @@ namespace { // "device@/external/" const std::regex kDeviceNameRE("device@([0-9]+\\.[0-9]+)/external/(.+)"); const int kMaxDevicePathLen = 256; -const char* kDevicePath = "/dev/"; -constexpr char kPrefix[] = "video"; -constexpr int kPrefixLen = sizeof(kPrefix) - 1; -constexpr int kDevicePrefixLen = sizeof(kDevicePath) + kPrefixLen + 1; +constexpr const char* kDevicePath = "/dev/"; +constexpr const char* kPrefix = "video"; +constexpr int kPrefixLen = std::char_traits::length(kPrefix); +constexpr int kDevicePrefixLen = std::char_traits::length(kDevicePath) + kPrefixLen; bool matchDeviceName(int cameraIdOffset, const hidl_string& deviceName, std::string* deviceVersion, diff --git a/camera/provider/2.7/default/ExternalCameraProviderImpl_2_7.cpp b/camera/provider/2.7/default/ExternalCameraProviderImpl_2_7.cpp index b63e3bb88b..62ce074404 100644 --- a/camera/provider/2.7/default/ExternalCameraProviderImpl_2_7.cpp +++ b/camera/provider/2.7/default/ExternalCameraProviderImpl_2_7.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "ExternalCameraDevice_3_4.h" #include "ExternalCameraDevice_3_5.h" #include "ExternalCameraDevice_3_6.h" @@ -39,10 +40,10 @@ namespace { // "device@/external/" const std::regex kDeviceNameRE("device@([0-9]+\\.[0-9]+)/external/(.+)"); const int kMaxDevicePathLen = 256; -const char* kDevicePath = "/dev/"; -constexpr char kPrefix[] = "video"; -constexpr int kPrefixLen = sizeof(kPrefix) - 1; -constexpr int kDevicePrefixLen = sizeof(kDevicePath) + kPrefixLen + 1; +constexpr const char* kDevicePath = "/dev/"; +constexpr const char* kPrefix = "video"; +constexpr int kPrefixLen = std::char_traits::length(kPrefix); +constexpr int kDevicePrefixLen = std::char_traits::length(kDevicePath) + kPrefixLen; bool matchDeviceName(int cameraIdOffset, const hidl_string& deviceName, std::string* deviceVersion, std::string* cameraDevicePath) {