LCOV - code coverage report
Current view: top level - src/tds/unittests - corrupt.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 46 51 90.2 %
Date: 2026-11-07 23:58:45 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2015  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /*
      21             :  * Check sending integer till they overlap a packet.
      22             :  * This is to check if an integer can spread in multiple
      23             :  * packets. The problem raise from internal implementation
      24             :  * and how is handled the overlapping.
      25             :  */
      26             : #include "common.h"
      27             : #include <freetds/tds/checks.h>
      28             : 
      29             : static const char select_query[] = "\nselect 'test'";
      30             : 
      31             : static void
      32          10 : unfinished_query_test(TDSSOCKET *tds)
      33             : {
      34             :         int char_len;
      35             :         char *buf, *p;
      36             :         int i, len;
      37             :         union
      38             :         {
      39             :                 TDS_USMALLINT si;
      40             :                 TDS_UINT i;
      41             :                 TDS_INT8 i8;
      42             :                 char buf[8];
      43             :         } conv;
      44             : 
      45          10 :         if (IS_TDS72_PLUS(tds->conn))
      46             :                 return;
      47             : 
      48           6 :         tds_init_write_buf(tds);
      49             : 
      50             :         /* try to build an invalid (unfinished) query split in two packets */
      51           6 :         char_len = IS_TDS7_PLUS(tds->conn) ? 2 : 1;
      52           6 :         buf = tds_new0(char, tds->out_buf_max + 200);
      53             : 
      54           6 :         memset(buf, '-', tds->out_buf_max + 200);
      55           6 :         strcpy(buf + (tds->out_buf_max - 8) / char_len - strlen(select_query) + 1, select_query);
      56           6 :         memset(strchr(buf, 0), 0, 16);
      57             : 
      58             :         /* convert if needed */
      59           6 :         len = (int) strlen(buf);
      60       12274 :         for (i = len; --i >= 0;) {
      61       12262 :                 char c = buf[i];
      62             : 
      63       12262 :                 buf[i * char_len + 0] = c;
      64       12262 :                 if (IS_TDS7_PLUS(tds->conn))
      65        8180 :                         buf[i * char_len + 1] = 0;
      66             :         }
      67           6 :         len *= char_len;
      68             : 
      69             :         /* send the query using tds_put_int8, not aligned */
      70           6 :         tds->out_flag = TDS_QUERY;
      71           6 :         if (tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
      72           0 :                 exit(1);
      73           6 :         p = buf;
      74           6 :         memcpy(conv.buf, p, 2);
      75             : #ifdef WORDS_BIGENDIAN
      76             :         tds_swap_bytes(conv.buf, 2);
      77             : #endif
      78           6 :         tds_put_smallint(tds, conv.si);
      79           6 :         p += 2;
      80        2560 :         for (; p < buf + len; p += 8) {
      81        2554 :                 CHECK_TDS_EXTRA(tds);
      82        2554 :                 memcpy(conv.buf, p, 8);
      83             : #ifdef WORDS_BIGENDIAN
      84             :                 tds_swap_bytes(conv.buf, 8);
      85             : #endif
      86        2554 :                 tds_put_int8(tds, conv.i8);
      87             :         }
      88           6 :         tds_flush_packet(tds);
      89           6 :         tds_set_state(tds, TDS_PENDING);
      90             : 
      91             :         /* check result was fine */
      92           6 :         if (TDS_FAILED(tds_process_simple_query(tds))) {
      93           0 :                 fprintf(stderr, "Error in prepared query\n");
      94           0 :                 exit(1);
      95             :         }
      96           6 :         free(buf);
      97             : }
      98             : 
      99          10 : TEST_MAIN()
     100             : {
     101             :         TDSLOGIN *login;
     102             :         TDSSOCKET *tds;
     103             :         int ret;
     104          10 :         int verbose = 0;
     105             :         TDS_INT8 i8;
     106             :         unsigned limit;
     107             : 
     108          10 :         printf("%s: Testing login, logout\n", __FILE__);
     109          10 :         ret = try_tds_login(&login, &tds, __FILE__, verbose);
     110          10 :         if (ret != TDS_SUCCESS) {
     111           0 :                 fprintf(stderr, "try_tds_login() failed\n");
     112           0 :                 return 1;
     113             :         }
     114             : 
     115          10 :         unfinished_query_test(tds);
     116             : 
     117          10 :         tds->out_flag = TDS_QUERY;
     118          10 :         if (tds_set_state(tds, TDS_WRITING) != TDS_WRITING) {
     119             :                 return 1;
     120             :         }
     121             : 
     122          10 :         tds_put_n(tds, "aaa", 3);
     123          10 :         limit = tds->out_buf_max / 8 + 100;
     124        5618 :         for (i8 = 0; i8 < limit; ++i8) {
     125        5608 :                 CHECK_TDS_EXTRA(tds);
     126        5608 :                 tds_put_int8(tds, i8);
     127             :         }
     128             : 
     129          10 :         tds_send_cancel(tds);
     130          10 :         tds_process_simple_query(tds);
     131             : 
     132          10 :         try_tds_logout(login, tds, verbose);
     133          10 :         return 0;
     134             : }

Generated by: LCOV version 1.13