Skip to content

Commit 0e2dbb2

Browse files
authored
UTF8 workaround memory optimization
Found by: mortmann Patch by: thommey
1 parent e535d43 commit 0e2dbb2

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/tcl.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -693,18 +693,19 @@ int needs_unicodesup(const char *str)
693693
* - encode high/low as 3-byte utf-8 strings
694694
* the length of the result could be len/4*6 bytes long, so
695695
* to avoid frequent reallocation/string appending, a temporary buffer is used
696-
* for long strings (>512 characters, which does not apply to IRC lines) and assembled to a Tcl_DString
697-
* for short strings the 512 character buffer is enough
696+
* for long strings (>512 characters, which does not apply to IRC lines) chunks are assembled to a Tcl_DString
697+
* for short strings the 512/4*6 character buffer is enough
698698
*/
699699
Tcl_Obj *egg_string_unicodesup_surrogate(const char *oldstr, int len)
700700
{
701-
int stridx = 0, bufidx = 0;
702-
char buf[512];
701+
int stridx = 0, bufidx = 0, use_dstring = 0;
702+
char buf[768];
703703
Tcl_DString ds;
704704
Tcl_Obj *result;
705705

706706
/* chunked */
707-
if (len > sizeof buf) {
707+
if (len > 512) {
708+
use_dstring = 1;
708709
Tcl_DStringInit(&ds);
709710
}
710711

@@ -741,12 +742,12 @@ Tcl_Obj *egg_string_unicodesup_surrogate(const char *oldstr, int len)
741742
buf[bufidx++] = oldstr[stridx++];
742743
}
743744
}
744-
if (len > sizeof buf && bufidx > sizeof buf - 6) {
745+
if (use_dstring && bufidx > sizeof buf - 6) {
745746
Tcl_DStringAppend(&ds, buf, bufidx);
746747
bufidx = 0;
747748
}
748749
}
749-
if (len > sizeof buf && bufidx) {
750+
if (use_dstring && bufidx) {
750751
Tcl_DStringAppend(&ds, buf, bufidx);
751752
result = Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
752753
Tcl_DStringFree(&ds);

0 commit comments

Comments
 (0)