Index: awk.h =================================================================== RCS file: /cvsroot/xmlgawk/xmlgawk/awk.h,v retrieving revision 1.30 diff -b -u -p -r1.30 awk.h --- awk.h 14 Mar 2006 21:59:28 -0000 1.30 +++ awk.h 11 Jul 2006 14:56:49 -0000 @@ -1217,8 +1217,12 @@ extern int parse_escape P((const char ** #ifdef MBS_SUPPORT extern NODE *str2wstr P((NODE *n, size_t **ptr)); #define force_wstring(n) str2wstr(n, NULL) +extern void release_wstr P((NODE *n)); +#define RELEASE_WSTR(N) release_wstr(N); extern const wchar_t *wstrstr P((const wchar_t *haystack, size_t hs_len, const wchar_t *needle, size_t needle_len)); extern const wchar_t *wcasestrstr P((const wchar_t *haystack, size_t hs_len, const wchar_t *needle, size_t needle_len)); +#else /* !MBS_SUPPORT */ +#define RELEASE_WSTR(N) /* no-op */ #endif /* re.c */ extern Regexp *make_regexp P((const char *s, size_t len, int ignorecase, int dfa)); Index: node.c =================================================================== RCS file: /cvsroot/xmlgawk/xmlgawk/node.c,v retrieving revision 1.4 diff -b -u -p -r1.4 node.c --- node.c 6 Mar 2006 22:37:19 -0000 1.4 +++ node.c 11 Jul 2006 14:56:49 -0000 @@ -214,15 +214,7 @@ format_val(const char *format, int index no_malloc: s->stref = 1; s->flags |= STRCUR; -#if defined MBS_SUPPORT - if ((s->flags & WSTRCUR) != 0) { - assert(s->wstptr != NULL); - free(s->wstptr); - s->wstptr = NULL; - s->wstlen = 0; - s->flags &= ~WSTRCUR; - } -#endif + RELEASE_WSTR(s) return s; } @@ -508,20 +500,13 @@ unref(register NODE *tmp) return; } free(tmp->stptr); -#if defined MBS_SUPPORT - if (tmp->wstptr != NULL) { - assert((tmp->flags & WSTRCUR) != 0); - free(tmp->wstptr); - } - tmp->flags &= ~WSTRCUR; - tmp->wstptr = NULL; - tmp->wstlen = 0; -#endif + RELEASE_WSTR(tmp) } freenode(tmp); return; } if ((tmp->flags & FIELD) != 0) { + RELEASE_WSTR(tmp) freenode(tmp); return; } @@ -685,6 +670,28 @@ isnondecimal(const char *str, int use_lo } #if defined MBS_SUPPORT + +void +release_wstr(NODE *n) +{ + if ((n->flags & WSTRCUR) != 0) { + assert(n->wstptr != NULL); + free(n->wstptr); + n->wstptr = NULL; + n->wstlen = 0; + n->flags &= ~WSTRCUR; + } +#if 0 + /* I suspect this is generally not safe (to check wstptr or wstlen + fields if the WSTRCUR is not set), so it's commented out. + Would it be valid to check these if the STRCUR flag is set? */ + else if ((n->flags & STRCUR) != 0) { + assert(n->wstptr == NULL); + assert(n->wstlen == 0); + } +#endif +} + /* str2wstr --- convert a multibyte string to a wide string */ NODE * @@ -702,12 +709,11 @@ str2wstr(NODE *n, size_t **ptr) return n; /* otherwise fall through and recompute to fill in the array */ - } - if (n->wstptr != NULL) { + assert(n->wstptr != NULL); free(n->wstptr); - n->wstptr = NULL; - n->wstlen = 0; + /* no need to reset wstptr and wstlen since they will + be set below */ } /* Index: builtin.c =================================================================== RCS file: /cvsroot/xmlgawk/xmlgawk/builtin.c,v retrieving revision 1.9 diff -b -u -p -r1.9 builtin.c --- builtin.c 8 Mar 2006 23:39:59 -0000 1.9 +++ builtin.c 11 Jul 2006 14:56:49 -0000 @@ -2527,6 +2527,7 @@ sub_common(NODE *tree, long how_many, in free(t->stptr); t->stptr = buf; t->stlen = textlen; + RELEASE_WSTR(t) free_temp(s); if (matches > 0 && lhs) { Index: eval.c =================================================================== RCS file: /cvsroot/xmlgawk/xmlgawk/eval.c,v retrieving revision 1.17 diff -b -u -p -r1.17 eval.c --- eval.c 6 Jul 2006 19:10:38 -0000 1.17 +++ eval.c 11 Jul 2006 14:56:50 -0000 @@ -1176,6 +1176,7 @@ r_tree_eval(register NODE *tree, int isc memcpy(l->stptr + l->stlen, r->stptr, r->stlen); l->stlen += r->stlen; l->stptr[l->stlen] = '\0'; + RELEASE_WSTR(l) } else { char *nval; size_t nlen = l->stlen + r->stlen + 2;