Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash fix and test for using long default values #176

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build_with_bam.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
IF "%VS170COMNTOOLS%"=="" CALL "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
local\bam\bam.exe config=debug platform=winx64
29 changes: 25 additions & 4 deletions src/dl_typelib_read_txt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ static const dl_builtin_type* dl_find_builtin_type( const char* name )
dl_type_t dl_make_type( dl_type_atom_t atom, dl_type_storage_t storage );
dl_error_t dl_txt_pack_internal( dl_ctx_t dl_ctx, const char* txt_instance, unsigned char* out_buffer, size_t out_buffer_size, size_t* produced_bytes, bool use_fast_ptr_patch );

struct SScopedPointer
{
~SScopedPointer()
{
if (alloc)
dl_free( alloc, ptr );
}
dl_allocator* alloc = nullptr;
void* ptr;
};

static void dl_load_txt_build_default_data( dl_ctx_t ctx, dl_txt_read_ctx* read_state, unsigned int member_index )
{
if( ctx->member_descs[member_index].default_value_offset == 0xFFFFFFFF )
Expand All @@ -215,7 +226,8 @@ static void dl_load_txt_build_default_data( dl_ctx_t ctx, dl_txt_read_ctx* read_
uint32_t def_start = member->default_value_offset;
uint32_t def_len = member->default_value_size;

char def_buffer[2048]; // TODO: no hardcode =/
char def_buffer[2048];
char* def_buffer_ptr = def_buffer;

// TODO: check that typename do not exist in the ctx!

Expand All @@ -230,11 +242,20 @@ static void dl_load_txt_build_default_data( dl_ctx_t ctx, dl_txt_read_ctx* read_
def_member->offset[0] = 0;
def_member->offset[1] = 0;

dl_internal_str_format( def_buffer, sizeof(def_buffer), "{\"a_type_here\":{\"%s\":%.*s}}", dl_internal_member_name( ctx, member ), (int)def_len, read_state->start + def_start );
int wanted_length = dl_internal_str_format( def_buffer, sizeof(def_buffer), "{\"a_type_here\":{\"%s\":%.*s}}", dl_internal_member_name( ctx, member ), (int)def_len, read_state->start + def_start );
SScopedPointer def_buffer_scope {};
if( size_t(wanted_length) >= sizeof( def_buffer ) )
{
def_buffer_ptr = (char*)dl_alloc( &ctx->alloc, size_t(wanted_length + 1) );
def_buffer_scope.alloc = &ctx->alloc;
def_buffer_scope.ptr = def_buffer_ptr;
int written_length = dl_internal_str_format( def_buffer_ptr, size_t(wanted_length + 1), "{\"a_type_here\":{\"%s\":%.*s}}", dl_internal_member_name( ctx, member ), (int)def_len, read_state->start + def_start );
DL_ASSERT( wanted_length == written_length ); (void) written_length;
}

size_t prod_bytes;
dl_error_t err;
err = dl_txt_pack( ctx, def_buffer, 0x0, 0, &prod_bytes );
err = dl_txt_pack( ctx, def_buffer_ptr, 0x0, 0, &prod_bytes );
if( err != DL_ERROR_OK )
dl_txt_read_failed( ctx, read_state, DL_ERROR_INVALID_DEFAULT_VALUE, "failed to pack default-value for member \"%s\" with error \"%s\"",
dl_internal_member_name( ctx, member ),
Expand All @@ -243,7 +264,7 @@ static void dl_load_txt_build_default_data( dl_ctx_t ctx, dl_txt_read_ctx* read_
uint8_t* pack_buffer = (uint8_t*)dl_alloc( &ctx->alloc, prod_bytes );

bool use_fast_ptr_patch = false;
err = dl_txt_pack_internal( ctx, def_buffer, pack_buffer, prod_bytes, 0x0, use_fast_ptr_patch );
err = dl_txt_pack_internal( ctx, def_buffer_ptr, pack_buffer, prod_bytes, 0x0, use_fast_ptr_patch );
if( err != DL_ERROR_OK )
dl_txt_read_failed( ctx, read_state, DL_ERROR_INVALID_DEFAULT_VALUE, "failed to pack default-value for member \"%s\" with error \"%s\"",
dl_internal_member_name( ctx, member ),
Expand Down
38 changes: 38 additions & 0 deletions tests/dl_tests_typelib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,44 @@ TEST_F( DLTypeLibTxt, DISABLED_default_inl_arr_of_bits )
"InlineArray": { "members": [ { "name": "seeds", "type": "BitField[2]", "default": [ { "bits": 1 } ] } ] }
}
} );
EXPECT_DL_ERR_OK( dl_context_load_txt_type_library( ctx, json_typelib, sizeof( json_typelib ) - 1 ) );
}

TEST_F( DLTypeLibTxt, long_default_value )
{
const char json_typelib[] = STRINGIFY( {
"types": {
"InlineArray": { "members": [ { "name": "seeds", "type": "uint64[128]", "default": [
0x1234567812345678, 0x2345678123456781, 0x3456781234567812, 0x4567812345678123, 0x5678123456781234,
0x6781234567812345, 0x7812345678123456, 0x8123456781234567, 0x2345678923456789, 0x3456789234567892,
0x4567892345678923, 0x5678923456789234, 0x6789234567892345, 0x7892345678923456, 0x8923456789234567,
0x9234567892345678, 0x0123456701234567, 0x1234567012345670, 0x2345670123456701, 0x3456701234567012,
0x4567012345670123, 0x5670123456701234, 0x6701234567012345, 0x7012345670123456, 0x0123456789012345,
0x1234567890123456, 0x2345678901234567, 0x3456789012345678, 0x4567890123456789, 0x5678901234567890,
0x6789012345678901, 0x7890123456789012, 0x1234567812345678, 0x2345678123456781, 0x3456781234567812,
0x4567812345678123, 0x5678123456781234, 0x6781234567812345, 0x7812345678123456, 0x8123456781234567,
0x2345678923456789, 0x3456789234567892, 0x4567892345678923, 0x5678923456789234, 0x6789234567892345,
0x7892345678923456, 0x8923456789234567, 0x9234567892345678, 0x0123456701234567, 0x1234567012345670,
0x2345670123456701, 0x3456701234567012, 0x4567012345670123, 0x5670123456701234, 0x6701234567012345,
0x7012345670123456, 0x0123456789012345, 0x1234567890123456, 0x2345678901234567, 0x3456789012345678,
0x4567890123456789, 0x5678901234567890, 0x6789012345678901, 0x7890123456789012, 0x1234567812345678,
0x2345678123456781, 0x3456781234567812, 0x4567812345678123, 0x5678123456781234, 0x6781234567812345,
0x7812345678123456, 0x8123456781234567, 0x2345678923456789, 0x3456789234567892, 0x4567892345678923,
0x5678923456789234, 0x6789234567892345, 0x7892345678923456, 0x8923456789234567, 0x9234567892345678,
0x0123456701234567, 0x1234567012345670, 0x2345670123456701, 0x3456701234567012, 0x4567012345670123,
0x5670123456701234, 0x6701234567012345, 0x7012345670123456, 0x0123456789012345, 0x1234567890123456,
0x2345678901234567, 0x3456789012345678, 0x4567890123456789, 0x5678901234567890, 0x6789012345678901,
0x7890123456789012, 0x1234567812345678, 0x2345678123456781, 0x3456781234567812, 0x4567812345678123,
0x5678123456781234, 0x6781234567812345, 0x7812345678123456, 0x8123456781234567, 0x2345678923456789,
0x3456789234567892, 0x4567892345678923, 0x5678923456789234, 0x6789234567892345, 0x7892345678923456,
0x8923456789234567, 0x9234567892345678, 0x0123456701234567, 0x1234567012345670, 0x2345670123456701,
0x3456701234567012, 0x4567012345670123, 0x5670123456701234, 0x6701234567012345, 0x7012345670123456,
0x0123456789012345, 0x1234567890123456, 0x2345678901234567, 0x3456789012345678, 0x4567890123456789,
0x5678901234567890, 0x6789012345678901, 0x7890123456789012
] } ] }
}
} );

// ... load typelib ...
EXPECT_DL_ERR_OK( dl_context_load_txt_type_library( ctx, json_typelib, sizeof( json_typelib ) - 1 ) );
}
Loading