[freetds] Problem with src/tds/write.c

Thompson, William bill.d.thompson at baml.com
Tue Feb 2 09:20:10 EST 2016

Hi All,

I'm investigating an issue which is affecting some of my processes which use the db-library bcp API.
I'm getting an error back from SQL Server: "The incoming tabular data stream (TDS) protocol stream is incorrect. The stream ended unexpectedly."
After debugging it, It turns out we have an edge case involving some of the functions in src/tds/write.c

The functions tds_put_int8, tds_put_int and tds_put_smallint do not check that there is sufficient space in the packet buffer for their respective data types.
As a consequence, data gets dropped if it happens that one of these data types is being added to the packet buffer when the packet size is reached.

Having looked at the functions, I think I'm going to change these lines (which occur in all the functions):

        if (tds->out_pos >= tds->out_buf_max)
                tds_write_packet(tds, 0x0);

to be:

        if (tds->out_pos  + sizeof(TDS_INT) >= tds->out_buf_max)   /* or TDS_INT8 or TDS_SMALLINT */
                tds_write_packet(tds, 0x0);

If I do this there will be cases where the packet (although not a final packet) is smaller than the maximum size,
but I REALLY don't want to go anywhere near those TDS_PUT_UA* macros. :)
Incidentally, what is the idea behind this TDS_ADDITIONAL_SPACE macro and all those TDS_PUT macros? Life used to be much simpler (sigh), e.g.

<old code>
Int tds_put_smallint(TDSSOCKET * tds, TDS_SMALLINT si)
    if (tds->emul_little_endian) {
        tds_put_byte(tds, si & 0x000000FF);
        tds_put_byte(tds, (si & 0x0000FF00) >> 8);
        return 0;
    return tds_put_n(tds, (const unsigned char *) &si, sizeof(TDS_SMALLINT));
<\old code>


This message, and any attachments, is for the intended recipient(s) only, may contain information that is privileged, confidential and/or proprietary and subject to important terms and conditions available at http://www.bankofamerica.com/emaildisclaimer.   If you are not the intended recipient, please delete this message.

More information about the FreeTDS mailing list