Skip to content

Commit

Permalink
fix(string): fix bug caused by releasing pre-allocated memory for string
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Nov 10, 2023
1 parent 85c0429 commit b456991
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
15 changes: 8 additions & 7 deletions include/mln_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ typedef struct {

#define mln_string_free(pstr) \
({\
if ((pstr) != NULL) {\
if ((pstr)->ref-- <= 1) {\
if (!(pstr)->data_ref && (pstr)->data != NULL) {\
if ((pstr)->pool) mln_alloc_free((pstr)->data);\
else free((pstr)->data);\
mln_string_t *__s = (pstr);\
if (__s != NULL) {\
if (__s->ref-- <= 1) {\
if (!__s->data_ref && __s->data != NULL) {\
if (__s->pool) mln_alloc_free(__s->data);\
else free(__s->data);\
}\
if ((pstr)->pool) mln_alloc_free((pstr));\
else free((pstr));\
if (__s->pool) mln_alloc_free(__s);\
else free(__s);\
}\
}\
})
Expand Down
3 changes: 2 additions & 1 deletion src/mln_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright (C) Niklaus F.Schen.
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
Expand Down Expand Up @@ -169,7 +170,7 @@ mln_string_t *mln_string_pool_alloc(mln_alloc_t *pool, mln_s32_t size)
}
s->len = size;
s->data_ref = 0;
s->pool = 0;
s->pool = 1;
s->ref = 1;
return s;
}
Expand Down

0 comments on commit b456991

Please sign in to comment.