Skip to content

Commit

Permalink
in-place decode and regular decode return BAD_PAYLOAD if there are an…
Browse files Browse the repository at this point in the history
…y non-terminal zero bytes in the payload (#44)

* in-place decode and regular decode return BAD_PAYLOAD  if there are any non-terminal zero bytes in the payload

* actions v3, MP on win
  • Loading branch information
charlesnicholson authored Dec 21, 2022
1 parent dbdc6e9 commit 9482051
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
CXX: /usr/bin/clang++

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Build
run: COBS_SANITIZE=1 make -j

Expand All @@ -46,15 +46,15 @@ jobs:
architecture: [32, 64]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Build
run: COBS_LINUXARCH=${{ matrix.architecture }} make -j

macos:
runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Build
run: make -j

Expand All @@ -66,7 +66,7 @@ jobs:
architecture: [32, 64]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Build
shell: cmd
run: |
Expand Down
11 changes: 4 additions & 7 deletions cobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

typedef unsigned char cobs_byte_t;


cobs_ret_t cobs_encode_inplace(void *buf, unsigned len) {
if (!buf || (len < 2)) { return COBS_RET_ERR_BAD_ARG; }

Expand All @@ -30,14 +29,16 @@ cobs_ret_t cobs_encode_inplace(void *buf, unsigned len) {
return COBS_RET_SUCCESS;
}


cobs_ret_t cobs_decode_inplace(void *buf, unsigned const len) {
if (!buf || (len < 2)) { return COBS_RET_ERR_BAD_ARG; }

cobs_byte_t *const src = (cobs_byte_t *)buf;
unsigned ofs, cur = 0;
while (cur < len && ((ofs = src[cur]) != COBS_FRAME_DELIMITER)) {
src[cur] = 0;
for (unsigned i = 1; i < ofs; ++i) {
if (src[cur + i] == 0) { return COBS_RET_ERR_BAD_PAYLOAD; }
}
cur += ofs;
}

Expand All @@ -47,7 +48,6 @@ cobs_ret_t cobs_decode_inplace(void *buf, unsigned const len) {
return COBS_RET_SUCCESS;
}


cobs_ret_t cobs_encode(void const *dec,
unsigned dec_len,
void *out_enc,
Expand All @@ -65,7 +65,6 @@ cobs_ret_t cobs_encode(void const *dec,
return r;
}


cobs_ret_t cobs_encode_inc_begin(void *out_enc,
unsigned enc_max,
cobs_enc_ctx_t *out_ctx) {
Expand All @@ -81,7 +80,6 @@ cobs_ret_t cobs_encode_inc_begin(void *out_enc,
return COBS_RET_SUCCESS;
}


cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx,
void const *dec,
unsigned dec_len) {
Expand Down Expand Up @@ -133,7 +131,6 @@ cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx,
return COBS_RET_SUCCESS;
}


cobs_ret_t cobs_encode_inc_end(cobs_enc_ctx_t *ctx, unsigned *out_enc_len) {
if (!ctx || !out_enc_len) { return COBS_RET_ERR_BAD_ARG; }

Expand All @@ -145,7 +142,6 @@ cobs_ret_t cobs_encode_inc_end(cobs_enc_ctx_t *ctx, unsigned *out_enc_len) {
return COBS_RET_SUCCESS;
}


cobs_ret_t cobs_decode(void const *enc,
unsigned enc_len,
void *out_dec,
Expand All @@ -170,6 +166,7 @@ cobs_ret_t cobs_decode(void const *enc,

if ((dst_idx + code - 1) > dec_max) { return COBS_RET_ERR_EXHAUSTED; }
for (unsigned i = 0; i < code - 1; ++i) {
if (src[src_idx] == 0) { return COBS_RET_ERR_BAD_PAYLOAD; }
dst[dst_idx++] = src[src_idx++];
}

Expand Down
2 changes: 1 addition & 1 deletion make-win.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cl.exe /W4 /WX /EHsc ^
cl.exe /W4 /WX /MP /EHsc ^
cobs.c ^
tests/test_cobs_decode.cc ^
tests/test_cobs_decode_inplace.cc ^
Expand Down
26 changes: 18 additions & 8 deletions tests/test_cobs_decode.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
#include "../cobs.h"
#include "byte_vec.h"
#include "doctest.h"

#include <cstring>

TEST_CASE("Decoding validation") {
unsigned char enc[32], dec[32];
unsigned char dec[32];
unsigned dec_len;

SUBCASE("Invalid payload") {
// first byte jumps past end
enc[0] = 3;
enc[1] = 0;
REQUIRE(cobs_decode(&enc, 2, dec, sizeof(enc), &dec_len) == COBS_RET_ERR_BAD_PAYLOAD);
SUBCASE("Invalid payload: jump past end") {
byte_vec_t enc{3, 0};
REQUIRE(cobs_decode(enc.data(),
unsigned(enc.size()),
dec,
sizeof(dec),
&dec_len) == COBS_RET_ERR_BAD_PAYLOAD);
}

SUBCASE("Invalid payload: jump over internal zeroes") {
byte_vec_t enc{5, 1, 0, 0, 1, 0};
REQUIRE(cobs_decode(enc.data(),
unsigned(enc.size()),
dec,
sizeof(dec),
&dec_len) == COBS_RET_ERR_BAD_PAYLOAD);
}
}
4 changes: 4 additions & 0 deletions tests/test_cobs_decode_inplace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ TEST_CASE("Inplace decoding validation") {
REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD);
buf = byte_vec_t{0x01, 0x00, 0x00}; // land on an interior 0x00
REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD);
buf = byte_vec_t{0x02, 0x00, 0x00}; // jump over an interior 0x00
REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD);
buf = byte_vec_t{0x04, 0x01, 0x00, 0x01, 0x00}; // jump over interior 0x00
REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD);
}
}

Expand Down

0 comments on commit 9482051

Please sign in to comment.