Patrick Williams | 691668f | 2023-11-01 08:19:10 -0500 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 2 | #ifndef PLDM_MSGBUF_H |
| 3 | #define PLDM_MSGBUF_H |
| 4 | |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 5 | #include "compiler.h" |
| 6 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 7 | /* |
| 8 | * Historically, many of the structs exposed in libpldm's public headers are |
| 9 | * defined with __attribute__((packed)). This is unfortunate: it gives the |
| 10 | * impression that a wire-format buffer can be cast to the message type to make |
| 11 | * the message's fields easily accessible. As it turns out, that's not |
| 12 | * that's valid for several reasons: |
| 13 | * |
| 14 | * 1. Casting the wire-format buffer to a struct of the message type doesn't |
| 15 | * abstract the endianness of message field values |
| 16 | * |
| 17 | * 2. Some messages contain packed tagged union fields which cannot be properly |
| 18 | * described in a C struct. |
| 19 | * |
| 20 | * The msgbuf APIs exist to assist with (un)packing the wire-format in a way |
| 21 | * that is type-safe, spatially memory-safe, endian-safe, performant, and |
| 22 | * free of undefined-behaviour. Message structs that are added to the public |
| 23 | * library API should no-longer be marked __attribute__((packed)), and the |
| 24 | * implementation of their encode and decode functions must exploit the msgbuf |
| 25 | * API. |
| 26 | * |
| 27 | * However, we would like to allow implementation of codec functions in terms of |
| 28 | * msgbuf APIs even if they're decoding a message into a (historically) packed |
| 29 | * struct. Some of the complexity that follows is a consequence of the packed/ |
| 30 | * unpacked conflict. |
| 31 | */ |
| 32 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 33 | #ifdef __cplusplus |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 34 | /* |
| 35 | * Fix up C11's _Static_assert() vs C++'s static_assert(). |
| 36 | * |
| 37 | * Can we please have nice things for once. |
| 38 | */ |
| 39 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
| 40 | #define _Static_assert(...) static_assert(__VA_ARGS__) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Andrew Jeffery | b0c1d20 | 2023-11-07 22:08:44 +1030 | [diff] [blame] | 44 | #include <libpldm/base.h> |
| 45 | #include <libpldm/pldm_types.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 46 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 47 | #include "compiler.h" |
| 48 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 49 | #include <assert.h> |
| 50 | #include <endian.h> |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 51 | #include <errno.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 52 | #include <limits.h> |
| 53 | #include <stdbool.h> |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 54 | #include <stdint.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 55 | #include <string.h> |
| 56 | #include <sys/types.h> |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 57 | #include <uchar.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 58 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 59 | /* |
| 60 | * We can't use static_assert() outside of some other C construct. Deal |
| 61 | * with high-level global assertions by burying them in an unused struct |
| 62 | * declaration, that has a sole member for compliance with the requirement that |
| 63 | * types must have a size. |
| 64 | */ |
| 65 | static struct { |
| 66 | static_assert( |
| 67 | INTMAX_MAX != SIZE_MAX, |
| 68 | "Extraction and insertion value comparisons may be broken"); |
| 69 | static_assert(INTMAX_MIN + INTMAX_MAX <= 0, |
| 70 | "Extraction and insertion arithmetic may be broken"); |
| 71 | int compliance; |
Andrew Jeffery | 860a43d | 2024-08-23 01:21:58 +0000 | [diff] [blame] | 72 | } build_assertions LIBPLDM_CC_UNUSED; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 73 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 74 | struct pldm_msgbuf { |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 75 | uint8_t *cursor; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 76 | intmax_t remaining; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 77 | }; |
| 78 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 79 | LIBPLDM_CC_NONNULL |
| 80 | LIBPLDM_CC_ALWAYS_INLINE |
| 81 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
| 82 | void pldm__msgbuf_cleanup(struct pldm_msgbuf *ctx LIBPLDM_CC_UNUSED) |
| 83 | { |
| 84 | assert(ctx->cursor == NULL && ctx->remaining == INTMAX_MIN); |
| 85 | } |
| 86 | |
| 87 | #ifdef __cplusplus |
| 88 | // NOLINTBEGIN(bugprone-macro-parentheses) |
| 89 | #define PLDM_MSGBUF_DEFINE_P(name) \ |
| 90 | struct pldm_msgbuf _##name LIBPLDM_CC_CLEANUP( \ |
| 91 | pldm__msgbuf_cleanup) = { NULL, INTMAX_MIN }; \ |
| 92 | auto *name = &(_##name) |
| 93 | // NOLINTEND(bugprone-macro-parentheses) |
| 94 | #else |
| 95 | #define PLDM_MSGBUF_DEFINE_P(name) \ |
| 96 | struct pldm_msgbuf _##name LIBPLDM_CC_CLEANUP( \ |
| 97 | pldm__msgbuf_cleanup) = { NULL, INTMAX_MIN }; \ |
| 98 | struct pldm_msgbuf *(name) = &(_##name) |
| 99 | #endif |
| 100 | |
| 101 | LIBPLDM_CC_NONNULL |
| 102 | LIBPLDM_CC_ALWAYS_INLINE |
| 103 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
| 104 | int pldm__msgbuf_invalidate(struct pldm_msgbuf *ctx) |
| 105 | { |
| 106 | ctx->remaining = INTMAX_MIN; |
| 107 | return -EOVERFLOW; |
| 108 | } |
| 109 | |
Andrew Jeffery | d861a68 | 2024-06-03 21:43:09 +0930 | [diff] [blame] | 110 | /** |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 111 | * @brief Initialize pldm buf struct for buf extractor |
| 112 | * |
| 113 | * @param[out] ctx - pldm_msgbuf context for extractor |
| 114 | * @param[in] minsize - The minimum required length of buffer `buf` |
| 115 | * @param[in] buf - buffer to be extracted |
| 116 | * @param[in] len - size of buffer |
| 117 | * |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 118 | * @return 0 on success, otherwise an error code appropriate for the current |
| 119 | * personality. |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 120 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 121 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 122 | LIBPLDM_CC_ALWAYS_INLINE |
| 123 | LIBPLDM_CC_WARN_UNUSED_RESULT |
| 124 | int |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 125 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 126 | pldm_msgbuf_init_errno(struct pldm_msgbuf *ctx, size_t minsize, const void *buf, |
| 127 | size_t len) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 128 | { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 129 | ctx->cursor = NULL; |
| 130 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 131 | if ((minsize > len)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 132 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 133 | } |
| 134 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 135 | #if INTMAX_MAX < SIZE_MAX |
| 136 | if (len > INTMAX_MAX) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 137 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 138 | } |
| 139 | #endif |
| 140 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 141 | if (UINTPTR_MAX - (uintptr_t)buf < len) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 142 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | ctx->cursor = (uint8_t *)buf; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 146 | ctx->remaining = (intmax_t)len; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 147 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /** |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 152 | * @brief Validate buffer overflow state |
| 153 | * |
| 154 | * @param[in] ctx - pldm_msgbuf context for extractor |
| 155 | * |
| 156 | * @return PLDM_SUCCESS if there are zero or more bytes of data that remain |
| 157 | * unread from the buffer. Otherwise, PLDM_ERROR_INVALID_LENGTH indicates that a |
| 158 | * prior accesses would have occurred beyond the bounds of the buffer, and |
| 159 | * PLDM_ERROR_INVALID_DATA indicates that the provided context was not a valid |
| 160 | * pointer. |
| 161 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 162 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 163 | LIBPLDM_CC_ALWAYS_INLINE |
| 164 | LIBPLDM_CC_WARN_UNUSED_RESULT |
| 165 | int pldm_msgbuf_validate(struct pldm_msgbuf *ctx) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 166 | { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 167 | if (ctx->remaining < 0) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 168 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 169 | } |
| 170 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 171 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 175 | * @brief Test whether a message buffer has been exactly consumed |
| 176 | * |
| 177 | * @param[in] ctx - pldm_msgbuf context for extractor |
| 178 | * |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 179 | * @return 0 iff there are zero bytes of data that remain unread from the buffer |
Andrew Jeffery | 63b5a66 | 2025-03-05 13:26:12 +1030 | [diff] [blame] | 180 | * and no overflow has occurred. Otherwise, -EBADMSG if the buffer has not been |
| 181 | * completely consumed, or -EOVERFLOW if accesses were attempted beyond the |
| 182 | * bounds of the buffer. |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 183 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 184 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 185 | LIBPLDM_CC_ALWAYS_INLINE |
| 186 | LIBPLDM_CC_WARN_UNUSED_RESULT |
| 187 | int pldm_msgbuf_consumed(struct pldm_msgbuf *ctx) |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 188 | { |
Andrew Jeffery | 63b5a66 | 2025-03-05 13:26:12 +1030 | [diff] [blame] | 189 | if (ctx->remaining > 0) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 190 | return -EBADMSG; |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 191 | } |
| 192 | |
Andrew Jeffery | 63b5a66 | 2025-03-05 13:26:12 +1030 | [diff] [blame] | 193 | if (ctx->remaining < 0) { |
| 194 | return -EOVERFLOW; |
| 195 | } |
| 196 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 197 | return 0; |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 201 | * @brief End use of a msgbuf under error conditions |
| 202 | * |
| 203 | * @param[in] ctx - The msgbuf instance to discard |
| 204 | * @param[in] error - The error value to propagate |
| 205 | * |
| 206 | * Under normal conditions use of a msgbuf instance must be ended using @ref |
| 207 | * pldm_msgbuf_complete or one of its related APIs. Under error conditions, @ref |
| 208 | * pldm_msgbuf_discard should be used instead, as it makes it straight-forward |
| 209 | * to finalise the msgbuf while propagating the existing error code. |
| 210 | * |
| 211 | * @return The value provided in @param error |
| 212 | */ |
| 213 | LIBPLDM_CC_NONNULL |
| 214 | LIBPLDM_CC_ALWAYS_INLINE |
| 215 | LIBPLDM_CC_WARN_UNUSED_RESULT |
| 216 | int pldm_msgbuf_discard(struct pldm_msgbuf *ctx, int error) |
| 217 | { |
| 218 | ctx->cursor = NULL; |
| 219 | pldm__msgbuf_invalidate(ctx); |
| 220 | return error; |
| 221 | } |
| 222 | |
| 223 | /** |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 224 | * @brief Complete the pldm_msgbuf instance |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 225 | * |
| 226 | * @param[in] ctx - pldm_msgbuf context for extractor |
| 227 | * |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 228 | * @return 0 if all buffer accesses were in-bounds, -EOVERFLOW otherwise. |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 229 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 230 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 231 | LIBPLDM_CC_ALWAYS_INLINE |
| 232 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 233 | int pldm_msgbuf_complete(struct pldm_msgbuf *ctx) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 234 | { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 235 | return pldm_msgbuf_discard(ctx, pldm_msgbuf_validate(ctx)); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 239 | * @brief Complete the pldm_msgbuf instance, and check that the underlying buffer |
| 240 | * has been entirely consumed without overflow |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 241 | * |
| 242 | * @param[in] ctx - pldm_msgbuf context |
| 243 | * |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 244 | * @return 0 if all buffer access were in-bounds and completely consume the |
Andrew Jeffery | 63b5a66 | 2025-03-05 13:26:12 +1030 | [diff] [blame] | 245 | * underlying buffer. Otherwise, -EBADMSG if the buffer has not been completely |
| 246 | * consumed, or -EOVERFLOW if accesses were attempted beyond the bounds of the |
| 247 | * buffer. |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 248 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 249 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 250 | LIBPLDM_CC_ALWAYS_INLINE |
| 251 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 252 | int pldm_msgbuf_complete_consumed(struct pldm_msgbuf *ctx) |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 253 | { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 254 | return pldm_msgbuf_discard(ctx, pldm_msgbuf_consumed(ctx)); |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 255 | } |
| 256 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 257 | /* |
| 258 | * Exploit the pre-processor to perform type checking by macro substitution. |
| 259 | * |
| 260 | * A C type is defined by its alignment as well as its object |
| 261 | * size, and compilers have a hammer to enforce it in the form of |
| 262 | * `-Waddress-of-packed-member`. Due to the unpacked/packed struct conflict in |
| 263 | * the libpldm public API this presents a problem: Naively attempting to use the |
| 264 | * msgbuf APIs on a member of a packed struct would yield an error. |
| 265 | * |
| 266 | * The msgbuf APIs are implemented such that data is moved through unaligned |
| 267 | * pointers in a safe way, but to mitigate `-Waddress-of-packed-member` we must |
| 268 | * make the object pointers take a trip through `void *` at its API boundary. |
| 269 | * That presents a bit too much of an opportunity to non-surgically remove your |
| 270 | * own foot, so here we set about doing something to mitigate that as well. |
| 271 | * |
| 272 | * pldm_msgbuf_extract_typecheck() exists to enforce pointer type correctness |
| 273 | * only for the purpose of object sizes, disregarding alignment. We have a few |
| 274 | * constraints that cause some headaches: |
| 275 | * |
| 276 | * 1. We have to perform the type-check before a call through a C function, |
| 277 | * as the function must take the object pointer argument as `void *`. |
| 278 | * Essentially, this constrains us to doing something with macros. |
| 279 | * |
| 280 | * 2. While libpldm is a C library, its test suite is written in C++ to take |
| 281 | * advantage of gtest. |
| 282 | * |
| 283 | * 3. Ideally we'd do something with C's `static_assert()`, however |
| 284 | * `static_assert()` is defined as void, and as we're constrained to macros, |
| 285 | * using `static_assert()` would require a statement-expression |
| 286 | * |
| 287 | * 4. Currently the project is built with `-std=c17`. CPP statement-expressions |
| 288 | * are a GNU extension. We prefer to avoid switching to `-std=gnu17` just for |
| 289 | * the purpose of enabling statement-expressions in this one instance. |
| 290 | * |
| 291 | * 5. We can achieve a conditional build error using `pldm_require_obj_type()`, |
| 292 | * however it's implemented in terms of `_Generic()`, which is not available |
| 293 | * in C++. |
| 294 | * |
| 295 | * Combined this means we need separate solutions for C and C++. |
| 296 | * |
| 297 | * For C, as we don't have statement-expressions, we need to exploit some other |
| 298 | * language feature to inject a `pldm_require_obj_type()` prior to the msgbuf |
| 299 | * API function call. We also have to take care of the fact that the call-sites |
| 300 | * may be in the context of a variable assignment for error-handling purposes. |
| 301 | * The key observation is that we can use the comma operator as a sequence point |
| 302 | * to order the type check before the API call, discarding the "result" value of |
| 303 | * the type check and yielding the return value of the API call. |
| 304 | * |
| 305 | * C++ could be less of a headache than the C as we can leverage template |
| 306 | * functions. An advantage of template functions is that while their definition |
| 307 | * is driven by instantion, the definition does not appear at the source |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 308 | * location of the instantiation, which gives it a great leg-up over the problems |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 309 | * we have in the C path. However, the use of the msgbuf APIs in the test suite |
| 310 | * still makes things somewhat tricky, as the call-sites in the test suite are |
| 311 | * wrapped up in EXPECT_*() gtest macros. Ideally we'd implement functions that |
| 312 | * takes both the object type and the required type as template arguments, and |
| 313 | * then define the object pointer parameter as `void *` for a call through to |
| 314 | * the appropriate msgbuf API. However, because the msgbuf API call-sites are |
| 315 | * encapsulated in gtest macros, use of commas in the template specification |
| 316 | * causes pre-processor confusion. In this way we're constrained to only one |
| 317 | * template argument per function. |
| 318 | * |
| 319 | * Implement the C++ path using template functions that take the destination |
| 320 | * object type as a template argument, while the name of the function symbols |
| 321 | * are derived from the required type. The manual implementations of these |
| 322 | * appear at the end of the header. The type safety is actually enforced |
| 323 | * by `static_assert()` this time, as we can use statements as we're not |
| 324 | * constrained to an expression in the templated function body. |
| 325 | * |
| 326 | * The invocations of pldm_msgbuf_extract_typecheck() typically result in |
| 327 | * double-evaluation of some arguments. We're not yet bothered by this for two |
| 328 | * reasons: |
| 329 | * |
| 330 | * 1. The nature of the current call-sites are such that there are no |
| 331 | * argument expressions that result in undesirable side-effects |
| 332 | * |
| 333 | * 2. It's an API internal to the libpldm implementation, and we can fix things |
| 334 | * whenever something crops up the violates the observation in 1. |
| 335 | */ |
| 336 | #ifdef __cplusplus |
| 337 | #define pldm_msgbuf_extract_typecheck(ty, fn, dst, ...) \ |
| 338 | pldm_msgbuf_typecheck_##ty<decltype(dst)>(__VA_ARGS__) |
| 339 | #else |
| 340 | #define pldm_msgbuf_extract_typecheck(ty, fn, dst, ...) \ |
| 341 | (pldm_require_obj_type(dst, ty), fn(__VA_ARGS__)) |
| 342 | #endif |
| 343 | |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 344 | /** |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 345 | * @brief pldm_msgbuf extractor for a uint8_t |
| 346 | * |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 347 | * @param[in,out] ctx - pldm_msgbuf context for extractor |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 348 | * @param[out] dst - destination of extracted value |
| 349 | * |
| 350 | * @return PLDM_SUCCESS if buffer accesses were in-bounds, |
| 351 | * PLDM_ERROR_INVALID_LENGTH otherwise. |
| 352 | * PLDM_ERROR_INVALID_DATA if input a invalid ctx |
| 353 | */ |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 354 | #define pldm_msgbuf_extract_uint8(ctx, dst) \ |
| 355 | pldm_msgbuf_extract_typecheck(uint8_t, pldm__msgbuf_extract_uint8, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 356 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 357 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 358 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 359 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 360 | pldm__msgbuf_extract_uint8(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 361 | { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 362 | if (ctx->remaining >= (intmax_t)sizeof(uint8_t)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 363 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 364 | memcpy(dst, ctx->cursor, sizeof(uint8_t)); |
| 365 | ctx->cursor++; |
| 366 | ctx->remaining -= sizeof(uint8_t); |
| 367 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 368 | } |
| 369 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 370 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(uint8_t)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 371 | ctx->remaining -= sizeof(uint8_t); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 372 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 373 | } |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 374 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 375 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 376 | } |
| 377 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 378 | #define pldm_msgbuf_extract_int8(ctx, dst) \ |
| 379 | pldm_msgbuf_extract_typecheck(int8_t, pldm__msgbuf_extract_int8, dst, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 380 | ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 381 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 382 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 383 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 384 | pldm__msgbuf_extract_int8(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 385 | { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 386 | if (ctx->remaining >= (intmax_t)sizeof(int8_t)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 387 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 388 | memcpy(dst, ctx->cursor, sizeof(int8_t)); |
| 389 | ctx->cursor++; |
| 390 | ctx->remaining -= sizeof(int8_t); |
| 391 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 392 | } |
| 393 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 394 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(int8_t)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 395 | ctx->remaining -= sizeof(int8_t); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 396 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 397 | } |
| 398 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 399 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 400 | } |
| 401 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 402 | #define pldm_msgbuf_extract_uint16(ctx, dst) \ |
| 403 | pldm_msgbuf_extract_typecheck(uint16_t, pldm__msgbuf_extract_uint16, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 404 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 405 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 406 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 407 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 408 | pldm__msgbuf_extract_uint16(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 409 | { |
| 410 | uint16_t ldst; |
| 411 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 412 | // Check for underflow while tracking the magnitude of the buffer overflow |
| 413 | static_assert( |
| 414 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 415 | sizeof(ldst) < INTMAX_MAX, |
| 416 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 417 | |
| 418 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 419 | assert(ctx->cursor); |
| 420 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 421 | // Use memcpy() to have the compiler deal with any alignment |
| 422 | // issues on the target architecture |
| 423 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 424 | |
| 425 | // Only assign the target value once it's correctly decoded |
| 426 | ldst = le16toh(ldst); |
| 427 | |
| 428 | // Allow storing to unaligned |
| 429 | memcpy(dst, &ldst, sizeof(ldst)); |
| 430 | |
| 431 | ctx->cursor += sizeof(ldst); |
| 432 | ctx->remaining -= sizeof(ldst); |
| 433 | return 0; |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 434 | } |
| 435 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 436 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 437 | ctx->remaining -= sizeof(ldst); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 438 | return -EOVERFLOW; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 439 | } |
| 440 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 441 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 442 | } |
| 443 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 444 | #define pldm_msgbuf_extract_int16(ctx, dst) \ |
| 445 | pldm_msgbuf_extract_typecheck(int16_t, pldm__msgbuf_extract_int16, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 446 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 447 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 448 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 449 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 450 | pldm__msgbuf_extract_int16(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 451 | { |
| 452 | int16_t ldst; |
| 453 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 454 | static_assert( |
| 455 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 456 | sizeof(ldst) < INTMAX_MAX, |
| 457 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 458 | |
| 459 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 460 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 461 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 462 | ldst = le16toh(ldst); |
| 463 | memcpy(dst, &ldst, sizeof(ldst)); |
| 464 | ctx->cursor += sizeof(ldst); |
| 465 | ctx->remaining -= sizeof(ldst); |
| 466 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 467 | } |
| 468 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 469 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 470 | ctx->remaining -= sizeof(ldst); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 471 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 472 | } |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 473 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 474 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 475 | } |
| 476 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 477 | #define pldm_msgbuf_extract_uint32(ctx, dst) \ |
| 478 | pldm_msgbuf_extract_typecheck(uint32_t, pldm__msgbuf_extract_uint32, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 479 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 480 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 481 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 482 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 483 | pldm__msgbuf_extract_uint32(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 484 | { |
| 485 | uint32_t ldst; |
| 486 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 487 | static_assert( |
| 488 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 489 | sizeof(ldst) < INTMAX_MAX, |
| 490 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 491 | |
| 492 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 493 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 494 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 495 | ldst = le32toh(ldst); |
| 496 | memcpy(dst, &ldst, sizeof(ldst)); |
| 497 | ctx->cursor += sizeof(ldst); |
| 498 | ctx->remaining -= sizeof(ldst); |
| 499 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 500 | } |
| 501 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 502 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 503 | ctx->remaining -= sizeof(ldst); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 504 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 505 | } |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 506 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 507 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 508 | } |
| 509 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 510 | #define pldm_msgbuf_extract_int32(ctx, dst) \ |
| 511 | pldm_msgbuf_extract_typecheck(int32_t, pldm__msgbuf_extract_int32, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 512 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 513 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 514 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 515 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 516 | pldm__msgbuf_extract_int32(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 517 | { |
| 518 | int32_t ldst; |
| 519 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 520 | static_assert( |
| 521 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 522 | sizeof(ldst) < INTMAX_MAX, |
| 523 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 524 | |
| 525 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 526 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 527 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 528 | ldst = le32toh(ldst); |
| 529 | memcpy(dst, &ldst, sizeof(ldst)); |
| 530 | ctx->cursor += sizeof(ldst); |
| 531 | ctx->remaining -= sizeof(ldst); |
| 532 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 533 | } |
| 534 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 535 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 536 | ctx->remaining -= sizeof(ldst); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 537 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 538 | } |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 539 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 540 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 541 | } |
| 542 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 543 | #define pldm_msgbuf_extract_real32(ctx, dst) \ |
| 544 | pldm_msgbuf_extract_typecheck(real32_t, pldm__msgbuf_extract_real32, \ |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 545 | dst, ctx, (void *)&(dst)) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 546 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 547 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 548 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 549 | pldm__msgbuf_extract_real32(struct pldm_msgbuf *ctx, void *dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 550 | { |
| 551 | uint32_t ldst; |
| 552 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 553 | static_assert(sizeof(real32_t) == sizeof(ldst), |
| 554 | "Mismatched type sizes for dst and ldst"); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 555 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 556 | static_assert( |
| 557 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 558 | sizeof(ldst) < INTMAX_MAX, |
| 559 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 560 | |
| 561 | if (ctx->remaining >= (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 562 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 563 | memcpy(&ldst, ctx->cursor, sizeof(ldst)); |
| 564 | ldst = le32toh(ldst); |
| 565 | memcpy(dst, &ldst, sizeof(ldst)); |
| 566 | ctx->cursor += sizeof(ldst); |
| 567 | ctx->remaining -= sizeof(ldst); |
| 568 | return 0; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 569 | } |
| 570 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 571 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(ldst)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 572 | ctx->remaining -= sizeof(ldst); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 573 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 574 | } |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 575 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 576 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 577 | } |
| 578 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 579 | /** |
| 580 | * Extract the field at the msgbuf cursor into the lvalue named by dst. |
| 581 | * |
| 582 | * @param ctx The msgbuf context object |
| 583 | * @param dst The lvalue into which the field at the msgbuf cursor should be |
| 584 | * extracted |
| 585 | * |
| 586 | * @return PLDM_SUCCESS on success, otherwise another value on error |
| 587 | */ |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 588 | #define pldm_msgbuf_extract(ctx, dst) \ |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 589 | _Generic((dst), \ |
| 590 | uint8_t: pldm__msgbuf_extract_uint8, \ |
| 591 | int8_t: pldm__msgbuf_extract_int8, \ |
| 592 | uint16_t: pldm__msgbuf_extract_uint16, \ |
| 593 | int16_t: pldm__msgbuf_extract_int16, \ |
| 594 | uint32_t: pldm__msgbuf_extract_uint32, \ |
| 595 | int32_t: pldm__msgbuf_extract_int32, \ |
| 596 | real32_t: pldm__msgbuf_extract_real32)(ctx, (void *)&(dst)) |
| 597 | |
| 598 | /** |
| 599 | * Extract the field at the msgbuf cursor into the object pointed-to by dst. |
| 600 | * |
| 601 | * @param ctx The msgbuf context object |
| 602 | * @param dst The pointer to the object into which the field at the msgbuf |
| 603 | * cursor should be extracted |
| 604 | * |
| 605 | * @return PLDM_SUCCESS on success, otherwise another value on error |
| 606 | */ |
| 607 | #define pldm_msgbuf_extract_p(ctx, dst) \ |
| 608 | _Generic((dst), \ |
| 609 | uint8_t *: pldm__msgbuf_extract_uint8, \ |
| 610 | int8_t *: pldm__msgbuf_extract_int8, \ |
| 611 | uint16_t *: pldm__msgbuf_extract_uint16, \ |
| 612 | int16_t *: pldm__msgbuf_extract_int16, \ |
| 613 | uint32_t *: pldm__msgbuf_extract_uint32, \ |
| 614 | int32_t *: pldm__msgbuf_extract_int32, \ |
| 615 | real32_t *: pldm__msgbuf_extract_real32)(ctx, dst) |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 616 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 617 | /** |
| 618 | * @ref pldm_msgbuf_extract_array |
| 619 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 620 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 621 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 622 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 623 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 624 | pldm__msgbuf_extract_array_void(struct pldm_msgbuf *ctx, size_t count, |
| 625 | void *dst, size_t dst_count) |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 626 | { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 627 | if (count > dst_count) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 628 | return -EINVAL; |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | if (!count) { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 632 | return 0; |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 633 | } |
| 634 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 635 | #if INTMAX_MAX < SIZE_MAX |
| 636 | if (count > INTMAX_MAX) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 637 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 638 | } |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 639 | #endif |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 640 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 641 | if (ctx->remaining >= (intmax_t)count) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 642 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 643 | memcpy(dst, ctx->cursor, count); |
| 644 | ctx->cursor += count; |
| 645 | ctx->remaining -= (intmax_t)count; |
| 646 | return 0; |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 647 | } |
| 648 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 649 | if (ctx->remaining > INTMAX_MIN + (intmax_t)count) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 650 | ctx->remaining -= (intmax_t)count; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 651 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 652 | } |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 653 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 654 | return pldm__msgbuf_invalidate(ctx); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 655 | } |
| 656 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 657 | /** |
| 658 | * @ref pldm_msgbuf_extract_array |
| 659 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 660 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 661 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 662 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 663 | pldm_msgbuf_extract_array_char(struct pldm_msgbuf *ctx, size_t count, char *dst, |
| 664 | size_t dst_count) |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 665 | { |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 666 | return pldm__msgbuf_extract_array_void(ctx, count, dst, dst_count); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 667 | } |
| 668 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 669 | /** |
| 670 | * @ref pldm_msgbuf_extract_array |
| 671 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 672 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 673 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 674 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 675 | pldm_msgbuf_extract_array_uint8(struct pldm_msgbuf *ctx, size_t count, |
| 676 | uint8_t *dst, size_t dst_count) |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 677 | { |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 678 | return pldm__msgbuf_extract_array_void(ctx, count, dst, dst_count); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 679 | } |
| 680 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 681 | /** |
| 682 | * Extract an array of data from the msgbuf instance |
| 683 | * |
| 684 | * @param ctx - The msgbuf instance from which to extract an array of data |
| 685 | * @param count - The number of array elements to extract |
| 686 | * @param dst - The array object into which elements from @p ctx should be |
| 687 | extracted |
| 688 | * @param dst_count - The maximum number of elements to place into @p dst |
| 689 | * |
| 690 | * Note that both @p count and @p dst_count can only be counted by `sizeof` for |
| 691 | * arrays where `sizeof(*dst) == 1` holds. Specifically, they count the number |
| 692 | * of array elements and _not_ the object size of the array. |
| 693 | */ |
| 694 | #define pldm_msgbuf_extract_array(ctx, count, dst, dst_count) \ |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 695 | _Generic((*(dst)), \ |
| 696 | uint8_t: pldm_msgbuf_extract_array_uint8, \ |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 697 | char: pldm_msgbuf_extract_array_char)(ctx, count, dst, \ |
| 698 | dst_count) |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 699 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 700 | LIBPLDM_CC_NONNULL |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 701 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint64(struct pldm_msgbuf *ctx, |
| 702 | const uint64_t src) |
| 703 | { |
| 704 | uint64_t val = htole64(src); |
| 705 | |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 706 | static_assert( |
| 707 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 708 | sizeof(src) < INTMAX_MAX, |
| 709 | "The following addition may not uphold the runtime assertion"); |
| 710 | |
| 711 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 712 | assert(ctx->cursor); |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 713 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 714 | ctx->cursor += sizeof(src); |
| 715 | ctx->remaining -= sizeof(src); |
| 716 | return 0; |
| 717 | } |
| 718 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 719 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(src)) { |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 720 | ctx->remaining -= sizeof(src); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 721 | return -EOVERFLOW; |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 722 | } |
| 723 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 724 | return pldm__msgbuf_invalidate(ctx); |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 728 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint32(struct pldm_msgbuf *ctx, |
| 729 | const uint32_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 730 | { |
| 731 | uint32_t val = htole32(src); |
| 732 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 733 | static_assert( |
| 734 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 735 | sizeof(src) < INTMAX_MAX, |
| 736 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 737 | |
| 738 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 739 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 740 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 741 | ctx->cursor += sizeof(src); |
| 742 | ctx->remaining -= sizeof(src); |
| 743 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 744 | } |
| 745 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 746 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(src)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 747 | ctx->remaining -= sizeof(src); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 748 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 749 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 750 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 751 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 752 | } |
| 753 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 754 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 755 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint16(struct pldm_msgbuf *ctx, |
| 756 | const uint16_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 757 | { |
| 758 | uint16_t val = htole16(src); |
| 759 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 760 | static_assert( |
| 761 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 762 | sizeof(src) < INTMAX_MAX, |
| 763 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 764 | |
| 765 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 766 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 767 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 768 | ctx->cursor += sizeof(src); |
| 769 | ctx->remaining -= sizeof(src); |
| 770 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 771 | } |
| 772 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 773 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(src)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 774 | ctx->remaining -= sizeof(src); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 775 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 776 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 777 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 778 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 779 | } |
| 780 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 781 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 782 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_uint8(struct pldm_msgbuf *ctx, |
| 783 | const uint8_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 784 | { |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 785 | static_assert( |
| 786 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 787 | sizeof(src) < INTMAX_MAX, |
| 788 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 789 | |
| 790 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 791 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 792 | memcpy(ctx->cursor, &src, sizeof(src)); |
| 793 | ctx->cursor += sizeof(src); |
| 794 | ctx->remaining -= sizeof(src); |
| 795 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 796 | } |
| 797 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 798 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(src)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 799 | ctx->remaining -= sizeof(src); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 800 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 801 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 802 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 803 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 804 | } |
| 805 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 806 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 807 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int32(struct pldm_msgbuf *ctx, |
| 808 | const int32_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 809 | { |
| 810 | int32_t val = htole32(src); |
| 811 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 812 | static_assert( |
| 813 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 814 | sizeof(src) < INTMAX_MAX, |
| 815 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 816 | |
| 817 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 818 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 819 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 820 | ctx->cursor += sizeof(src); |
| 821 | ctx->remaining -= sizeof(src); |
| 822 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 823 | } |
| 824 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 825 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(src)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 826 | ctx->remaining -= sizeof(src); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 827 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 828 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 829 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 830 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 831 | } |
| 832 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 833 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 834 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int16(struct pldm_msgbuf *ctx, |
| 835 | const int16_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 836 | { |
| 837 | int16_t val = htole16(src); |
| 838 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 839 | static_assert( |
| 840 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 841 | sizeof(src) < INTMAX_MAX, |
| 842 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 843 | |
| 844 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 845 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 846 | memcpy(ctx->cursor, &val, sizeof(val)); |
| 847 | ctx->cursor += sizeof(src); |
| 848 | ctx->remaining -= sizeof(src); |
| 849 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 850 | } |
| 851 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 852 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(src)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 853 | ctx->remaining -= sizeof(src); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 854 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 855 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 856 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 857 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 858 | } |
| 859 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 860 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 861 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_insert_int8(struct pldm_msgbuf *ctx, |
| 862 | const int8_t src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 863 | { |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 864 | static_assert( |
| 865 | // NOLINTNEXTLINE(bugprone-sizeof-expression) |
| 866 | sizeof(src) < INTMAX_MAX, |
| 867 | "The following addition may not uphold the runtime assertion"); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 868 | |
| 869 | if (ctx->remaining >= (intmax_t)sizeof(src)) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 870 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 871 | memcpy(ctx->cursor, &src, sizeof(src)); |
| 872 | ctx->cursor += sizeof(src); |
| 873 | ctx->remaining -= sizeof(src); |
| 874 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 875 | } |
| 876 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 877 | if (ctx->remaining > INTMAX_MIN + (intmax_t)sizeof(src)) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 878 | ctx->remaining -= sizeof(src); |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 879 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 880 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 881 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 882 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | #define pldm_msgbuf_insert(dst, src) \ |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 886 | _Generic((src), \ |
| 887 | uint8_t: pldm_msgbuf_insert_uint8, \ |
| 888 | int8_t: pldm_msgbuf_insert_int8, \ |
| 889 | uint16_t: pldm_msgbuf_insert_uint16, \ |
| 890 | int16_t: pldm_msgbuf_insert_int16, \ |
| 891 | uint32_t: pldm_msgbuf_insert_uint32, \ |
Matt Johnston | e8d8d33 | 2024-10-28 17:13:32 +0800 | [diff] [blame] | 892 | int32_t: pldm_msgbuf_insert_int32, \ |
| 893 | uint64_t: pldm_msgbuf_insert_uint64)(dst, src) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 894 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 895 | /** |
| 896 | * @ref pldm_msgbuf_insert_array |
| 897 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 898 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 899 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 900 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 901 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 902 | pldm__msgbuf_insert_array_void(struct pldm_msgbuf *ctx, size_t count, |
| 903 | const void *src, size_t src_count) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 904 | { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 905 | if (count > src_count) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 906 | return -EINVAL; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | if (!count) { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 910 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 911 | } |
| 912 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 913 | #if INTMAX_MAX < SIZE_MAX |
| 914 | if (count > INTMAX_MAX) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 915 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 916 | } |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 917 | #endif |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 918 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 919 | if (ctx->remaining >= (intmax_t)count) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 920 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 921 | memcpy(ctx->cursor, src, count); |
| 922 | ctx->cursor += count; |
| 923 | ctx->remaining -= (intmax_t)count; |
| 924 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 925 | } |
| 926 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 927 | if (ctx->remaining > INTMAX_MIN + (intmax_t)count) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 928 | ctx->remaining -= (intmax_t)count; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 929 | return -EOVERFLOW; |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 930 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 931 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 932 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 933 | } |
| 934 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 935 | /** |
| 936 | * @ref pldm_msgbuf_insert_array |
| 937 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 938 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 939 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 940 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 941 | pldm_msgbuf_insert_array_char(struct pldm_msgbuf *ctx, size_t count, |
| 942 | const char *src, size_t src_count) |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 943 | { |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 944 | return pldm__msgbuf_insert_array_void(ctx, count, src, src_count); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 945 | } |
| 946 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 947 | /** |
| 948 | * @ref pldm_msgbuf_insert_array |
| 949 | */ |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 950 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 951 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 952 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 953 | pldm_msgbuf_insert_array_uint8(struct pldm_msgbuf *ctx, size_t count, |
| 954 | const uint8_t *src, size_t src_count) |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 955 | { |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 956 | return pldm__msgbuf_insert_array_void(ctx, count, src, src_count); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 957 | } |
| 958 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 959 | /** |
| 960 | * Insert an array of data into the msgbuf instance |
| 961 | * |
| 962 | * @param ctx - The msgbuf instance into which the array of data should be |
| 963 | * inserted |
| 964 | * @param count - The number of array elements to insert |
| 965 | * @param src - The array object from which elements should be inserted into |
| 966 | @p ctx |
| 967 | * @param src_count - The maximum number of elements to insert from @p src |
| 968 | * |
| 969 | * Note that both @p count and @p src_count can only be counted by `sizeof` for |
| 970 | * arrays where `sizeof(*dst) == 1` holds. Specifically, they count the number |
| 971 | * of array elements and _not_ the object size of the array. |
| 972 | */ |
| 973 | #define pldm_msgbuf_insert_array(dst, count, src, src_count) \ |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 974 | _Generic((*(src)), \ |
| 975 | uint8_t: pldm_msgbuf_insert_array_uint8, \ |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 976 | char: pldm_msgbuf_insert_array_char)(dst, count, src, \ |
| 977 | src_count) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 978 | |
Varsha Kaverappa | 7939382 | 2024-08-07 00:40:13 -0500 | [diff] [blame] | 979 | LIBPLDM_CC_NONNULL_ARGS(1) |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 980 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_span_required(struct pldm_msgbuf *ctx, |
| 981 | size_t required, |
| 982 | void **cursor) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 983 | { |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 984 | #if INTMAX_MAX < SIZE_MAX |
| 985 | if (required > INTMAX_MAX) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 986 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 987 | } |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 988 | #endif |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 989 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 990 | if (ctx->remaining >= (intmax_t)required) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 991 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 992 | if (cursor) { |
| 993 | *cursor = ctx->cursor; |
| 994 | } |
| 995 | ctx->cursor += required; |
| 996 | ctx->remaining -= (intmax_t)required; |
| 997 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 998 | } |
| 999 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1000 | if (ctx->remaining > INTMAX_MIN + (intmax_t)required) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1001 | ctx->remaining -= (intmax_t)required; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1002 | return -EOVERFLOW; |
Varsha Kaverappa | 7939382 | 2024-08-07 00:40:13 -0500 | [diff] [blame] | 1003 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1004 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1005 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1008 | LIBPLDM_CC_NONNULL_ARGS(1) |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1009 | LIBPLDM_CC_ALWAYS_INLINE int |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1010 | pldm_msgbuf_span_string_ascii(struct pldm_msgbuf *ctx, void **cursor, |
| 1011 | size_t *length) |
| 1012 | { |
| 1013 | intmax_t measured; |
| 1014 | |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1015 | if (ctx->remaining < 0) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1016 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1017 | } |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1018 | assert(ctx->cursor); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1019 | |
| 1020 | measured = (intmax_t)strnlen((const char *)ctx->cursor, ctx->remaining); |
| 1021 | if (measured == ctx->remaining) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1022 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* Include the NUL terminator in the span length, as spans are opaque */ |
| 1026 | measured++; |
| 1027 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1028 | if (ctx->remaining >= measured) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1029 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1030 | if (cursor) { |
| 1031 | *cursor = ctx->cursor; |
| 1032 | } |
| 1033 | |
| 1034 | ctx->cursor += measured; |
| 1035 | |
| 1036 | if (length) { |
| 1037 | *length = measured; |
| 1038 | } |
| 1039 | |
| 1040 | ctx->remaining -= measured; |
| 1041 | return 0; |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1044 | if (ctx->remaining > INTMAX_MIN + measured) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1045 | ctx->remaining -= measured; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1046 | return -EOVERFLOW; |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1049 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1052 | LIBPLDM_CC_NONNULL_ARGS(1) |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1053 | LIBPLDM_CC_ALWAYS_INLINE int |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1054 | pldm_msgbuf_span_string_utf16(struct pldm_msgbuf *ctx, void **cursor, |
| 1055 | size_t *length) |
| 1056 | { |
| 1057 | static const char16_t term = 0; |
| 1058 | ptrdiff_t measured; |
| 1059 | void *end; |
| 1060 | |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1061 | if (ctx->remaining < 0) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1062 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1063 | } |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1064 | assert(ctx->cursor); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1065 | |
| 1066 | /* |
| 1067 | * Avoid tripping up on UTF16-LE: We may have consecutive NUL _bytes_ that do |
| 1068 | * not form a UTF16 NUL _code-point_ due to alignment with respect to the |
| 1069 | * start of the string |
| 1070 | */ |
Andrew Jeffery | 2b440d4 | 2024-07-25 10:36:08 +0930 | [diff] [blame] | 1071 | end = ctx->cursor; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1072 | do { |
| 1073 | if (end != ctx->cursor) { |
| 1074 | /* |
| 1075 | * If we've looped we've found a relatively-unaligned NUL code-point. |
| 1076 | * Scan again from a relatively-aligned start point. |
| 1077 | */ |
| 1078 | end = (char *)end + 1; |
| 1079 | } |
| 1080 | measured = (char *)end - (char *)ctx->cursor; |
Andrew Jeffery | 2b440d4 | 2024-07-25 10:36:08 +0930 | [diff] [blame] | 1081 | end = memmem(end, ctx->remaining - measured, &term, |
| 1082 | sizeof(term)); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1083 | } while (end && ((uintptr_t)end & 1) != ((uintptr_t)ctx->cursor & 1)); |
| 1084 | |
| 1085 | if (!end) { |
| 1086 | /* |
| 1087 | * Optimistically, the last required pattern byte was one beyond the end of |
| 1088 | * the buffer. Setting ctx->remaining negative ensures the |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 1089 | * `pldm_msgbuf_complete*()` APIs also return an error. |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1090 | */ |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1091 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | end = (char *)end + sizeof(char16_t); |
| 1095 | measured = (char *)end - (char *)ctx->cursor; |
| 1096 | |
| 1097 | #if INTMAX_MAX < PTRDIFF_MAX |
| 1098 | if (measured >= INTMAX_MAX) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1099 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1100 | } |
| 1101 | #endif |
| 1102 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1103 | if (ctx->remaining >= (intmax_t)measured) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1104 | assert(ctx->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1105 | if (cursor) { |
| 1106 | *cursor = ctx->cursor; |
| 1107 | } |
| 1108 | |
| 1109 | ctx->cursor += measured; |
| 1110 | |
| 1111 | if (length) { |
| 1112 | *length = (size_t)measured; |
| 1113 | } |
| 1114 | |
| 1115 | ctx->remaining -= (intmax_t)measured; |
| 1116 | return 0; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1119 | if (ctx->remaining > INTMAX_MIN + (intmax_t)measured) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1120 | ctx->remaining -= (intmax_t)measured; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1121 | return -EOVERFLOW; |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1124 | return pldm__msgbuf_invalidate(ctx); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1127 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1128 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 1129 | pldm_msgbuf_span_remaining(struct pldm_msgbuf *ctx, void **cursor, size_t *len) |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1130 | { |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1131 | if (ctx->remaining < 0) { |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1132 | return -EOVERFLOW; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1135 | assert(ctx->cursor); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1136 | *cursor = ctx->cursor; |
| 1137 | ctx->cursor += ctx->remaining; |
| 1138 | *len = ctx->remaining; |
| 1139 | ctx->remaining = 0; |
| 1140 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1141 | return 0; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1142 | } |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1143 | |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1144 | LIBPLDM_CC_NONNULL |
| 1145 | LIBPLDM_CC_ALWAYS_INLINE int |
| 1146 | pldm_msgbuf_peek_remaining(struct pldm_msgbuf *ctx, void **cursor, size_t *len) |
| 1147 | { |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1148 | if (ctx->remaining < 0) { |
| 1149 | return -EOVERFLOW; |
| 1150 | } |
| 1151 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1152 | assert(ctx->cursor); |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1153 | *cursor = ctx->cursor; |
| 1154 | *len = ctx->remaining; |
| 1155 | |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | LIBPLDM_CC_NONNULL |
| 1160 | LIBPLDM_CC_ALWAYS_INLINE int pldm_msgbuf_skip(struct pldm_msgbuf *ctx, |
| 1161 | size_t count) |
| 1162 | { |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1163 | #if INTMAX_MAX < SIZE_MAX |
| 1164 | if (count > INTMAX_MAX) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1165 | return pldm__msgbuf_invalidate(ctx); |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1166 | } |
| 1167 | #endif |
| 1168 | |
Andrew Jeffery | 7e68f34 | 2025-03-03 21:37:23 +1030 | [diff] [blame] | 1169 | if (ctx->remaining >= (intmax_t)count) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1170 | assert(ctx->cursor); |
Andrew Jeffery | 7e68f34 | 2025-03-03 21:37:23 +1030 | [diff] [blame] | 1171 | ctx->cursor += count; |
| 1172 | ctx->remaining -= (intmax_t)count; |
| 1173 | return 0; |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1174 | } |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1175 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1176 | if (ctx->remaining > INTMAX_MIN + (intmax_t)count) { |
Andrew Jeffery | 7e68f34 | 2025-03-03 21:37:23 +1030 | [diff] [blame] | 1177 | ctx->remaining -= (intmax_t)count; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1178 | return -EOVERFLOW; |
Andrew Jeffery | 7e68f34 | 2025-03-03 21:37:23 +1030 | [diff] [blame] | 1179 | } |
| 1180 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1181 | return pldm__msgbuf_invalidate(ctx); |
Matt Johnston | 8f3b13c | 2024-10-16 16:08:21 +0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1184 | /** |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 1185 | * @brief Complete the pldm_msgbuf instance and return the number of bytes |
| 1186 | * consumed. |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1187 | * |
| 1188 | * @param ctx - The msgbuf. |
| 1189 | * @param orig_len - The original size of the msgbuf, the `len` argument passed to |
| 1190 | * pldm_msgbuf_init_errno(). |
| 1191 | * @param ret_used_len - The number of bytes that have been used from the msgbuf instance. |
| 1192 | * |
| 1193 | * This can be called after a number of pldm_msgbuf_insert...() calls to |
| 1194 | * determine the total size that was written. |
| 1195 | * |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 1196 | * @return 0 on success, -EOVERFLOW if an implausible orig_len was provided or |
| 1197 | * an out-of-bounds access occurred. |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1198 | */ |
| 1199 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 1200 | LIBPLDM_CC_ALWAYS_INLINE |
| 1201 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | 70d21c9 | 2025-03-05 12:59:42 +1030 | [diff] [blame] | 1202 | int pldm_msgbuf_complete_used(struct pldm_msgbuf *ctx, size_t orig_len, |
| 1203 | size_t *ret_used_len) |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1204 | { |
| 1205 | int rc; |
Andrew Jeffery | 704e4d5 | 2025-03-03 17:13:50 +1030 | [diff] [blame] | 1206 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1207 | ctx->cursor = NULL; |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1208 | rc = pldm_msgbuf_validate(ctx); |
| 1209 | if (rc) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1210 | pldm__msgbuf_invalidate(ctx); |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1211 | return rc; |
| 1212 | } |
| 1213 | |
| 1214 | if ((size_t)ctx->remaining > orig_len) { |
| 1215 | /* Caller passed incorrect orig_len */ |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1216 | return pldm__msgbuf_invalidate(ctx); |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | *ret_used_len = orig_len - ctx->remaining; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1220 | pldm__msgbuf_invalidate(ctx); |
Matt Johnston | 5d4f7b5 | 2024-12-12 14:03:57 +0800 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | /** |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1225 | * @brief pldm_msgbuf copy data between two msg buffers |
| 1226 | * |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 1227 | * @param[in,out] src - pldm_msgbuf for source from where value should be copied |
| 1228 | * @param[in,out] dst - destination of copy from source |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1229 | * @param[in] size - size of data to be copied |
| 1230 | * @param[in] description - description of data copied |
| 1231 | * |
| 1232 | * @return PLDM_SUCCESS if buffer accesses were in-bounds, |
| 1233 | * PLDM_ERROR_INVALID_LENGTH otherwise. |
| 1234 | * PLDM_ERROR_INVALID_DATA if input is invalid |
| 1235 | */ |
| 1236 | #define pldm_msgbuf_copy(dst, src, type, name) \ |
| 1237 | pldm__msgbuf_copy(dst, src, sizeof(type), #name) |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1238 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1239 | LIBPLDM_CC_ALWAYS_INLINE int |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1240 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
Andrew Jeffery | 76712f6 | 2024-05-22 15:19:00 +0930 | [diff] [blame] | 1241 | pldm__msgbuf_copy(struct pldm_msgbuf *dst, struct pldm_msgbuf *src, size_t size, |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1242 | const char *description LIBPLDM_CC_UNUSED) |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1243 | { |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1244 | #if INTMAX_MAX < SIZE_MAX |
| 1245 | if (size > INTMAX_MAX) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1246 | pldm__msgbuf_invalidate(src); |
| 1247 | pldm__msgbuf_invalidate(dst); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1248 | return -EOVERFLOW; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1249 | } |
| 1250 | #endif |
| 1251 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1252 | if (src->remaining >= (intmax_t)size && |
| 1253 | dst->remaining >= (intmax_t)size) { |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1254 | assert(src->cursor && dst->cursor); |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1255 | memcpy(dst->cursor, src->cursor, size); |
| 1256 | src->cursor += size; |
| 1257 | src->remaining -= (intmax_t)size; |
| 1258 | dst->cursor += size; |
| 1259 | dst->remaining -= (intmax_t)size; |
| 1260 | return 0; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1263 | if (src->remaining > INTMAX_MIN + (intmax_t)size) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1264 | src->remaining -= (intmax_t)size; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1265 | } else { |
| 1266 | pldm__msgbuf_invalidate(src); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1267 | } |
| 1268 | |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1269 | if (dst->remaining > INTMAX_MIN + (intmax_t)size) { |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1270 | dst->remaining -= (intmax_t)size; |
Andrew Jeffery | a189696 | 2025-03-03 21:41:25 +1030 | [diff] [blame] | 1271 | } else { |
| 1272 | pldm__msgbuf_invalidate(dst); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1273 | } |
| 1274 | |
Andrew Jeffery | 4f60fb7 | 2024-09-23 13:56:44 +0930 | [diff] [blame] | 1275 | return -EOVERFLOW; |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1276 | } |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1277 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1278 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1279 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1280 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1281 | pldm_msgbuf_copy_string_ascii(struct pldm_msgbuf *dst, struct pldm_msgbuf *src) |
| 1282 | { |
| 1283 | void *ascii = NULL; |
| 1284 | size_t len = 0; |
| 1285 | int rc; |
| 1286 | |
| 1287 | rc = pldm_msgbuf_span_string_ascii(src, &ascii, &len); |
| 1288 | if (rc < 0) { |
| 1289 | return rc; |
| 1290 | } |
| 1291 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1292 | return pldm__msgbuf_insert_array_void(dst, len, ascii, len); |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1293 | } |
| 1294 | |
Andrew Jeffery | 90bbe6c | 2024-09-01 13:02:02 +0300 | [diff] [blame] | 1295 | LIBPLDM_CC_NONNULL |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1296 | LIBPLDM_CC_WARN_UNUSED_RESULT |
Andrew Jeffery | cb569bc | 2024-09-01 09:38:09 +0300 | [diff] [blame] | 1297 | LIBPLDM_CC_ALWAYS_INLINE int |
Andrew Jeffery | 56f73f9 | 2024-07-08 12:50:28 +0930 | [diff] [blame] | 1298 | pldm_msgbuf_copy_string_utf16(struct pldm_msgbuf *dst, struct pldm_msgbuf *src) |
| 1299 | { |
| 1300 | void *utf16 = NULL; |
| 1301 | size_t len = 0; |
| 1302 | int rc; |
| 1303 | |
| 1304 | rc = pldm_msgbuf_span_string_utf16(src, &utf16, &len); |
| 1305 | if (rc < 0) { |
| 1306 | return rc; |
| 1307 | } |
| 1308 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1309 | return pldm__msgbuf_insert_array_void(dst, len, utf16, len); |
Andrew Jeffery | 56f73f9 | 2024-07-08 12:50:28 +0930 | [diff] [blame] | 1310 | } |
| 1311 | |
Chau Ly | 1a4eed9 | 2025-03-21 14:57:57 +0000 | [diff] [blame] | 1312 | /** |
| 1313 | * @brief pldm_msgbuf uint8_t extractor for a size_t |
| 1314 | * |
| 1315 | * @param[in,out] ctx - pldm_msgbuf context for extractor |
| 1316 | * @param[out] dst - destination of extracted value |
| 1317 | * |
| 1318 | * @return 0 if buffer accesses were in-bounds, |
| 1319 | * -EINVAL if dst pointer is invalid, |
| 1320 | * -EOVERFLOW is the buffer was out of bound. |
| 1321 | */ |
| 1322 | #define pldm_msgbuf_extract_uint8_to_size(ctx, dst) \ |
| 1323 | pldm__msgbuf_extract_uint8_to_size(ctx, &(dst)) |
| 1324 | LIBPLDM_CC_NONNULL |
| 1325 | LIBPLDM_CC_ALWAYS_INLINE int |
| 1326 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
| 1327 | pldm__msgbuf_extract_uint8_to_size(struct pldm_msgbuf *ctx, size_t *dst) |
| 1328 | { |
| 1329 | uint8_t value; |
| 1330 | int rc; |
| 1331 | |
| 1332 | rc = pldm__msgbuf_extract_uint8(ctx, &value); |
| 1333 | if (rc) { |
| 1334 | return rc; |
| 1335 | } |
| 1336 | |
| 1337 | static_assert(SIZE_MAX >= UINT8_MAX, "Invalid promotion"); |
| 1338 | |
| 1339 | *dst = value; |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | /** |
| 1344 | * @brief pldm_msgbuf uint16_t extractor for a size_t |
| 1345 | * |
| 1346 | * @param[in,out] ctx - pldm_msgbuf context for extractor |
| 1347 | * @param[out] dst - destination of extracted value |
| 1348 | * |
| 1349 | * @return 0 if buffer accesses were in-bounds, |
| 1350 | * -EINVAL if dst pointer is invalid, |
| 1351 | * -EOVERFLOW is the buffer was out of bound. |
| 1352 | */ |
| 1353 | #define pldm_msgbuf_extract_uint16_to_size(ctx, dst) \ |
| 1354 | pldm__msgbuf_extract_uint16_to_size(ctx, &(dst)) |
| 1355 | LIBPLDM_CC_NONNULL |
| 1356 | LIBPLDM_CC_ALWAYS_INLINE int |
| 1357 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
| 1358 | pldm__msgbuf_extract_uint16_to_size(struct pldm_msgbuf *ctx, size_t *dst) |
| 1359 | { |
| 1360 | uint16_t value; |
| 1361 | int rc; |
| 1362 | |
| 1363 | rc = pldm__msgbuf_extract_uint16(ctx, &value); |
| 1364 | if (rc) { |
| 1365 | return rc; |
| 1366 | } |
| 1367 | |
| 1368 | static_assert(SIZE_MAX >= UINT16_MAX, "Invalid promotion"); |
| 1369 | |
| 1370 | *dst = value; |
| 1371 | return 0; |
| 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * @brief pldm_msgbuf uint32_t extractor for a size_t |
| 1376 | * |
| 1377 | * @param[in,out] ctx - pldm_msgbuf context for extractor |
| 1378 | * @param[out] dst - destination of extracted value |
| 1379 | * |
| 1380 | * @return 0 if buffer accesses were in-bounds, |
| 1381 | * -EINVAL if dst pointer is invalid, |
| 1382 | * -EOVERFLOW is the buffer was out of bound. |
| 1383 | */ |
| 1384 | #define pldm_msgbuf_extract_uint32_to_size(ctx, dst) \ |
| 1385 | pldm__msgbuf_extract_uint32_to_size(ctx, &(dst)) |
| 1386 | LIBPLDM_CC_NONNULL |
| 1387 | LIBPLDM_CC_ALWAYS_INLINE int |
| 1388 | // NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
| 1389 | pldm__msgbuf_extract_uint32_to_size(struct pldm_msgbuf *ctx, size_t *dst) |
| 1390 | { |
| 1391 | uint32_t value; |
| 1392 | int rc; |
| 1393 | |
| 1394 | rc = pldm__msgbuf_extract_uint32(ctx, &value); |
| 1395 | if (rc) { |
| 1396 | return rc; |
| 1397 | } |
| 1398 | |
| 1399 | static_assert(SIZE_MAX >= UINT32_MAX, "Invalid promotion"); |
| 1400 | |
| 1401 | *dst = value; |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 1405 | #ifdef __cplusplus |
| 1406 | } |
| 1407 | #endif |
| 1408 | |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1409 | #ifdef __cplusplus |
| 1410 | #include <type_traits> |
| 1411 | |
| 1412 | template <typename T> |
| 1413 | static inline int pldm_msgbuf_typecheck_uint8_t(struct pldm_msgbuf *ctx, |
| 1414 | void *buf) |
| 1415 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1416 | static_assert(std::is_same<uint8_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1417 | return pldm__msgbuf_extract_uint8(ctx, buf); |
| 1418 | } |
| 1419 | |
| 1420 | template <typename T> |
| 1421 | static inline int pldm_msgbuf_typecheck_int8_t(struct pldm_msgbuf *ctx, |
| 1422 | void *buf) |
| 1423 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1424 | static_assert(std::is_same<int8_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1425 | return pldm__msgbuf_extract_int8(ctx, buf); |
| 1426 | } |
| 1427 | |
| 1428 | template <typename T> |
| 1429 | static inline int pldm_msgbuf_typecheck_uint16_t(struct pldm_msgbuf *ctx, |
| 1430 | void *buf) |
| 1431 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1432 | static_assert(std::is_same<uint16_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1433 | return pldm__msgbuf_extract_uint16(ctx, buf); |
| 1434 | } |
| 1435 | |
| 1436 | template <typename T> |
| 1437 | static inline int pldm_msgbuf_typecheck_int16_t(struct pldm_msgbuf *ctx, |
| 1438 | void *buf) |
| 1439 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1440 | static_assert(std::is_same<int16_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1441 | return pldm__msgbuf_extract_int16(ctx, buf); |
| 1442 | } |
| 1443 | |
| 1444 | template <typename T> |
| 1445 | static inline int pldm_msgbuf_typecheck_uint32_t(struct pldm_msgbuf *ctx, |
| 1446 | void *buf) |
| 1447 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1448 | static_assert(std::is_same<uint32_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1449 | return pldm__msgbuf_extract_uint32(ctx, buf); |
| 1450 | } |
| 1451 | |
| 1452 | template <typename T> |
| 1453 | static inline int pldm_msgbuf_typecheck_int32_t(struct pldm_msgbuf *ctx, |
| 1454 | void *buf) |
| 1455 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1456 | static_assert(std::is_same<int32_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1457 | return pldm__msgbuf_extract_int32(ctx, buf); |
| 1458 | } |
| 1459 | |
| 1460 | template <typename T> |
| 1461 | static inline int pldm_msgbuf_typecheck_real32_t(struct pldm_msgbuf *ctx, |
| 1462 | void *buf) |
| 1463 | { |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1464 | static_assert(std::is_same<real32_t, T>::value); |
Andrew Jeffery | 66c7723 | 2024-04-24 11:42:02 +0930 | [diff] [blame] | 1465 | return pldm__msgbuf_extract_real32(ctx, buf); |
| 1466 | } |
| 1467 | #endif |
| 1468 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 1469 | #endif /* BUF_H */ |