LCOV - code coverage report
Current view: top level - src/odbc - odbc_checks.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 52 53 98.1 %
Date: 2025-01-18 12:13:41 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2003-2008  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             : #include <config.h>
      20             : 
      21             : #include <stdarg.h>
      22             : #include <stdio.h>
      23             : 
      24             : #if HAVE_STDLIB_H
      25             : #include <stdlib.h>
      26             : #endif /* HAVE_STDLIB_H */
      27             : 
      28             : #if HAVE_STRING_H
      29             : #include <string.h>
      30             : #endif /* HAVE_STRING_H */
      31             : 
      32             : #include <assert.h>
      33             : 
      34             : #include <freetds/odbc.h>
      35             : #include <freetds/utils/string.h>
      36             : 
      37             : #if ENABLE_EXTRA_CHECKS
      38             : 
      39             : /* Check common fields in ODBC structures */
      40             : #define TEST_ATTRIBUTE(t,sa,fa,sb,fb) \
      41             :         TDS_COMPILE_CHECK(t,sizeof(((sa*)0)->fa) == sizeof(((sb*)0)->fb) && TDS_OFFSET(sa,fa) == TDS_OFFSET(sb,fb))
      42             : 
      43             : #define SAME_TDS_CHK_FIELDS(t,struct) \
      44             :         TEST_ATTRIBUTE(t ## _a,TDS_CHK,htype,struct,htype); \
      45             :         TEST_ATTRIBUTE(t ## _b,TDS_CHK,errs, struct,errs); \
      46             :         TEST_ATTRIBUTE(t ## _c,TDS_CHK,mtx,  struct,mtx)
      47             : 
      48             : SAME_TDS_CHK_FIELDS(t1, TDS_ENV);
      49             : SAME_TDS_CHK_FIELDS(t2, TDS_DBC);
      50             : SAME_TDS_CHK_FIELDS(t3, TDS_STMT);
      51             : SAME_TDS_CHK_FIELDS(t4, TDS_DESC);
      52             : 
      53             : void
      54        3580 : odbc_check_env_extra(TDS_ENV * env)
      55             : {
      56        3580 :         assert(env && env->htype == SQL_HANDLE_ENV);
      57        3580 :         assert(env->attr.odbc_version == SQL_OV_ODBC3 || env->attr.odbc_version == SQL_OV_ODBC2);
      58        3580 : }
      59             : 
      60             : void
      61       37573 : odbc_check_dbc_extra(TDS_DBC * dbc)
      62             : {
      63       37573 :         assert(dbc && dbc->htype == SQL_HANDLE_DBC);
      64       37573 : }
      65             : 
      66             : void
      67      307180 : odbc_check_stmt_extra(TDS_STMT * stmt)
      68             : {
      69      307180 :         assert(stmt && stmt->htype == SQL_HANDLE_STMT);
      70             :         /* TODO deep check on connection */
      71      307180 :         assert(stmt->dbc);
      72      307180 :         odbc_check_desc_extra(stmt->ard);
      73      307180 :         odbc_check_desc_extra(stmt->ird);
      74      307180 :         odbc_check_desc_extra(stmt->apd);
      75      307180 :         odbc_check_desc_extra(stmt->ipd);
      76      307180 :         assert(!stmt->prepared_query_is_func || stmt->prepared_query_is_rpc);
      77      307180 :         assert(stmt->param_num <= stmt->param_count + 1);
      78      307180 :         assert(stmt->num_param_rows >= 1);
      79             :         assert(stmt->curr_param_row >= 0);
      80      307180 :         assert(stmt->curr_param_row <= stmt->num_param_rows);
      81      307180 :         if (stmt->prepared_query_is_rpc) {
      82       55460 :                 assert(stmt->prepared_pos == 0 || stmt->prepared_pos <= tds_dstr_len(&stmt->query));
      83             :         } else {
      84      279450 :                 assert(stmt->prepared_pos == 0);
      85             :         }
      86             :         /* TODO assert dbc has this statement in list */
      87      307180 : }
      88             : 
      89             : static void
      90      688886 : odbc_check_drecord(TDS_DESC * desc, struct _drecord *drec)
      91             : {
      92      688886 :         assert(drec->sql_desc_concise_type != SQL_INTERVAL && drec->sql_desc_concise_type != SQL_DATETIME);
      93             : 
      94             :         /* unbound columns have type == 0 */
      95             :         /* TODO test errors on code if type == 0 */
      96      688886 :         if (desc->type == DESC_IPD || desc->type == DESC_IRD) {
      97      485618 :                 assert((drec->sql_desc_type == 0 && drec->sql_desc_concise_type == 0) || odbc_get_concise_sql_type(drec->sql_desc_type, drec->sql_desc_datetime_interval_code) == drec->sql_desc_concise_type);
      98             :         } else {
      99      203268 :                 assert((drec->sql_desc_type == 0 && drec->sql_desc_concise_type == 0) || odbc_get_concise_c_type(drec->sql_desc_type, drec->sql_desc_datetime_interval_code) == drec->sql_desc_concise_type);
     100             :         }
     101      688886 : }
     102             : 
     103             : void
     104     1290104 : odbc_check_desc_extra(TDS_DESC * desc)
     105             : {
     106             :         int i;
     107             : 
     108     1290104 :         assert(desc && desc->htype == SQL_HANDLE_DESC);
     109     1290104 :         assert(desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_AUTO || desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_USER);
     110     1290104 :         assert((desc->type != DESC_IPD && desc->type != DESC_IRD) || desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_AUTO);
     111     1290104 :         assert(desc->parent && (IS_HSTMT(desc->parent) || IS_HDBC(desc->parent)));
     112      688886 :         for (i = 0; i < desc->header.sql_desc_count; ++i) {
     113      688886 :                 odbc_check_drecord(desc, &desc->records[i]);
     114             :         }
     115     1290104 : }
     116             : 
     117             : void
     118      191509 : odbc_check_struct_extra(void *p)
     119             : {
     120      191509 :         const int invalid_htype = 0;
     121             : 
     122      191509 :         switch (((TDS_CHK *) p)->htype) {
     123        1340 :         case SQL_HANDLE_ENV:
     124        1340 :                 odbc_check_env_extra((TDS_ENV *) p);
     125        1340 :                 break;
     126       18887 :         case SQL_HANDLE_DBC:
     127       18887 :                 odbc_check_dbc_extra((TDS_DBC *) p);
     128       18887 :                 break;
     129      169112 :         case SQL_HANDLE_STMT:
     130      169112 :                 odbc_check_stmt_extra((TDS_STMT *) p);
     131      169112 :                 break;
     132        2170 :         case SQL_HANDLE_DESC:
     133        2170 :                 odbc_check_desc_extra((TDS_DESC *) p);
     134        2170 :                 break;
     135             :         default:
     136           0 :                 assert(invalid_htype);
     137             :         }
     138      191509 : }
     139             : 
     140             : #endif /* ENABLE_EXTRA_CHECKS */

Generated by: LCOV version 1.13