Posted to tcl by gps at Sun Feb 15 21:06:58 GMT 2009view raw

  1. Before the realloc branch I'd try something like this pattern:
  2. char isSame = 0; /*Tcl_Bool would be nice. */
  3. int oldNumChars = 0;
  4. if (stringPtr->unicode == unicode) {
  5. isSame = 1;
  6. oldNumChar = stringPtr->numChars;
  7. }
  8.  
  9.  
  10. /* [realloc as needed] */
  11.  
  12. if(isSame) {
  13. memcpy(stringPtr->unicode + oldNumChars, stringPtr->unicode, appendNumChars * sizeof(*unicode));
  14. } else {
  15. memcpy(...);
  16. }
  17.  
  18. That assumes that unicode will have the same offset/pointer as the head of the Tcl_Obj. Is that valid?
  19.  
  20.  
  21. Regarding this:
  22. /* TODO: overflow check */
  23. numChars = stringPtr->numChars + appendNumChars;
  24.  
  25.  
  26. You can do:
  27.  
  28. if((INT_MAX - stringPtr->numChars) < appendNumChars)
  29. /* handle overflow*/
  30.