Overview
| Artifact ID: | 44546b91cfac5f56d0acf8a0ccedf0e37cbed905620afa701d143d98de031452 |
|---|---|
| Ticket: | 73bb42fb3f35cd613af6fcea465e35bbfd352216
Panic "Buffer Underflow, BUFFER_PADDING not enough" |
| User & Date: | oehhar on 2025-01-14 17:24:22 |
Changes
- icomment:
generic/tclio.c Line 6574 <verbatim> /* * Space is made at the beginning of the buffer to copy the * previous unused bytes there. Check first if the buffer we are * using actually has enough space at its beginning for the data * we are copying. Because if not we will write over the buffer * management information, especially the 'nextPtr'. * * Note that the BUFFER_PADDING (See AllocChannelBuffer) is used * to prevent exactly this situation. I.e. it should never happen. * Therefore it is ok to panic should it happen despite the * precautions. */ if (nextPtr->nextRemoved < srcLen) { Tcl_Panic("Buffer Underflow, BUFFER_PADDING not enough"); } </verbatim> Current values: * srclen = 17 * nextPtr->nextRemoved = 16 * src: "\réXXXXXXXXXXXXXXX" Is the final assertion. The way to this result: First, "XXXXXXX\r" is received and decoded without issue. We fall in Case 2 of line 6401, as the "\n" is missing in the translation. In the next round: <verbatim> code = Tcl_ExternalToUtf(NULL, encoding, src, srcLen, flags, &statePtr->inputEncodingState, dst, dstLimit, &srcRead, &dstDecoded, &numChars); </verbatim> with * src = "xxxxxxx\r" * srcLen = 8 * srcRead=dstDecoded=numChars=7 returns code = -4: (TCL_CONVERT_NOSPACE) The following test: <verbatim> if (code == TCL_CONVERT_UNKNOWN || code == TCL_CONVERT_SYNTAX || (code == TCL_CONVERT_MULTIBYTE && GotFlag(statePtr, CHANNEL_EOF))) { SetFlag(statePtr, CHANNEL_ENCODING_ERROR); code = TCL_OK; } </verbatim> does not fire as "TCL_CONVERT_NOSPACE" is not handled. Then, the "\r" is not contained in the decoded data and thus, the translation works. The first 7 characters are returned by ReadChars Next round, only a one byte "\r" is decoded and failes due to "\r\n" eol requirement. This is correctly detected in line 6552 and "CHANNEL_NEED_MORE_DATA" is set. Next round, only one byte "\r" is decoded and failes again. This time, more data is available in the next buffer. Thus, the not consumed "\r" is prepended to the next buffer and ReadChars is called again (Line 6583) with "CharsToRead=1", The buffer now contains 9 characters starting from "\r". <verbatim> nextPtr->nextRemoved -= srcLen; memcpy(RemovePoint(nextPtr), src, srcLen); RecycleBuffer(statePtr, bufPtr, 0); statePtr->inQueueHead = nextPtr; Tcl_SetObjLength(objPtr, numBytes); return ReadChars(statePtr, objPtr, charsToRead, factorPtr); </verbatim> Then, the encoding decode (line 6325) is called with all 9 characters and a destination limit of 4. This results in code=-4 (TCL_CONVERT_NOSPACE). and only the "\r" decoded. Due to the flag "ENCODING_CHAR_LIMIT", the output length is limited to dstCharsPtr (numChars on input), which is 1. Then, it is remarked, that we still have characters. The encoding is translated in line 6449. <verbatim> Tcl_ExternalToUtf(NULL, encoding, src, srcLen, (statePtr->inputEncodingFlags | TCL_ENCODING_NO_TERMINATE), &statePtr->inputEncodingState, buffer, sizeof(buffer), &read, &decoded, &count); </verbatim> with the parameters: * src: \r\xe9XXXXXX * srclen = 9 This returns -1 (TCL_CONVERT_SYNTAX) which is ignored. The following handling handles \r\n and eof, but not the error case. Again, we return with "NEED_MORE_DATA" flag. I think, by adding a result check to Tcl_ExternalToUtf would probably the right moment to react on this. But lets continue. ReadChar is called again for 1 character. Tcl_ExternalToUtf is called again with 1 char output limit, resulting in -4 Again, additional characters are decoded using Tcl_ExternalToUtf. The resulting error is not checked again. In this round, it is tried to add the currently 9 characters to the beginning of the next buffer and ReadChar is called with this extended buffer (line 6582). Again, Tcl_ExternalToUtf is now called with 17 source characters and only 1 output character resulting in -4. Again, result is "NEED_MORE_DATA". In the next round, the panic condition fires, as it is tried to put the 9 characters in front of the next buffer, but there is not sufficient space. In a nutchell: * As the "\n" is missing, ReadChar returns with "NEED_MORE_DATA" * This additional data is accumulated in a 24 byte buffer, until there is no space and the panic fires. * The root cause is a non checked encoding conversion in line 6449. The purpose is only to read a "\n", but an encoding error arises. This error is not checked and the following handling ignores the error. There may be many solutions: * catch the encoding error there and handle it * think about the whole procedure and get it somewhere else Headbloat for today. Thanks for all, Harald - login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"