606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
-
+
-
-
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
+
+
-
-
-
+
-
+
+
+
+
+
-
-
+
+
-
-
-
-
+
+
+
-
-
-
-
-
+
-
-
+
-
-
-
-
-
+
-
+
-
-
+
+
-
|
/*
*----------------------------------------------------------------------
*
* TclAppendBytesToByteArray --
*
* This function appends an array of bytes to a byte array object. Note
* that the object *must* be unshared, and the array of bytes *must not*
* refer to the object being appended to. Also the caller must have
* refer to the object being appended to.
* already checked that the final length of the bytearray after the
* append operations is complete will not overflow the int range.
*
* Results:
* None.
*
* Side effects:
* Allocates enough memory for an array of bytes of the requested total
* size, or possibly larger. [Bug 2992970]
*
*----------------------------------------------------------------------
*/
void
TclAppendBytesToByteArray(
Tcl_Obj *objPtr,
const unsigned char *bytes,
int len)
{
ByteArray *byteArrayPtr;
int needed;
if (Tcl_IsShared(objPtr)) {
Tcl_Panic("%s called with shared object","TclAppendBytesToByteArray");
}
if (len < 0) {
Tcl_Panic("%s must be called with definite number of bytes to append",
"TclAppendBytesToByteArray");
}
if (len == 0) {
/* Append zero bytes is a no-op. */
return;
}
if (objPtr->typePtr != &tclByteArrayType) {
SetByteArrayFromAny(NULL, objPtr);
}
byteArrayPtr = GET_BYTEARRAY(objPtr);
if (len > INT_MAX - byteArrayPtr->used) {
Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX);
}
needed = byteArrayPtr->used + len;
/*
* If we need to, resize the allocated space in the byte array.
*/
if (byteArrayPtr->used + len > byteArrayPtr->allocated) {
if (needed > byteArrayPtr->allocated) {
unsigned int attempt, used = byteArrayPtr->used;
ByteArray *tmpByteArrayPtr = NULL;
ByteArray *ptr = NULL;
int attempt;
attempt = byteArrayPtr->allocated;
if (attempt < 1) {
if (needed <= INT_MAX/2) {
/*
* No allocated bytes, so must be none used too. We use this
* method to calculate how many bytes to allocate because we can
* end up with a zero-length buffer otherwise, when doubling can
* cause trouble. [Bug 3067036]
*/
attempt = len + 1;
/* Try to allocate double the total space that is needed. */
attempt = 2 * needed;
} else {
do {
attempt *= 2;
ptr = attemptckrealloc(byteArrayPtr, BYTEARRAY_SIZE(attempt));
} while (attempt < used+len);
}
if (ptr == NULL) {
/* Try to allocate double the increment that is needed (plus). */
unsigned int limit = INT_MAX - needed;
unsigned int extra = len + TCL_MIN_GROWTH;
int growth = (int) ((extra > limit) ? limit : extra);
if (BYTEARRAY_SIZE(attempt) > BYTEARRAY_SIZE(used)) {
tmpByteArrayPtr = attemptckrealloc(byteArrayPtr,
attempt = needed + growth;
ptr = attemptckrealloc(byteArrayPtr, BYTEARRAY_SIZE(attempt));
BYTEARRAY_SIZE(attempt));
}
if (tmpByteArrayPtr == NULL) {
attempt = used + len;
if (ptr == NULL) {
/* Last chance: Try to allocate exactly what is needed. */
attempt = needed;
if (BYTEARRAY_SIZE(attempt) < BYTEARRAY_SIZE(used)) {
Tcl_Panic("attempt to allocate a bigger buffer than we can handle");
}
tmpByteArrayPtr = ckrealloc(byteArrayPtr,
BYTEARRAY_SIZE(attempt));
ptr = ckrealloc(byteArrayPtr, BYTEARRAY_SIZE(attempt));
}
byteArrayPtr = tmpByteArrayPtr;
byteArrayPtr = ptr;
byteArrayPtr->allocated = attempt;
byteArrayPtr->used = used;
SET_BYTEARRAY(objPtr, byteArrayPtr);
}
/*
* Do the append if there's any point.
*/
if (bytes) {
if (len > 0) {
memcpy(byteArrayPtr->bytes + byteArrayPtr->used, bytes, len);
}
byteArrayPtr->used += len;
TclInvalidateStringRep(objPtr);
byteArrayPtr->used += len;
TclInvalidateStringRep(objPtr);
}
}
/*
*----------------------------------------------------------------------
*
* TclInitBinaryCmd --
*
|