Skip to content

Commit

Permalink
capi: Use home-grown strcpy for statically bounded buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Mar 25, 2021
1 parent 53917a2 commit d02dde6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "instantiate.hpp"
#include "parser.hpp"
#include <fizzy/fizzy.h>
#include <cstring>
#include <memory>

namespace
Expand All @@ -20,12 +21,30 @@ inline void set_success(FizzyError* error) noexcept
}
}

template <size_t DestSize>
inline size_t strlcpy_s(char* dest, const char* src) noexcept
{
static_assert(DestSize >= 3);

const auto src_len = strlen(src);
size_t copy_len = std::min(src_len, DestSize - 1);
memcpy(dest, src, copy_len);
if (copy_len < src_len)
{
dest[copy_len - 3] = '.';
dest[copy_len - 2] = '.';
dest[copy_len - 1] = '.';
}
dest[copy_len] = '\0';
return copy_len;
}

inline void set_error(FizzyErrorCode code, const char* message, FizzyError* error) noexcept
{
if (error != nullptr)
{
error->code = code;
snprintf(error->message, std::size(error->message), "%s", message);
strlcpy_s<sizeof(error->message)>(error->message, message);
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/unittests/capi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ TEST(capi, validate)
EXPECT_STREQ(error.message, "invalid wasm module prefix");
}

TEST(capi, validate_long_duplicate_export_name)
{
// clang-format off
/* wat2wasm --no-check
(func (export "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in"))
(func (export "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in"))
*/
const auto wasm = from_hex(
"0061736d0100000001040160000003030200000789040280024c6f72656d20697073756d20646f6c6f72207369"
"7420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742c2073656420646f2065"
"6975736d6f642074656d706f7220696e6369646964756e74207574206c61626f726520657420646f6c6f726520"
"6d61676e6120616c697175612e20557420656e696d206164206d696e696d2076656e69616d2c2071756973206e"
"6f737472756420657865726369746174696f6e20756c6c616d636f206c61626f726973206e6973692075742061"
"6c697175697020657820656120636f6d6d6f646f20636f6e7365717561742e2044756973206175746520697275"
"726520646f6c6f7220696e000080024c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f"
"6e73656374657475722061646970697363696e6720656c69742c2073656420646f20656975736d6f642074656d"
"706f7220696e6369646964756e74207574206c61626f726520657420646f6c6f7265206d61676e6120616c6971"
"75612e20557420656e696d206164206d696e696d2076656e69616d2c2071756973206e6f737472756420657865"
"726369746174696f6e20756c6c616d636f206c61626f726973206e69736920757420616c697175697020657820"
"656120636f6d6d6f646f20636f6e7365717561742e2044756973206175746520697275726520646f6c6f722069"
"6e00010a070202000b02000b");
// clang-format on

FizzyError error;
EXPECT_FALSE(fizzy_validate(wasm.data(), wasm.size(), &error));
EXPECT_EQ(error.code, FIZZY_ERROR_OTHER);
EXPECT_STREQ(error.message,
"duplicate export name Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis "
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat...");
}

TEST(capi, parse)
{
uint8_t wasm_prefix[]{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00};
Expand Down

0 comments on commit d02dde6

Please sign in to comment.