[libvirt] [PATCH] internal: Simplify STREQ_NULLABLE

Our STREQ_NULLABLE and STRNEQ_NULLABLE macros are too complicated. This was a result of some broken version of gcc. However, that is long gone and therefore we can simplify the macros. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/internal.h | 4 ++-- tests/virstringtest.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/internal.h b/src/internal.h index d8cc5adc3..334659d32 100644 --- a/src/internal.h +++ b/src/internal.h @@ -92,9 +92,9 @@ # define STRSKIP(a, b) (STRPREFIX(a, b) ? (a) + strlen(b) : NULL) # define STREQ_NULLABLE(a, b) \ - ((a) ? (b) && STREQ((a) ? (a) : "", (b) ? (b) : "") : !(b)) + ((a) ? (b) && STREQ((a), (b)) : !(b)) # define STRNEQ_NULLABLE(a, b) \ - ((a) ? !(b) || STRNEQ((a) ? (a) : "", (b) ? (b) : "") : !!(b)) + ((a) ? !(b) || STRNEQ((a), (b)) : !!(b)) # define NUL_TERMINATE(buf) do { (buf)[sizeof(buf)-1] = '\0'; } while (0) # define ARRAY_CARDINALITY(Array) (sizeof(Array) / sizeof(*(Array))) diff --git a/tests/virstringtest.c b/tests/virstringtest.c index 1d660b798..db1731f96 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -34,6 +34,53 @@ VIR_LOG_INIT("tests.stringtest"); +struct testStreqData { + const char *a; + const char *b; +}; + +static int testStreq(const void *args) +{ + const struct testStreqData *data = args; + int ret = -1; + bool equal = true; + bool streq_rv, strneq_rv; + size_t i; + + if ((size_t) data->a ^ (size_t) data->b) + equal = false; + if (data->a && data->b) { + for (i = 0; data->a[i] != '\0'; i++) { + if (data->b[i] == '\0' || + data->a[i] != data->b[i]) { + equal = false; + break; + } + } + } + + streq_rv = STREQ_NULLABLE(data->a, data->b); + strneq_rv = STRNEQ_NULLABLE(data->a, data->b); + + if (streq_rv != equal) { + virFilePrintf(stderr, + "STREQ not working correctly. Expected %d got %d", + (int) equal, (int) streq_rv); + goto cleanup; + } + + if (strneq_rv == equal) { + virFilePrintf(stderr, + "STRNEQ not working correctly. Expected %d got %d", + (int) equal, (int) strneq_rv); + goto cleanup; + } + + ret = 0; + cleanup: + return ret; +} + struct testSplitData { const char *string; const char *delim; @@ -651,6 +698,20 @@ mymain(void) { int ret = 0; +#define TEST_STREQ(aa, bb) \ + do { \ + struct testStreqData streqData = {.a = aa, .b = bb}; \ + if (virTestRun("Streq", testStreq, &streqData) < 0) \ + ret = -1; \ + } while (0) + + TEST_STREQ("hello", "world"); + TEST_STREQ(NULL, NULL); + TEST_STREQ(NULL, ""); + TEST_STREQ("", NULL); + TEST_STREQ("", ""); + TEST_STREQ("hello", "hello"); + #define TEST_SPLIT(str, del, max, toks) \ do { \ struct testSplitData splitData = { \ -- 2.11.0

On 12/09/2016 05:55 AM, Michal Privoznik wrote:
Our STREQ_NULLABLE and STRNEQ_NULLABLE macros are too complicated. This was a result of some broken version of gcc. However, that is long gone and therefore we can simplify the macros.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/internal.h | 4 ++-- tests/virstringtest.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-)
Going through older orphan patches... Seems reasonable - the change being removing the '(a) ? (a) : ""' and '(b) ? (b) : ""' from within the STR(N)EQ comparisons. Any chance at knowing which compiler version "fixed" the issue with strcmp and of course secondarily documenting or ensuring that version is available in the "minimal requirements" for build? ACK - for the code... Having that version is just extra just in case John

On 01/05/2017 02:51 PM, John Ferlan wrote:
On 12/09/2016 05:55 AM, Michal Privoznik wrote:
Our STREQ_NULLABLE and STRNEQ_NULLABLE macros are too complicated. This was a result of some broken version of gcc. However, that is long gone and therefore we can simplify the macros.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/internal.h | 4 ++-- tests/virstringtest.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-)
Going through older orphan patches...
Seems reasonable - the change being removing the '(a) ? (a) : ""' and '(b) ? (b) : ""' from within the STR(N)EQ comparisons.
Any chance at knowing which compiler version "fixed" the issue with strcmp and of course secondarily documenting or ensuring that version is available in the "minimal requirements" for build?
Eric states in the original commit that it is 4.7 what's broken. That's ancient history so I think even rhel6 is safe. Michal
participants (2)
-
John Ferlan
-
Michal Privoznik