Skip to content

Commit c6f5680

Browse files
committed
Fixing readTextFree bug
1 parent 1525125 commit c6f5680

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/cws_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ int readText(const char **text, size_t *text_len, const char *filename)
143143
inline
144144
void readTextFree(const char **text)
145145
{
146-
if (text) {
146+
if (*text) {
147147
free((void *)(*text));
148148
(*text)=NULL;
149149
}

tests/C/include/pointers_asserts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
void test_pointer_assert();
55
void test_object_assert();
6+
void test_text_reader();
67

78
#define DECLARE_SOAP_INTERNAL struct soap *soap_internal;
89

tests/C/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
int main(int argc, char **argv)
55
{
66
TITLE_MSG("Begin C Witsml 2.1 parser tests ...")
7+
test_text_reader();
78
test_pointer_assert();
89
test_object_assert();
910
end_tests();

tests/C/pointers_assert.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ static void test_pointer_assert_RELEASE(void *p)
2828
cws_config_free(&test_pointer_assert_rel->config);
2929
}
3030

31+
void test_text_reader()
32+
{
33+
#define MSG_SZ 512
34+
const char *msg=malloc(MSG_SZ);
35+
36+
C_ASSERT_NOT_NULL((void *)msg, CTEST_SETTER(
37+
CTEST_TITLE("Testing message text buffer"),
38+
CTEST_ON_ERROR("Was expected NOT NULL at malloc msg pointer"),
39+
CTEST_ON_SUCCESS("Alloc'd msg=%p with size %d", msg, MSG_SZ)
40+
))
41+
42+
memset((void *)msg, 0, MSG_SZ);
43+
44+
readTextFree(&msg);
45+
46+
C_ASSERT_NULL((void *)msg, CTEST_SETTER(
47+
CTEST_TITLE("Testing message text buffer after free"),
48+
CTEST_ON_ERROR("Was expected NULL msg pointer"),
49+
CTEST_ON_SUCCESS("msg pointer dealloc'd")
50+
))
51+
52+
readTextFree(&msg);
53+
54+
C_ASSERT_NULL((void *)msg, CTEST_SETTER(
55+
CTEST_TITLE("Testing double free safe"),
56+
CTEST_ON_ERROR("Was expected NULL msg pointer"),
57+
CTEST_ON_SUCCESS("msg pointer is null")
58+
))
59+
60+
#undef MSG_SZ
61+
}
62+
3163
void test_pointer_assert()
3264
{
3365
int err;

0 commit comments

Comments
 (0)