From 99938c3a36b592b38e0f75d259ad815874abb8c1 Mon Sep 17 00:00:00 2001 From: Rahul Frias Date: Fri, 27 Nov 2020 03:38:44 -0800 Subject: [PATCH] Fix Format String Signedness [ Merge of http://go/wvgerrit/110165 ] The conditional compilation for flagging the CDM and ODKiTEE logging functions as printf-like was guarded by a check on "__gnuc__" or "__clang__". However, GCC doesn't actually define "__gnuc__", it defines "__GNUC__", all caps. Fixing this causes GCC to find a slew of format-string errors that Clang was accepting. This patch fixes the capitalization and the uncovered errors, most of which fall into one of a few categories: 1) The format string and variable had different signedness. For these, the format strings are updated to match the variables. 2) The variable was an array index that was not of size_t. For these, the variables have been updated to be size_t and the format strings have been updated to use %zu. A few index variables that weren't actually used in format strings are also fixed to be size_t. 3) The code assumed the signedness of the internal representation of an integer literal, enum constant, or enum variable. For these, I either cast the input to a known type so that the format string is valid regardless of internal representation or I switched to a hexadecimal format string. The latter case is more useful on ODKiTEE enums where the literal value is in the code as a 32-bit hexadecimal value anyway. This patch also adds missing integer casts to the enum literals in the ODKiTEE logging header. (These are required for pedantic C99 compatibility when using literal values that do not fit into a 16-bit integer.) Bug: 173460694 Test: jenkins/odkitee_ta Test: build.py x86-64 Change-Id: I244972639a5a6ea0de157eb67e1e0dfa9787ec32 --- libwvdrmengine/cdm/core/src/crypto_session.cpp | 2 +- libwvdrmengine/cdm/core/src/initialization_data.cpp | 5 +++-- libwvdrmengine/cdm/util/include/log.h | 2 +- libwvdrmengine/cdm/util/src/string_conversions.cpp | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/libwvdrmengine/cdm/core/src/crypto_session.cpp b/libwvdrmengine/cdm/core/src/crypto_session.cpp index 21ad78a7..8f21a408 100644 --- a/libwvdrmengine/cdm/core/src/crypto_session.cpp +++ b/libwvdrmengine/cdm/core/src/crypto_session.cpp @@ -127,7 +127,7 @@ void AdvanceDestBuffer(OEMCrypto_DestBufferDesc* dest_buffer, size_t bytes) { return; } LOGE("Unrecognized OEMCryptoBufferType %u - doing nothing", - dest_buffer->type); + static_cast(dest_buffer->type)); } bool GetGenericSigningAlgorithm(CdmSigningAlgorithm algorithm, diff --git a/libwvdrmengine/cdm/core/src/initialization_data.cpp b/libwvdrmengine/cdm/core/src/initialization_data.cpp index 1ff2b082..fbacf1a8 100644 --- a/libwvdrmengine/cdm/core/src/initialization_data.cpp +++ b/libwvdrmengine/cdm/core/src/initialization_data.cpp @@ -700,8 +700,9 @@ void InitializationData::DumpToLogs() const { } if (pssh.has_protection_scheme()) { uint32_t scheme = pssh.protection_scheme(); - LOGD("InitData: Protection Scheme: %c%c%c%c", (scheme >> 24) & 0xFF, - (scheme >> 16) & 0xFF, (scheme >> 8) & 0xFF, (scheme >> 0) & 0xFF); + LOGD("InitData: Protection Scheme: %c%c%c%c", + static_cast(scheme >> 24), static_cast(scheme >> 16), + static_cast(scheme >> 8), static_cast(scheme >> 0)); } switch (pssh.type()) { case video_widevine::WidevinePsshData_Type_SINGLE: diff --git a/libwvdrmengine/cdm/util/include/log.h b/libwvdrmengine/cdm/util/include/log.h index 3a95662c..5d945e4e 100644 --- a/libwvdrmengine/cdm/util/include/log.h +++ b/libwvdrmengine/cdm/util/include/log.h @@ -35,7 +35,7 @@ CORE_UTIL_EXPORT void InitLogging(); // Only enable format specifier warnings on LP64 systems. There is // no easy portable method to handle format specifiers for int64_t. -#if (defined(__gnuc__) || defined(__clang__)) && defined(__LP64__) +#if (defined(__GNUC__) || defined(__clang__)) && defined(__LP64__) [[gnu::format(printf, 5, 6)]] CORE_UTIL_EXPORT void Log(const char* file, const char* function, int line, diff --git a/libwvdrmengine/cdm/util/src/string_conversions.cpp b/libwvdrmengine/cdm/util/src/string_conversions.cpp index 3bdb68ae..cd93dbbd 100644 --- a/libwvdrmengine/cdm/util/src/string_conversions.cpp +++ b/libwvdrmengine/cdm/util/src/string_conversions.cpp @@ -56,12 +56,12 @@ std::vector a2b_hex(const std::string& byte) { return array; } - for (unsigned int i = 0; i < count / 2; ++i) { + for (size_t i = 0; i < count / 2; ++i) { unsigned char msb = 0; // most significant 4 bits unsigned char lsb = 0; // least significant 4 bits if (!DecodeHexChar(byte[i * 2], &msb) || !DecodeHexChar(byte[i * 2 + 1], &lsb)) { - LOGE("Invalid hex value %c%c at index %d", byte[i * 2], byte[i * 2 + 1], + LOGE("Invalid hex value %c%c at index %zu", byte[i * 2], byte[i * 2 + 1], i); return array; }