From c4640e8d5deddd382f0344afafedb4a7a895a502 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Sun, 28 Nov 2021 11:46:32 +0800 Subject: [PATCH 1/6] Fix clang `-Wabsolute-value` warning --- librz/util/hex.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/librz/util/hex.c b/librz/util/hex.c index bd6926981c9..a6fb58a7ae0 100644 --- a/librz/util/hex.c +++ b/librz/util/hex.c @@ -407,7 +407,7 @@ RZ_API void rz_hex_ut2st_str(const ut64 in, RZ_INOUT char *out, const int len) { RZ_LOG_FATAL("Output buffer too small for 64bit value.\n"); } char tmp[20]; - sprintf(tmp, "-0x%x", abs((st64)in)); + sprintf(tmp, "-0x%" PFMT64x, llabs((st64)in)); memcpy(out, tmp, 20); return; } From 374836f8160f5e79aff7f48374603ff3186db7c4 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Sun, 28 Nov 2021 11:49:51 +0800 Subject: [PATCH 2/6] Trigger linux clang build on `clang` in branch name --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1527d6a5d00..bdf2658b080 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,7 +69,7 @@ jobs: compiler: clang run_tests: true meson_options: -Dbuildtype=release - enabled: ${{ github.event_name != 'pull_request' && needs.changes.outputs.edited == 'true' }} + enabled: ${{ (github.event_name != 'pull_request' || contains(github.head_ref, 'clang')) && needs.changes.outputs.edited == 'true' }} timeout: 45 allow_failure: false - name: linux-meson-gcc-tests From 4664fcc15a53f18ef1c14198c798dff75f76ddb3 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Sun, 28 Nov 2021 13:26:42 +0800 Subject: [PATCH 3/6] Take two's complement of `(ut32)in` instead --- librz/util/hex.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/librz/util/hex.c b/librz/util/hex.c index a6fb58a7ae0..ef5b8914d84 100644 --- a/librz/util/hex.c +++ b/librz/util/hex.c @@ -407,7 +407,7 @@ RZ_API void rz_hex_ut2st_str(const ut64 in, RZ_INOUT char *out, const int len) { RZ_LOG_FATAL("Output buffer too small for 64bit value.\n"); } char tmp[20]; - sprintf(tmp, "-0x%" PFMT64x, llabs((st64)in)); + sprintf(tmp, "-0x%" PFMT32x, ~(ut32)in + 1); memcpy(out, tmp, 20); return; } From b87124a91f877e043c563beb91603fd8798b7c06 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Sun, 28 Nov 2021 13:52:02 +0800 Subject: [PATCH 4/6] Clarify function of `rz_hex_ut2st_str()` --- librz/util/hex.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/librz/util/hex.c b/librz/util/hex.c index ef5b8914d84..3059a25e184 100644 --- a/librz/util/hex.c +++ b/librz/util/hex.c @@ -394,8 +394,8 @@ RZ_API int rz_hex_bin2str(const ut8 *in, int len, char *out) { } /** - * \brief Takes an unsigned integer and returns the signed integer in hex format as string. - * E.g.: 0xffffffffffffffff -> "-0x1" + * \brief Takes an unsigned 32bit integer with MSB set to 1 and returns the signed integer in hex format as string. + * E.g.: 0xffffffff -> "-0x1" * * \param in The integer to convert to the signed string. * \param out The buffer to write the signed hex string to. @@ -403,12 +403,12 @@ RZ_API int rz_hex_bin2str(const ut8 *in, int len, char *out) { * \return char* The signed integer as hex string. */ RZ_API void rz_hex_ut2st_str(const ut64 in, RZ_INOUT char *out, const int len) { - if (len < 20) { - RZ_LOG_FATAL("Output buffer too small for 64bit value.\n"); + char tmp[12]; + if (len < RZ_ARRAY_SIZE(tmp)) { + RZ_LOG_FATAL("Output buffer too small for negative 32bit value.\n"); } - char tmp[20]; - sprintf(tmp, "-0x%" PFMT32x, ~(ut32)in + 1); - memcpy(out, tmp, 20); + snprintf(tmp, RZ_ARRAY_SIZE(tmp), "-0x%" PFMT32x, ~(ut32)in + 1); + memcpy(out, tmp, RZ_ARRAY_SIZE(tmp)); return; } From e5078afb5d0a03e754cac2075d1904a73c510165 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Sun, 28 Nov 2021 13:57:24 +0800 Subject: [PATCH 5/6] Use `sizeof(tmp)` instead --- librz/util/hex.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/librz/util/hex.c b/librz/util/hex.c index 3059a25e184..50b3404fb8a 100644 --- a/librz/util/hex.c +++ b/librz/util/hex.c @@ -404,11 +404,11 @@ RZ_API int rz_hex_bin2str(const ut8 *in, int len, char *out) { */ RZ_API void rz_hex_ut2st_str(const ut64 in, RZ_INOUT char *out, const int len) { char tmp[12]; - if (len < RZ_ARRAY_SIZE(tmp)) { + if (len < sizeof(tmp)) { RZ_LOG_FATAL("Output buffer too small for negative 32bit value.\n"); } - snprintf(tmp, RZ_ARRAY_SIZE(tmp), "-0x%" PFMT32x, ~(ut32)in + 1); - memcpy(out, tmp, RZ_ARRAY_SIZE(tmp)); + snprintf(tmp, sizeof(tmp), "-0x%" PFMT32x, ~(ut32)in + 1); + memcpy(out, tmp, sizeof(tmp)); return; } From 24b28c1bd31226ad4c53302dcc56c3cb5527de11 Mon Sep 17 00:00:00 2001 From: Khairul Azhar Kasmiran Date: Sun, 28 Nov 2021 17:08:16 +0800 Subject: [PATCH 6/6] `rz_hex_ut2st_str()` accepts `ut32` instead --- librz/include/rz_util/rz_hex.h | 2 +- librz/util/hex.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/librz/include/rz_util/rz_hex.h b/librz/include/rz_util/rz_hex.h index b8c514588f4..faca386cc48 100644 --- a/librz/include/rz_util/rz_hex.h +++ b/librz/include/rz_util/rz_hex.h @@ -9,7 +9,7 @@ RZ_API int rz_hex_pair2bin(const char *arg); RZ_API int rz_hex_str2binmask(const char *in, ut8 *out, ut8 *mask); RZ_API int rz_hex_str2bin(const char *in, ut8 *out); RZ_API int rz_hex_bin2str(const ut8 *in, int len, char *out); -RZ_API void rz_hex_ut2st_str(const ut64 in, RZ_INOUT char *out, const int len); +RZ_API void rz_hex_ut2st_str(const ut32 in, RZ_INOUT char *out, const int len); RZ_API char *rz_hex_bin2strdup(const ut8 *in, int len); RZ_API bool rz_hex_to_byte(ut8 *val, ut8 c); RZ_API int rz_hex_str_is_valid(const char *s); diff --git a/librz/util/hex.c b/librz/util/hex.c index 50b3404fb8a..1da7df7e040 100644 --- a/librz/util/hex.c +++ b/librz/util/hex.c @@ -402,12 +402,12 @@ RZ_API int rz_hex_bin2str(const ut8 *in, int len, char *out) { * \param len Length of the out buffer. * \return char* The signed integer as hex string. */ -RZ_API void rz_hex_ut2st_str(const ut64 in, RZ_INOUT char *out, const int len) { +RZ_API void rz_hex_ut2st_str(const ut32 in, RZ_INOUT char *out, const int len) { char tmp[12]; if (len < sizeof(tmp)) { RZ_LOG_FATAL("Output buffer too small for negative 32bit value.\n"); } - snprintf(tmp, sizeof(tmp), "-0x%" PFMT32x, ~(ut32)in + 1); + snprintf(tmp, sizeof(tmp), "-0x%" PFMT32x, ~in + 1); memcpy(out, tmp, sizeof(tmp)); return; }