msgbuf: Ensure memmem() is correctly typed and visible where required
To ensure memmem() is visible _GNU_SOURCE needs to be defined early,
at least before any system headers are included. Define it in the build
flags as clang-tidy will re-order includes based on vibes rather than
dependencies. Finally, clean up the remaining compiler warnings by
dropping the unnecessary casts.
Note that _GNU_SOURCE implies _DEFAULT_SOURCE, so we drop the latter:
> Since glibc 2.19, defining _GNU_SOURCE also has the effect of
> implicitly defining _DEFAULT_SOURCE. Before glibc 2.20, defining
> _GNU_SOURCE also had the effect of implicitly defining _BSD_SOURCE
> and _SVID_SOURCE.
https://www.man7.org/linux/man-pages/man7/feature_test_macros.7.html
Fixes: #12
Fixes: 1523778d2739 ("msgbuf: Add pldm_msgbuf_span_string_utf16()")
Change-Id: I9206f7616740790a89366762cce11d3045471b97
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/meson.build b/meson.build
index 0a2cc04..fff2c0d 100644
--- a/meson.build
+++ b/meson.build
@@ -17,7 +17,8 @@
add_languages('cpp')
endif
-add_project_arguments('-D_DEFAULT_SOURCE', language: ['c'])
+# For memmem() in src/msgbuf.h
+add_project_arguments('-D_GNU_SOURCE', language: ['c'])
compiler = meson.get_compiler('c')
conf = configuration_data()
diff --git a/src/msgbuf.h b/src/msgbuf.h
index 1d06a75..c2c02fe 100644
--- a/src/msgbuf.h
+++ b/src/msgbuf.h
@@ -2,13 +2,6 @@
#ifndef PLDM_MSGBUF_H
#define PLDM_MSGBUF_H
-// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
-#ifndef _GNU_SOURCE
-/* For memmem(3) */
-#define _GNU_SOURCE
-#endif
-// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
-
/*
* Historically, many of the structs exposed in libpldm's public headers are
* defined with __attribute__((packed)). This is unfortunate: it gives the
@@ -1140,7 +1133,7 @@
* not form a UTF16 NUL _code-point_ due to alignment with respect to the
* start of the string
*/
- end = (char *)ctx->cursor;
+ end = ctx->cursor;
do {
if (end != ctx->cursor) {
/*
@@ -1150,8 +1143,8 @@
end = (char *)end + 1;
}
measured = (char *)end - (char *)ctx->cursor;
- end = (char *)memmem(end, ctx->remaining - measured, &term,
- sizeof(term));
+ end = memmem(end, ctx->remaining - measured, &term,
+ sizeof(term));
} while (end && ((uintptr_t)end & 1) != ((uintptr_t)ctx->cursor & 1));
if (!end) {
diff --git a/src/requester/instance-id.c b/src/requester/instance-id.c
index a2700af..d891a48 100644
--- a/src/requester/instance-id.c
+++ b/src/requester/instance-id.c
@@ -1,6 +1,5 @@
/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
-#define _GNU_SOURCE
#include <libpldm/instance-id.h>
#include <libpldm/pldm.h>
diff --git a/src/transport/test.c b/src/transport/test.c
index 2a929cc..1260031 100644
--- a/src/transport/test.c
+++ b/src/transport/test.c
@@ -1,6 +1,5 @@
/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
/* NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
-#define _GNU_SOURCE
#include "array.h"
#include "container-of.h"
#include "transport.h"