Mercurial > hg > th-libs
comparison th_string.c @ 706:d289fae3c1a8
Oops, forgot to free elemlens in th_join_string_elems() in no-error case. Ugh.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 27 Apr 2020 00:16:40 +0300 |
parents | 5653e386730b |
children | 57d30f49fe1e |
comparison
equal
deleted
inserted
replaced
705:dee28d507da7 | 706:d289fae3c1a8 |
---|---|
657 | 657 |
658 | 658 |
659 int th_join_string_elems(th_char_t **str, const th_strelems_t *ctx, const th_char_t *sep) | 659 int th_join_string_elems(th_char_t **str, const th_strelems_t *ctx, const th_char_t *sep) |
660 { | 660 { |
661 size_t len, n, offs, seplen, *elemlens; | 661 size_t len, n, offs, seplen, *elemlens; |
662 int res = THERR_OK; | |
662 | 663 |
663 if (str == NULL || ctx == NULL || sep == NULL) | 664 if (str == NULL || ctx == NULL || sep == NULL) |
664 return THERR_NULLPTR; | 665 return THERR_NULLPTR; |
665 | 666 |
666 if ((elemlens = th_malloc(ctx->nelems * sizeof(size_t))) == NULL) | 667 if ((elemlens = th_malloc(ctx->nelems * sizeof(size_t))) == NULL) |
675 | 676 |
676 len += 1 + n * seplen; | 677 len += 1 + n * seplen; |
677 | 678 |
678 if ((*str = th_malloc(len)) == NULL) | 679 if ((*str = th_malloc(len)) == NULL) |
679 { | 680 { |
680 th_free(elemlens); | 681 res = THERR_MALLOC; |
681 return THERR_MALLOC; | 682 goto out; |
682 } | 683 } |
683 | 684 |
684 for (offs = n = 0; n < ctx->nelems; n++) | 685 for (offs = n = 0; n < ctx->nelems; n++) |
685 { | 686 { |
686 if (n > 0) | 687 if (n > 0) |
693 offs += elemlens[n]; | 694 offs += elemlens[n]; |
694 } | 695 } |
695 | 696 |
696 (*str)[offs] = 0; | 697 (*str)[offs] = 0; |
697 | 698 |
699 out: | |
700 th_free(elemlens); | |
698 return THERR_OK; | 701 return THERR_OK; |
699 } | 702 } |
700 | 703 |
701 | 704 |
702 int th_join_string(th_char_t **str, th_char_t **elems, const size_t nelems, const th_char_t *sep) | 705 int th_join_string(th_char_t **str, th_char_t **elems, const size_t nelems, const th_char_t *sep) |