msgbuf: Return -EOVERFLOW where relevant in pldm_msgbuf_consumed()
-EBADMSG seems less appropriate for access patterns known to exceed
buffer limits.
Change-Id: I3051323cad0ec126c0fe5073902fcc50f8ff18a0
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/src/msgbuf.h b/src/msgbuf.h
index 437b423..cf30bad 100644
--- a/src/msgbuf.h
+++ b/src/msgbuf.h
@@ -144,17 +144,23 @@
* @param[in] ctx - pldm_msgbuf context for extractor
*
* @return 0 iff there are zero bytes of data that remain unread from the buffer
- * and no overflow has occurred. Otherwise, -EBADMSG.
+ * and no overflow has occurred. Otherwise, -EBADMSG if the buffer has not been
+ * completely consumed, or -EOVERFLOW if accesses were attempted beyond the
+ * bounds of the buffer.
*/
LIBPLDM_CC_NONNULL
LIBPLDM_CC_ALWAYS_INLINE
LIBPLDM_CC_WARN_UNUSED_RESULT
int pldm_msgbuf_consumed(struct pldm_msgbuf *ctx)
{
- if (ctx->remaining != 0) {
+ if (ctx->remaining > 0) {
return -EBADMSG;
}
+ if (ctx->remaining < 0) {
+ return -EOVERFLOW;
+ }
+
return 0;
}
@@ -187,7 +193,9 @@
* @param[in] ctx - pldm_msgbuf context
*
* @return 0 if all buffer access were in-bounds and completely consume the
- * underlying buffer. Otherwise, -EBADMSG.
+ * underlying buffer. Otherwise, -EBADMSG if the buffer has not been completely
+ * consumed, or -EOVERFLOW if accesses were attempted beyond the bounds of the
+ * buffer.
*/
LIBPLDM_CC_NONNULL
LIBPLDM_CC_ALWAYS_INLINE
diff --git a/tests/msgbuf.cpp b/tests/msgbuf.cpp
index bb64be9..14f5d73 100644
--- a/tests/msgbuf.cpp
+++ b/tests/msgbuf.cpp
@@ -516,7 +516,7 @@
ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, valid), 0);
EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, invalid), 0);
- EXPECT_EQ(pldm_msgbuf_complete_consumed(ctx), -EBADMSG);
+ EXPECT_EQ(pldm_msgbuf_complete_consumed(ctx), -EOVERFLOW);
}
TEST(msgbuf, pldm_msgbuf_insert_int32_good)