Skip to content

Commit

Permalink
msgbuf: Ensure memmem() is correctly typed and visible where required
Browse files Browse the repository at this point in the history
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: ibm-openbmc#12
Fixes: 1523778 ("msgbuf: Add pldm_msgbuf_span_string_utf16()")
Change-Id: I9206f7616740790a89366762cce11d3045471b97
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
  • Loading branch information
amboar authored and ArchanaKakani committed Sep 6, 2024
1 parent 5eb11bf commit 9896762
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 13 deletions.
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ if get_option('tests').allowed()
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()
Expand Down
13 changes: 3 additions & 10 deletions src/msgbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1140,7 +1133,7 @@ pldm_msgbuf_span_string_utf16(struct pldm_msgbuf *ctx, void **cursor,
* 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) {
/*
Expand All @@ -1150,8 +1143,8 @@ pldm_msgbuf_span_string_utf16(struct pldm_msgbuf *ctx, void **cursor,
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) {
Expand Down
1 change: 0 additions & 1 deletion src/requester/instance-id.c
Original file line number Diff line number Diff line change
@@ -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>

Expand Down
1 change: 0 additions & 1 deletion src/transport/test.c
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 9896762

Please sign in to comment.