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