From 9d3a93991395b1c8ad3f1a9dfd62274dafba739a Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Fri, 10 Jan 2025 15:00:04 +0800 Subject: [PATCH] compile: avoid min macro pollution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The generic min macro in util.h is too common and can conflict with other libraries. use ccan/minmax in the source files to avoid pollution. the ccan/minmax will check the type of the input parameters, so cast the `u8* - u8*` to `int` to avoid the type mismatch. Before renaming the macro, the cpp's std::min will fail to compile. ``` ./test/cpp.cc: In function β€˜int min_compile_test()’: ../src/nvme/util.h:563:19: error: expected unqualified-id before β€˜(’ token 563 | #define min(x, y) ((x) > (y) ? (y) : (x)) ``` Signed-off-by: Jian Zhang --- src/nvme/ioctl.c | 3 ++- src/nvme/mi.c | 3 ++- src/nvme/util.c | 1 + src/nvme/util.h | 2 -- test/cpp.cc | 8 ++++++++ 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/nvme/ioctl.c b/src/nvme/ioctl.c index 3ae6ecd8..078faa3b 100644 --- a/src/nvme/ioctl.c +++ b/src/nvme/ioctl.c @@ -23,6 +23,7 @@ #include #include +#include #include #include "ioctl.h" @@ -503,7 +504,7 @@ static int read_ana_chunk(int fd, enum nvme_log_ana_lsp lsp, bool rae, } while (*read < to_read) { - __u32 len = min(log_end - *read, NVME_LOG_PAGE_PDU_SIZE); + __u32 len = min_t(int, log_end - *read, NVME_LOG_PAGE_PDU_SIZE); int ret; ret = nvme_get_log_ana(fd, lsp, rae, *read - log, len, *read); diff --git a/src/nvme/mi.c b/src/nvme/mi.c index e9f39a84..71dd628d 100644 --- a/src/nvme/mi.c +++ b/src/nvme/mi.c @@ -14,6 +14,7 @@ #include #include +#include #include #include "log.h" @@ -1021,7 +1022,7 @@ static int read_ana_chunk(nvme_mi_ctrl_t ctrl, enum nvme_log_ana_lsp lsp, bool r } while (*read < to_read) { - __u32 len = min(log_end - *read, NVME_LOG_PAGE_PDU_SIZE); + __u32 len = min_t(int, log_end - *read, NVME_LOG_PAGE_PDU_SIZE); int ret; ret = nvme_mi_admin_get_log_ana(ctrl, lsp, rae, diff --git a/src/nvme/util.c b/src/nvme/util.c index 1af358a3..09d5a347 100644 --- a/src/nvme/util.c +++ b/src/nvme/util.c @@ -22,6 +22,7 @@ #include #include +#include #include #include "cleanup.h" diff --git a/src/nvme/util.h b/src/nvme/util.h index 364ca0f2..b898b419 100644 --- a/src/nvme/util.h +++ b/src/nvme/util.h @@ -560,8 +560,6 @@ char *kv_keymatch(const char *kv, const char *key); */ char *startswith(const char *s, const char *prefix); -#define min(x, y) ((x) > (y) ? (y) : (x)) - #define __round_mask(val, mult) ((__typeof__(val))((mult)-1)) /** diff --git a/test/cpp.cc b/test/cpp.cc index 3d0a7d25..6c24014e 100644 --- a/test/cpp.cc +++ b/test/cpp.cc @@ -6,9 +6,15 @@ * Authors: Keith Busch */ +#include #include #include +static int min_compile_test() +{ + return std::min(1, 2); +} + int main() { nvme_root_t r; @@ -62,5 +68,7 @@ int main() std::cout << "\n"; nvme_free_tree(r); + min_compile_test(); + return 0; }