Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2004-2011 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 : #undef NDEBUG
22 :
23 : #include <stdarg.h>
24 : #include <stdio.h>
25 :
26 : #if HAVE_STDLIB_H
27 : #include <stdlib.h>
28 : #endif /* HAVE_STDLIB_H */
29 :
30 : #if HAVE_STRING_H
31 : #include <string.h>
32 : #endif /* HAVE_STRING_H */
33 :
34 : #include <assert.h>
35 :
36 : #include <freetds/tds.h>
37 : #include <freetds/tds/convert.h>
38 : #include <freetds/utils/string.h>
39 : #include <freetds/tds/checks.h>
40 :
41 : #if ENABLE_EXTRA_CHECKS
42 : static void
43 11772644 : tds_check_packet_extra(const TDSPACKET * packet)
44 : {
45 11772644 : assert(packet);
46 11772644 : for (; packet; packet = packet->next) {
47 5933088 : assert(tds_packet_get_data_start(packet) == 0 || tds_packet_get_data_start(packet) == sizeof(TDS72_SMP_HEADER));
48 11772644 : assert(packet->data_len + tds_packet_get_data_start(packet) <= packet->capacity);
49 : }
50 11772644 : }
51 :
52 : void
53 5886322 : tds_check_tds_extra(const TDSSOCKET * tds)
54 : {
55 5886322 : const int invalid_state = 0;
56 : TDS_UINT i;
57 5886322 : TDSDYNAMIC *cur_dyn = NULL;
58 5886322 : TDSCURSOR *cur_cursor = NULL;
59 :
60 5886322 : assert(tds);
61 :
62 : /* test state and connection */
63 5886322 : switch (tds->state) {
64 : case TDS_DEAD:
65 : case TDS_WRITING:
66 : case TDS_SENDING:
67 : case TDS_PENDING:
68 : case TDS_IDLE:
69 : case TDS_READING:
70 : break;
71 : default:
72 0 : assert(invalid_state);
73 : }
74 :
75 2966544 : assert(tds->conn);
76 :
77 : #if ENABLE_ODBC_MARS
78 2966544 : assert(tds->sid < tds->conn->num_sessions);
79 2966544 : assert(tds->sid == 0 || tds->conn->mars);
80 2966544 : if (tds->state != TDS_DEAD)
81 2944273 : assert(!TDS_IS_SOCKET_INVALID(tds_get_s(tds)));
82 : #else
83 2919778 : assert(tds->state == TDS_DEAD || !TDS_IS_SOCKET_INVALID(tds_get_s(tds)));
84 2919778 : assert(tds->state != TDS_DEAD || TDS_IS_SOCKET_INVALID(tds_get_s(tds)));
85 : #endif
86 :
87 : /* test env */
88 5886322 : tds_check_env_extra(&tds->conn->env);
89 :
90 : /* test buffers and positions */
91 5886322 : assert(tds->send_packet != NULL);
92 5886322 : assert(tds->send_packet->next == NULL);
93 5886322 : tds_check_packet_extra(tds->send_packet);
94 5886322 : tds_check_packet_extra(tds->recv_packet);
95 :
96 : #if ENABLE_ODBC_MARS
97 2966544 : if (tds->conn->send_packets)
98 76180 : assert(tds->conn->send_pos <= tds->conn->send_packets->data_len + tds->conn->send_packets->data_start);
99 2966544 : if (tds->conn->recv_packet)
100 22 : assert(tds->conn->recv_pos <= tds->conn->recv_packet->data_len + tds->conn->recv_packet->data_start);
101 2966544 : if (tds->conn->mars)
102 423216 : assert(tds->send_packet->data_start == sizeof(TDS72_SMP_HEADER));
103 : else
104 2543328 : assert(tds->send_packet->data_start == 0);
105 : #endif
106 :
107 5886322 : assert(tds->in_pos <= tds->in_len);
108 5886322 : assert(tds->in_len <= tds->recv_packet->capacity);
109 : /* TODO remove blocksize from env and use out_len ?? */
110 : /* assert(tds->out_pos <= tds->out_len); */
111 : /* assert(tds->out_len == 0 || tds->out_buf != NULL); */
112 5886322 : assert(tds->send_packet->capacity >= tds->out_buf_max + TDS_ADDITIONAL_SPACE);
113 5886322 : assert(tds->out_buf == tds->send_packet->buf + tds_packet_get_data_start(tds->send_packet));
114 5886322 : assert(tds->out_buf + tds->out_buf_max + TDS_ADDITIONAL_SPACE <=
115 : tds->send_packet->buf + tds->send_packet->capacity);
116 5886322 : assert(tds->out_pos <= tds->out_buf_max + TDS_ADDITIONAL_SPACE);
117 :
118 5886322 : assert(tds->in_buf == tds->recv_packet->buf || tds->in_buf == tds->recv_packet->buf + sizeof(TDS72_SMP_HEADER));
119 5886322 : assert(tds->recv_packet->capacity > 0);
120 :
121 : /* test res_info */
122 5886322 : if (tds->res_info)
123 3216913 : tds_check_resultinfo_extra(tds->res_info);
124 :
125 : /* test num_comp_info, comp_info */
126 : assert(tds->num_comp_info >= 0);
127 5046 : for (i = 0; i < tds->num_comp_info; ++i) {
128 5046 : assert(tds->comp_info);
129 5046 : tds_check_resultinfo_extra(tds->comp_info[i]);
130 : }
131 :
132 : /* param_info */
133 5886322 : if (tds->param_info)
134 13604 : tds_check_resultinfo_extra(tds->param_info);
135 :
136 : /* test cursors */
137 6079378 : for (cur_cursor = tds->conn->cursors; cur_cursor != NULL; cur_cursor = cur_cursor->next)
138 193056 : tds_check_cursor_extra(cur_cursor);
139 :
140 : /* test dynamics */
141 6237753 : for (cur_dyn = tds->conn->dyns; cur_dyn != NULL; cur_dyn = cur_dyn->next)
142 351431 : tds_check_dynamic_extra(cur_dyn);
143 :
144 : /* test tds_ctx */
145 11772644 : tds_check_context_extra(tds_get_ctx(tds));
146 :
147 : /* TODO test char_conv_count, char_convs */
148 :
149 : /* we can't have compute and no results */
150 5886322 : assert(tds->num_comp_info == 0 || tds->res_info != NULL);
151 :
152 : /* we can't have normal and parameters results */
153 : /* TODO too strict ?? */
154 : /* assert(tds->param_info == NULL || tds->res_info == NULL); */
155 :
156 5886322 : if (tds->frozen) {
157 : TDSPACKET *pkt;
158 :
159 527658 : assert(tds->frozen_packets != NULL);
160 1061936 : for (pkt = tds->frozen_packets; pkt; pkt = pkt->next) {
161 534278 : if (pkt->next == NULL)
162 527658 : assert(pkt == tds->send_packet);
163 : }
164 : } else {
165 5358664 : assert(tds->frozen_packets == NULL);
166 : }
167 5886322 : }
168 :
169 : void
170 41385 : tds_check_context_extra(const TDSCONTEXT * ctx)
171 : {
172 5927707 : assert(ctx);
173 41385 : }
174 :
175 : void
176 5886322 : tds_check_env_extra(const TDSENV * env)
177 : {
178 5886322 : assert(env);
179 :
180 5886322 : assert(env->block_size >= 0 && env->block_size <= 65536);
181 5886322 : }
182 :
183 : void
184 14579382 : tds_check_column_extra(const TDSCOLUMN * column)
185 : {
186 : int size;
187 : TDSCONNECTION conn;
188 : int varint_ok;
189 : int column_varint_size;
190 :
191 14579382 : assert(column);
192 14579382 : column_varint_size = column->column_varint_size;
193 :
194 : /* 8 is for varchar(max) or similar */
195 14579382 : assert(column_varint_size == 8 || (column_varint_size <= 5 && column_varint_size != 3));
196 :
197 14579382 : assert(column->column_scale <= column->column_prec);
198 14579382 : assert(column->column_prec <= MAXPRECISION);
199 :
200 : /* I don't like this that much... freddy77 */
201 14579382 : if (column->column_type == 0)
202 696142 : return;
203 13980712 : assert(column->funcs);
204 : assert(column->column_type > 0);
205 :
206 : /* specific checks, if true fully checked */
207 13980712 : if (column->funcs->check(column))
208 : return;
209 :
210 : /* check type and server type same or SQLNCHAR -> SQLCHAR */
211 : #define SPECIAL(ttype, server_type, varint) \
212 : if (column->column_type == ttype && column->on_server.column_type == server_type && column_varint_size == varint) {} else
213 13883240 : SPECIAL(SYBTEXT, XSYBVARCHAR, 8)
214 13880690 : SPECIAL(SYBTEXT, XSYBNVARCHAR, 8)
215 13879266 : SPECIAL(SYBIMAGE, XSYBVARBINARY, 8)
216 13878008 : assert(tds_get_cardinal_type(column->on_server.column_type, column->column_usertype) == column->column_type
217 : || (tds_get_conversion_type(column->on_server.column_type, column->column_size) == column->column_type
218 : && column_varint_size == 1 && is_fixed_type(column->column_type)));
219 :
220 13883240 : varint_ok = 0;
221 13883240 : if (column_varint_size == 8) {
222 28276 : if (column->on_server.column_type == XSYBVARCHAR
223 14138 : || column->on_server.column_type == XSYBVARBINARY
224 4424 : || column->on_server.column_type == XSYBNVARCHAR)
225 : varint_ok = 1;
226 13869102 : } else if (type_has_textptr(column->column_type)) {
227 131295 : assert(column_varint_size >= 4);
228 13737807 : } else if (column->column_type == SYBVARIANT) {
229 9502 : assert(column_varint_size == 4);
230 : }
231 1096 : conn.tds_version = 0x500;
232 22747456 : varint_ok = varint_ok || tds_get_varint_size(&conn, column->on_server.column_type) == column_varint_size;
233 13883240 : conn.tds_version = 0x700;
234 18889222 : varint_ok = varint_ok || tds_get_varint_size(&conn, column->on_server.column_type) == column_varint_size;
235 0 : assert(varint_ok);
236 :
237 13883240 : assert(!is_numeric_type(column->column_type));
238 13883240 : assert(column->column_cur_size <= column->column_size);
239 :
240 : /* check size of fixed type correct */
241 13883240 : size = tds_get_size_by_type(column->column_type);
242 : /* these peculiar types are variable but have only a possible size */
243 13883240 : if ((size > 0 && (column->column_type != SYBBITN && column->column_type != SYBDATEN && column->column_type != SYBTIMEN))
244 10143275 : || column->column_type == SYBVOID) {
245 : /* check macro */
246 3740013 : assert(is_fixed_type(column->column_type));
247 : /* check current size */
248 3740013 : assert(size == column->column_size);
249 : /* check cases where server need nullable types */
250 3740013 : if (column->column_type != column->on_server.column_type && (column->column_type != SYBINT8 || column->on_server.column_type != SYB5INT8)) {
251 72247 : assert(!is_fixed_type(column->on_server.column_type));
252 72247 : assert(column_varint_size == 1);
253 72247 : assert(column->column_size == column->column_cur_size || column->column_cur_size == -1);
254 : } else {
255 3667766 : assert(column_varint_size == 0
256 : || (column->column_type == SYBUNIQUE && column_varint_size == 1));
257 3667766 : assert(column->column_size == column->column_cur_size
258 : || (column->column_type == SYBUNIQUE && column->column_cur_size == -1));
259 : }
260 3740013 : assert(column->column_size == column->on_server.column_size);
261 : } else {
262 10143227 : assert(!is_fixed_type(column->column_type));
263 10143227 : assert(is_char_type(column->column_type) || (column->on_server.column_size == column->column_size || column->on_server.column_size == 0));
264 10143227 : assert(column_varint_size != 0);
265 : }
266 :
267 : /* check size of nullable types (ie intN) it's supported */
268 13883240 : if (tds_get_conversion_type(column->column_type, 4) != column->column_type) {
269 : /* check macro */
270 3280034 : assert(is_nullable_type(column->column_type));
271 : /* check that size it's correct for this type of nullable */
272 3280034 : assert(tds_get_conversion_type(column->column_type, column->column_size) != column->column_type);
273 : /* check current size */
274 3280034 : assert(column->column_size >= column->column_cur_size || column->column_cur_size == -1);
275 : /* check same type and size on server */
276 3280034 : assert(column->column_type == column->on_server.column_type);
277 3280034 : assert(column->column_size == column->on_server.column_size);
278 : }
279 13883240 : assert(column->column_iconv_left >= 0 && column->column_iconv_left <= sizeof(column->column_iconv_buf));
280 : }
281 :
282 : void
283 3404529 : tds_check_resultinfo_extra(const TDSRESULTINFO * res_info)
284 : {
285 : int i;
286 :
287 3404529 : assert(res_info);
288 : assert(res_info->num_cols >= 0);
289 3404529 : assert(res_info->ref_count > 0);
290 12756075 : for (i = 0; i < res_info->num_cols; ++i) {
291 12756075 : assert(res_info->columns);
292 12756075 : tds_check_column_extra(res_info->columns[i]);
293 12756075 : assert(res_info->columns[i]->column_data != NULL || res_info->row_size == 0);
294 : }
295 :
296 3404529 : assert(res_info->row_size >= 0);
297 :
298 : assert(res_info->computeid >= 0);
299 :
300 : assert(res_info->by_cols >= 0);
301 3404529 : assert(res_info->by_cols == 0 || res_info->bycolumns);
302 3404529 : }
303 :
304 : void
305 193056 : tds_check_cursor_extra(const TDSCURSOR * cursor)
306 : {
307 193056 : assert(cursor);
308 :
309 193056 : assert(cursor->ref_count > 0);
310 :
311 193056 : if (cursor->res_info)
312 71426 : tds_check_resultinfo_extra(cursor->res_info);
313 193056 : }
314 :
315 : void
316 357091 : tds_check_dynamic_extra(const TDSDYNAMIC * dyn)
317 : {
318 357091 : assert(dyn);
319 :
320 357091 : assert(dyn->ref_count > 0);
321 :
322 357091 : if (dyn->res_info)
323 28026 : tds_check_resultinfo_extra(dyn->res_info);
324 357091 : if (dyn->params)
325 52194 : tds_check_resultinfo_extra(dyn->params);
326 :
327 357091 : assert(!dyn->emulated || dyn->query);
328 357091 : }
329 :
330 : void
331 367280 : tds_check_freeze_extra(const TDSFREEZE * freeze)
332 : {
333 : TDSPACKET *pkt;
334 :
335 367280 : assert(freeze);
336 367280 : assert(freeze->tds != NULL);
337 367280 : assert(freeze->size_len <= 4 && freeze->size_len != 3);
338 :
339 367280 : tds_check_tds_extra(freeze->tds);
340 :
341 : /* check position */
342 367280 : if (freeze->pkt == freeze->tds->send_packet)
343 364548 : assert(freeze->tds->out_pos >= freeze->pkt_pos);
344 : else
345 2732 : assert(freeze->pkt->data_len >= freeze->pkt_pos);
346 :
347 : /* check packet is in list */
348 367370 : for (pkt = freeze->tds->frozen_packets; ; pkt = pkt->next) {
349 367460 : assert(pkt);
350 367370 : if (pkt == freeze->pkt)
351 : break; /* found */
352 : }
353 367280 : }
354 :
355 : void
356 1414280 : tds_extra_assert_check(const char *fn, int line, int cond, const char *cond_str)
357 : {
358 1414280 : if (cond)
359 1414280 : return;
360 :
361 0 : fprintf(stderr, "%s:%d: Failed checking condition '%s'\n", fn, line, cond_str);
362 :
363 0 : abort();
364 : }
365 :
366 : #endif /* ENABLE_EXTRA_CHECKS */
|