summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog3
-rw-r--r--generic/tclStringObj.c44
2 files changed, 24 insertions, 23 deletions
diff --git a/ChangeLog b/ChangeLog
index 803ee95..c211fac 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,9 @@
Tcl_GetRange, and TclStringObjReverse to use the new macro, and
to more simply and clearly split the cases depending on whether
a valid unicode rep is present or needs to be created.
+ New utility routine UnicodeLength(), to compute the length of unicode
+ buffer arguments when no length is passed in, with built-in
+ overflow protection included. Update three callers to use it.
* generic/tclInt.h: New macro TclNumUtfChars meant to be a faster
replacement for a full Tcl_NumUtfChars() call when the string has all
diff --git a/generic/tclStringObj.c b/generic/tclStringObj.c
index f6c3bc8..c1f7e64 100644
--- a/generic/tclStringObj.c
+++ b/generic/tclStringObj.c
@@ -33,7 +33,7 @@
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tclStringObj.c,v 1.102 2009/02/13 03:22:52 dgp Exp $ */
+ * RCS: @(#) $Id: tclStringObj.c,v 1.103 2009/02/13 04:01:46 dgp Exp $ */
#include "tclInt.h"
#include "tommath.h"
@@ -950,6 +950,23 @@ Tcl_SetUnicodeObj(
SetUnicodeObj(objPtr, unicode, numChars);
}
+static int
+UnicodeLength(
+ const Tcl_UniChar *unicode)
+{
+ int numChars = 0;
+
+ if (unicode) {
+ while (numChars >= 0 && unicode[numChars] != 0) {
+ numChars++;
+ }
+ }
+ if (numChars < 0) {
+ Tcl_Panic("max length for a Tcl value (%d chars) exceeded", INT_MAX);
+ }
+ return numChars;
+}
+
static void
SetUnicodeObj(
Tcl_Obj *objPtr, /* The object to set the string of. */
@@ -962,12 +979,7 @@ SetUnicodeObj(
size_t uallocated;
if (numChars < 0) {
- numChars = 0;
- if (unicode) {
- while (unicode[numChars] != 0) {
- numChars++;
- }
- }
+ numChars = UnicodeLength(unicode);
}
/*
@@ -1296,12 +1308,7 @@ AppendUnicodeToUnicodeRep(
size_t numChars;
if (appendNumChars < 0) {
- appendNumChars = 0;
- if (unicode) {
- while (unicode[appendNumChars] != 0) {
- appendNumChars++;
- }
- }
+ appendNumChars = UnicodeLength(unicode);
}
if (appendNumChars == 0) {
return;
@@ -2860,16 +2867,7 @@ ExtendStringRepWithUnicode(
String *stringPtr = GET_STRING(objPtr);
if (numChars < 0) {
- numChars = 0;
- if (unicode) {
- while (numChars >= 0 && unicode[numChars] != 0) {
- numChars++;
- }
- if (numChars < 0) {
- Tcl_Panic("max length for a Tcl value (%d chars) exceeded",
- INT_MAX);
- }
- }
+ numChars = UnicodeLength(unicode);
}
if (numChars == 0) {