When the dedidacted buffer to store the command payload was added (
c5ae0da62
"MEDIUM: cli: Make a buffer for the command payload"), an bug was
introduced. When the pattern finishing the command payload is found, it is
removed from the buffer. A NULL-bytes is added before it, skipping the
previous newline character.
It worked well in all cases before the commit above, because the commandline
was already parsed and was placed at the beginning of the cmdline buffer.
So, there is always a line before the payload.
Now, the payload is stored in a dedicated buffer. So there is nothing
preceeding it in a buffer. If the payload is empty, we cannot rewind to the
previous line to set the NULL-byte character. We must handle this case to
avoid integer underflow on the payload buffer length.
It is a 3.4-specific bug. No backport needed.
*/
if (len-1 == strlen(appctx->cli_ctx.payload_pat)) {
if (strncmp(str, appctx->cli_ctx.payload_pat, len-1) == 0) {
- /* end of payload was reached, rewind before the previous \n and replace it by a \0 */
- b_sub(buf, strlen(appctx->cli_ctx.payload_pat) + 2);
+ /* end of payload was reached, rewind before the previous \n, if any, and replace it by a \0
+ * Otherwise, the payload is empty, just
+ */
+ if (b_data(buf) > len)
+ b_sub(buf, len+1);
+ else
+ b_sub(buf, len);
*b_tail(buf) = '\0';
appctx->st1 &= ~APPCTX_CLI_ST1_PAYLOAD;
}