summaryrefslogtreecommitdiffstats
path: root/generic/tclStringObj.c
diff options
context:
space:
mode:
authordgp <dgp@users.sourceforge.net>2009-02-13 04:01:46 (GMT)
committerdgp <dgp@users.sourceforge.net>2009-02-13 04:01:46 (GMT)
commitcff88797e19e9d9e0a3fd02e8936ae008e43db36 (patch)
tree745b4e1c53cf07dca81f882db72b8dd58d1cc9bc /generic/tclStringObj.c
parentf819f7990a8794c9429eb83b8f912950b85d0a91 (diff)
downloadtcl-cff88797e19e9d9e0a3fd02e8936ae008e43db36.zip
tcl-cff88797e19e9d9e0a3fd02e8936ae008e43db36.tar.gz
tcl-cff88797e19e9d9e0a3fd02e8936ae008e43db36.tar.bz2
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.
Diffstat (limited to 'generic/tclStringObj.c')
-rw-r--r--generic/tclStringObj.c44
1 files changed, 21 insertions, 23 deletions
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) {