Skip to content

Commit 53c5da7

Browse files
committed
Remove tail recursion in dynType_alloc and add more parsing tests.
1 parent 2422879 commit 53c5da7

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

libs/dfi/gtest/src/dyn_type_ei_tests.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,15 @@ TEST_F(DynTypeErrorInjectionTestSuite, ParseEnumTypeErrors) {
108108
ASSERT_NE(0, rc);
109109
celix_err_printErrors(stderr, nullptr, nullptr);
110110
}
111+
112+
TEST_F(DynTypeErrorInjectionTestSuite, AllocateErrors) {
113+
celix_autoptr(dyn_type) type = NULL;
114+
int rc = 0;
115+
rc = dynType_parseWithStr("#v1=0;#v2=1;E", NULL, NULL, &type);
116+
ASSERT_EQ(0, rc);
117+
celix_ei_expect_calloc((void*)dynType_alloc, 0, nullptr);
118+
void* buf = nullptr;
119+
rc = dynType_alloc(type, &buf);
120+
ASSERT_NE(0, rc);
121+
ASSERT_STREQ("Error allocating memory for type 'E'", celix_err_popLastError());
122+
}

libs/dfi/gtest/src/dyn_type_tests.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ TEST_F(DynTypeTests, ComplexTypeMissingClosingBrace) {
326326
ASSERT_STREQ("Error parsing complex type, expected '}'", celix_err_popLastError());
327327
}
328328

329+
TEST_F(DynTypeTests, ComplexTypeWithMoreNamesThanFields) {
330+
dyn_type *type = NULL;
331+
auto rc = dynType_parseWithStr(R"({II a b c})", nullptr, nullptr, &type);
332+
ASSERT_NE(0, rc);
333+
ASSERT_STREQ("Error parsing complex type, expected '}'", celix_err_popLastError());
334+
}
335+
329336
TEST_F(DynTypeTests, SchemaEndsWithoutNullTerminator) {
330337
dyn_type *type = NULL;
331338
//ends with '-'
@@ -365,8 +372,14 @@ TEST_F(DynTypeTests, MetaInfoMissingValue) {
365372

366373
TEST_F(DynTypeTests, ParseNestedTypeFailed) {
367374
dyn_type *type = NULL;
375+
int rc;
376+
//missing name
377+
rc = dynType_parseWithStr(R"(T={DD a b};)", nullptr, nullptr, &type);
378+
ASSERT_NE(0, rc);
379+
ASSERT_STREQ("Parsed empty name", celix_err_popLastError());
380+
368381
//missing '='
369-
auto rc = dynType_parseWithStr(R"(Ttype)", nullptr, nullptr, &type);
382+
rc = dynType_parseWithStr(R"(Ttype)", nullptr, nullptr, &type);
370383
ASSERT_EQ(3, rc);
371384
celix_err_printErrors(stderr, nullptr, nullptr);
372385

@@ -383,8 +396,13 @@ TEST_F(DynTypeTests, ParseNestedTypeFailed) {
383396

384397
TEST_F(DynTypeTests, ParseReferenceFailed) {
385398
dyn_type *type = NULL;
399+
int rc;
400+
// missing name
401+
rc = dynType_parseWithStr(R"(Ttype={DD a b};l;)", nullptr, nullptr, &type);
402+
ASSERT_NE(0, rc);
403+
ASSERT_STREQ("Parsed empty name", celix_err_popLastError());
386404
//missing ';'
387-
auto rc = dynType_parseWithStr(R"(Ttype={DD a b};ltype)", nullptr, nullptr, &type);
405+
rc = dynType_parseWithStr(R"(Ttype={DD a b};ltype)", nullptr, nullptr, &type);
388406
ASSERT_EQ(3, rc);
389407
celix_err_printErrors(stderr, nullptr, nullptr);
390408
//missing ';'

libs/dfi/src/dyn_type.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,7 @@ static void dynType_clear(dyn_type* type) {
427427
while (entry != NULL) {
428428
tmp = entry;
429429
entry = TAILQ_NEXT(entry, entries);
430-
if (tmp->type != NULL) {
431-
dynType_destroy(tmp->type);
432-
tmp->type = NULL;
433-
}
430+
dynType_destroy(tmp->type);
434431
free(tmp);
435432
}
436433

@@ -499,21 +496,19 @@ static void dynType_clearTypedPointer(dyn_type* type) {
499496
}
500497

501498
int dynType_alloc(const dyn_type* type, void** bufLoc) {
502-
int status = OK;
499+
const dyn_type* current = type;
503500

504-
if (type->type == DYN_TYPE_REF) {
505-
status = dynType_alloc(type->ref.ref, bufLoc);
506-
} else {
507-
void* inst = calloc(1, type->ffiType->size);
508-
if (inst != NULL) {
509-
*bufLoc = inst;
510-
} else {
511-
status = MEM_ERROR;
512-
celix_err_pushf("Error allocating memory for type '%c'", type->descriptor);
513-
}
501+
while (current->type == DYN_TYPE_REF) {
502+
current = current->ref.ref;
503+
}
504+
void* inst = calloc(1, current->ffiType->size);
505+
if (inst == NULL) {
506+
celix_err_pushf("Error allocating memory for type '%c'", current->descriptor);
507+
return MEM_ERROR;
514508
}
509+
*bufLoc = inst;
515510

516-
return status;
511+
return OK;
517512
}
518513

519514

libs/dfi/src/dyn_type_common.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ dyn_type* dynType_findType(dyn_type *type, char *name) {
5353

5454
ffi_type * dynType_ffiType(dyn_type * type) {
5555
if (type->type == DYN_TYPE_REF) {
56-
if (type->ref.ref == NULL) {
57-
celix_err_pushf("Error. Ref for %s is not (yet) initialized", type->name);
58-
return NULL;
59-
}
6056
return type->ref.ref->ffiType;
6157
}
6258
return type->ffiType;

0 commit comments

Comments
 (0)