-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.cpp
More file actions
761 lines (688 loc) · 17.8 KB
/
Copy pathstatus.cpp
File metadata and controls
761 lines (688 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
// Custom IStatus implemntation for usage simplification
#include <limits.h>
#include <string.h>
#include <utility>
#ifndef __WIN32__
#include <cxxabi.h>
#endif
#include "fbinterface.h"
#include "status.h"
void StatusTypeCatchStuff::catchException(StatusTypeCatchStuff* target)
{
try
{
throw;
}
catch(const Status& st)
{
if (target->real_status == nullptr)
return;
unsigned state = st.getState();
if (state & Firebird::IStatus::STATE_ERRORS)
{
target->real_status->setErrors(st.getErrors());
}
if (state & Firebird::IStatus::STATE_WARNINGS)
{
target->real_status->setWarnings(st.getWarnings());
}
}
catch(const std::bad_alloc&)
{
static const ISC_STATUS st[] = { isc_arg_gds, isc_virmemexh,
isc_arg_end
};
target->real_status->setErrors(st);
}
catch (const std::exception& ex)
{
ISC_STATUS st[] = { isc_arg_gds, isc_random, isc_arg_string, (ISC_STATUS)"std::exception caught",
isc_arg_gds, isc_random, isc_arg_string, reinterpret_cast<ISC_STATUS>(ex.what()),
isc_arg_end
};
target->real_status->setErrors(st);
}
#ifndef __WIN32__ // Workaround internals of pthread_cancel
catch (const abi::__forced_unwind&)
{
throw;
}
#endif
catch(...)
{
static const ISC_STATUS statusVector[] = {
isc_arg_gds, isc_random, isc_arg_string, (ISC_STATUS) "Unrecognized C++ exception",
isc_arg_end};
target->real_status->setErrors(statusVector);
}
}
void Status::setVersionError(Status* status, const char* what, unsigned foundVersion, unsigned requiredVersion)
{
const intptr_t err[] = {
isc_arg_gds,
isc_interface_version_too_old,
isc_arg_number,
(intptr_t) requiredVersion,
isc_arg_number,
(intptr_t) foundVersion,
isc_arg_string,
(intptr_t) what,
isc_arg_end
};
status->setErrors(err);
}
namespace
{
static const intptr_t NoErrorsVector[] = { isc_arg_gds, 0, isc_arg_end };
size_t calculateLegacySize(const unsigned char* buffer, size_t bufferLength)
{
size_t vectorLength = 0;
if (buffer != nullptr && bufferLength != 0)
{
const unsigned char* buffer_end = buffer + bufferLength;
for (const unsigned char* p = buffer; p < buffer_end;)
{
const unsigned char tag = *p++;
switch(tag)
{
case isc_arg_gds: // generic DSRI status value
case isc_arg_number: // numeric argument (long)
case isc_arg_unix: // UNIX error code
case isc_arg_win32: // Win32 error code
case isc_arg_warning: // warning argument
{
if ((p += 4) > buffer_end) // Unavoidable sanity check
{
// No throw, just cut it
return vectorLength;
}
vectorLength += 2;
break;
}
case isc_arg_string: // string argument
case isc_arg_interpreted: // interpreted status code (string)
{
if (p + 2 > buffer_end)
return vectorLength;
unsigned len = *reinterpret_cast<const unsigned short*>(p);
p += len;
if (p > buffer_end)
return vectorLength;
vectorLength += 2;
break;
}
default: // Every unrecognized code cause abort of parsing. We have no way to indicate errors here
// These codes are here as well
//case isc_arg_end: // end of argument list
//case isc_arg_sql_state: // SQLSTATE
//case isc_arg_cstring: // count & string argument
//case isc_arg_vms: // VAX/VMS status code (long)
//case isc_arg_domain: // Apollo/Domain error code
//case isc_arg_dos: // MSDOS/OS2 error code
//case isc_arg_mpexl: // HP MPE/XL error code
//case isc_arg_mpexl_ipc: // HP MPE/XL IPC error code
//case isc_arg_next_mach: // NeXT/Mach error code
//case isc_arg_netware: // NetWare error code
return vectorLength;
}
}
}
return vectorLength;
}
// This routine also advance source pointer
void GatherLegacyVector(const unsigned char* &from, intptr_t* to, size_t toLength)
{
for (size_t i = 0; i < toLength; i += 2)
{
char tag = *from++;
to[i] = tag;
if (tag == isc_arg_string || tag == isc_arg_interpreted)
{
unsigned len = *reinterpret_cast<const uint16_t*>(from);
from += sizeof(uint16_t);
to[i + 1] = reinterpret_cast<intptr_t>(from);
from += len;
}
else
{
to[i + 1] = *reinterpret_cast<const uint32_t*>(from);
from += sizeof(uint32_t);
}
}
}
}
Status::DataHolder::DataHolder(size_t length): refCnt(1)
{
// Allocation is here to abort creation on std::bad_alloc
data_len = length;
data = new unsigned char[length];
}
Status::DataHolder::~DataHolder()
{
delete[] data;
delete[] legacyVector;
}
Status::DataHolder* Status::DataHolder::create(const intptr_t* legacy_value, unsigned value_length)
{
// Calculate required length
size_t buffer_size = 0;
const intptr_t* value = legacy_value;
for (unsigned length = value_length; length-- > 0;) // Only tags with parameters are interesting
{
switch(*value++)
{
case isc_arg_gds: // generic DSRI status value
case isc_arg_number: // numeric argument (long)
case isc_arg_unix: // UNIX error code
case isc_arg_win32: // Win32 error code
case isc_arg_warning: // warning argument
{
if (length > 0) // Unavoidable sanity check
{
length--;
// So far fix value length to 4 bytes
buffer_size += 1 + 4;
}
break;
}
case isc_arg_string: // string argument
case isc_arg_interpreted: // interpreted status code (string)
{
if (length > 0)
{
length--;
size_t len = strlen((char*)(*value)) + 1; // Zero terminator is included
if (len > USHRT_MAX)
{
len = USHRT_MAX;
}
buffer_size += 1 + 2 + len;
}
break;
}
case isc_arg_cstring: // count & string argument
{
// Convert CSTRING into STRING
if (length > 1)
{
length -= 2;
size_t len = *value++;
if (len >= USHRT_MAX)
{
len = USHRT_MAX - 1;
}
buffer_size += 1 + 2 + len + 1;
}
break;
}
case isc_arg_sql_state: // SQLSTATE
{
// Ignore it...?
if (length > 0)
length--;
break;
}
default: // Every unrecognized code cause abort of parsing. We have no way to indicate errors here
// These codes are here as well
//case isc_arg_end: // end of argument list
//case isc_arg_vms: // VAX/VMS status code (long)
//case isc_arg_domain: // Apollo/Domain error code
//case isc_arg_dos: // MSDOS/OS2 error code
//case isc_arg_mpexl: // HP MPE/XL error code
//case isc_arg_mpexl_ipc: // HP MPE/XL IPC error code
//case isc_arg_next_mach: // NeXT/Mach error code
//case isc_arg_netware: // NetWare error code
goto bail;
}
value++;
}
bail:
if (buffer_size == 0)
return nullptr;
DataHolder* result = new DataHolder(buffer_size);
// Gather values
value = legacy_value;
for (unsigned char* buffer = result->data; buffer < result->data + buffer_size;)
{
*buffer++ = *value;
switch(*value++)
{
case isc_arg_gds: // generic DSRI status value
case isc_arg_number: // numeric argument (long)
case isc_arg_unix: // UNIX error code
case isc_arg_win32: // Win32 error code
case isc_arg_warning: // warning argument
{
// Value of fixed length 4 bytes
*reinterpret_cast<uint32_t*>(buffer) = *value++;
buffer += sizeof(uint32_t);
break;
}
case isc_arg_string: // string argument
case isc_arg_interpreted: // interpreted status code (string)
{
size_t len = strlen((char*)(*value));
if (len >= USHRT_MAX)
{
len = USHRT_MAX - 1;
}
*reinterpret_cast<uint16_t*>(buffer) = len + 1; // Zero-terminator is included
buffer += sizeof(uint16_t);
memcpy(buffer, reinterpret_cast<char*>(*value++), len);
buffer += len;
*buffer++ = '\0';
break;
}
case isc_arg_cstring: // count & string argument
{
buffer[-1] = isc_arg_string; // Force override tag
size_t len = *value++;
if (len >= USHRT_MAX)
{
len = USHRT_MAX - 1;
}
*reinterpret_cast<uint16_t*>(buffer) = len + 1;
buffer += sizeof(uint16_t);
memcpy(buffer, reinterpret_cast<char*>(*value++), len);
buffer += len;
*buffer++ = '\0';
break;
}
case isc_arg_sql_state: // SQLSTATE, ignore it
{
value++;
--buffer; // Undo tag addition
break;
}
default: // Every unrecognized code cause abort of parsing. We have no way to indicate errors here
// These codes are here as well
//case isc_arg_end: // end of argument list
//case isc_arg_vms: // VAX/VMS status code (long)
//case isc_arg_domain: // Apollo/Domain error code
//case isc_arg_dos: // MSDOS/OS2 error code
//case isc_arg_mpexl: // HP MPE/XL error code
//case isc_arg_mpexl_ipc: // HP MPE/XL IPC error code
//case isc_arg_next_mach: // NeXT/Mach error code
//case isc_arg_netware: // NetWare error code
return result;
}
}
return result;
}
const intptr_t* Status::DataHolder::getLegacy()
{
if (legacyVector == nullptr)
{
if (data != nullptr)
{
size_t vectorLength = calculateLegacySize(data, data_len);
if (vectorLength == 0)
{
return NoErrorsVector;
}
try
{
legacyVector = new intptr_t[vectorLength + 1]; // Include one position for isc_arg_end
const unsigned char* p = data;
GatherLegacyVector(p, legacyVector, vectorLength);
// Add terminator
legacyVector[vectorLength] = isc_arg_end;
}
catch (const std::bad_alloc&)
{
static const intptr_t OutOfMemory[] = { isc_arg_gds, isc_virmemexh,
isc_arg_gds, isc_random, isc_arg_string, (intptr_t)"getErrors()/getWarnings()",
isc_arg_end };
return OutOfMemory;
}
}
else
{
return NoErrorsVector;
}
}
return legacyVector;
}
unsigned Status::getState() const
{
unsigned res = (errors != nullptr ? STATE_ERRORS : 0)
| (warnings != nullptr ? STATE_WARNINGS : 0);
return res;
}
void Status::setErrors(const intptr_t* value)
{
// Protection from self-assignment
if (value != nullptr &&
(value == NoErrorsVector || (errors != nullptr && value == errors->legacyVector)))
{
// Do not touch
return;
}
// Preserve old holder until the new one is formed
// because self-assignment above doesn't protect from
// the way Firebird implements appending to the status vector
std::unique_ptr<DataHolder, DataHolder::deleter> oldErrors(errors);
errors = nullptr;
if (value != nullptr && value[0] == isc_arg_gds && value[1] != 0)
errors = DataHolder::create(value);
}
void Status::setErrors2(unsigned length, const intptr_t* value)
{
// Protection from self-assignment
if (value != nullptr &&
(value == NoErrorsVector || (errors != nullptr && value == errors->legacyVector)))
{
// Do not touch
return;
}
std::unique_ptr<DataHolder, DataHolder::deleter> oldErrors(errors);
errors = nullptr;
if (value != nullptr && length != 0 && value[0] == isc_arg_gds && value[1] != 0)
errors = DataHolder::create(value, length);
}
const intptr_t* Status::getErrors() const
{
if (errors != nullptr)
return errors->getLegacy();
return NoErrorsVector;
}
void Status::setWarnings(const intptr_t* value)
{
// Protection from self-assignment
if (value != nullptr &&
(value == NoErrorsVector || (warnings != nullptr && value == warnings->legacyVector)))
{
// Do not touch
return;
}
std::unique_ptr<DataHolder, DataHolder::deleter> oldWarnings(warnings);
warnings = nullptr;
if (value != nullptr && value[0] == isc_arg_gds && value[1] != 0)
warnings = DataHolder::create(value);
}
void Status::setWarnings2(unsigned length, const intptr_t* value)
{
// Protection from self-assignment
if (value != nullptr &&
(value == NoErrorsVector || (warnings != nullptr && value == warnings->legacyVector)))
{
// Do not touch
return;
}
std::unique_ptr<DataHolder, DataHolder::deleter> oldWarnings(warnings);
warnings = nullptr;
if (value != nullptr && length != 0 && value[0] == isc_arg_gds && value[1] != 0)
warnings = DataHolder::create(value, length);
}
const intptr_t* Status::getWarnings() const
{
if (warnings != nullptr)
return warnings->getLegacy();
return NoErrorsVector;
}
Firebird::IStatus* Status::clone() const
{
return new Status(*this);
}
void Status::checkException(Status* status)
{
unsigned state = status->getState();
if (state & Firebird::IStatus::STATE_ERRORS)
{
switch (status->behavior)
{
case to::Ignore:
{
status->found = 0;
if (status->ignored != nullptr)
{
for (iterator itr = status->beginErrors(); itr; ++itr)
{
unsigned code = itr.gdscode();
for (const unsigned* i = status->ignored; *i != isc_arg_end; i++)
{
if (code == *i)
{
status->found = itr.offset;
return;
}
}
}
}
}
// fall through
case to::Throw:
throw std::move(*status);
case to::Log:
break;
}
}
if (status->errors != nullptr)
{
fprintf(stderr, "%s error:\n", status->where());
for (iterator itr = status->beginErrors(); itr; ++itr)
{
char buffer[2048];
itr.interpret(buffer, sizeof(buffer));
fprintf(stderr, "%u\t%s\n", itr.gdscode(), buffer);
}
}
if (status->warnings != nullptr)
{
fprintf(stderr, "%s warning:\n", status->where());
std::string message;
for (iterator itr = status->beginWarnings(); itr; ++itr)
{
char buffer[2048];
itr.interpret(buffer, sizeof(buffer));
fprintf(stderr, "%u\t%s\n", itr.gdscode(), buffer);
}
}
}
const char* Status::what() const noexcept
{
if (!whatCache.empty())
{
// Hardly can be a case, but still
return whatCache.c_str();
}
if (errors != nullptr)
{
whatCache += w;
whatCache += " error:\n";
for (iterator itr = beginErrors(); itr; ++itr)
{
char buffer[2048];
itr.interpret(buffer, sizeof(buffer));
whatCache += std::to_string(itr.gdscode());
whatCache.push_back('\t');
whatCache += buffer;
whatCache.push_back('\n');
}
}
if (warnings != nullptr)
{
whatCache += w;
whatCache += " warning:\n";
for (iterator itr = beginWarnings(); itr; ++itr)
{
char buffer[2048];
itr.interpret(buffer, sizeof(buffer));
whatCache += std::to_string(itr.gdscode());
whatCache.push_back('\t');
whatCache += buffer;
whatCache.push_back('\n');
}
}
if (whatCache.empty())
{
// Strange case of calling what without error happen
whatCache = w;
whatCache += " undefined status exception";
}
else
{
// Remove last EoLN
whatCache.pop_back();
}
return whatCache.c_str();
}
unsigned Status::iterator::gdscode()
{
if (*this)
{
const unsigned char* p = holder->data + offset;
if (*p == isc_arg_gds) // Extra check
{
p += 1;
return *reinterpret_cast<const uint32_t*>(p);
}
else
return *p; // Provisory solution for primary codes other than isc_arg_gds
}
return isc_arg_end;
}
Status::iterator& Status::iterator::operator++()
{
if (holder != nullptr && holder->data != nullptr)
{
const unsigned char* p = holder->data + offset;
do
{
const unsigned char tag = *p++;
if (tag == isc_arg_string || tag == isc_arg_interpreted)
{
unsigned short len = *reinterpret_cast<const uint16_t*>(p);
p += sizeof(uint16_t) + len;
}
else
{
p += 4;
}
offset = p - holder->data;
}
while (offset < holder->data_len && (*p == isc_arg_number || *p == isc_arg_string));
}
return *this;
}
Status::value_t Status::iterator::operator[](size_t index)
{
Status::value_t result;
if (holder != nullptr && holder->data != nullptr)
{
const unsigned char* p = holder->data + offset;
do
{
const unsigned char tag = *p++;
if (tag == isc_arg_string)
{
unsigned short len = *reinterpret_cast<const uint16_t*>(p);
p += sizeof(uint16_t) + len;
}
else
{
p += sizeof(uint32_t);
}
}
while (index-- > 0 && p < holder->data + holder->data_len && (*p == isc_arg_number || *p == isc_arg_string));
if (p < holder->data + holder->data_len)
{
// Previous loop exited by reaching needed index. Perhaps.
if (*p == isc_arg_string)
{
result.type = *p;
p += 1 + sizeof(uint16_t);
result.string = reinterpret_cast<const char*>(p);
}
else if (*p == isc_arg_number)
{
result.type = *p;
p += 1;
result.integer = *reinterpret_cast<const uint32_t*>(p);
}
// else it was a primary code and we run out of parameters
}
}
return result;
}
namespace
{
class StaticStub: public Firebird::IStatusImpl<StaticStub, StatusTypeCatchIgnore>
{
const ISC_STATUS* v;
public:
StaticStub() = delete;
StaticStub(const ISC_STATUS* vector): v(vector)
{
}
void dispose() override {}
void init() override {}
unsigned getState() const override
{
return STATE_ERRORS;
}
void setErrors2(unsigned, const intptr_t* value) override
{
v = value;
}
void setWarnings2(unsigned, const intptr_t* value) override
{
v = value;
}
void setErrors(const intptr_t* value) override
{
v = value;
}
void setWarnings(const intptr_t* value) override
{
v = value;
}
const intptr_t* getErrors() const override
{
return v;
}
const intptr_t* getWarnings() const override
{
return v;
}
Firebird::IStatus* clone() const override
{
return nullptr;
}
};
}
size_t Status::iterator::interpret(char* buffer, size_t size)
{
if (*this)
{
ISC_STATUS legacyVector[2 + 9 * 2 + 1] = {}; // Two positions for primary code, two positions for each of up to 9 arguments and one position for isc_arg_end
StaticStub dummy(legacyVector);
Firebird::IUtil* util = master->getUtilInterface();
intptr_t* target = legacyVector;
const unsigned char* p = holder->data + offset;
const unsigned char* const end = holder->data + holder->data_len;
for (int i = 0; i < 10; i++)
{
unsigned char tag = *p++;
*target++ = tag;
if (tag == isc_arg_string || tag == isc_arg_interpreted)
{
unsigned short len = *reinterpret_cast<const uint16_t*>(p);
p += sizeof(uint16_t);
*target++ = reinterpret_cast<intptr_t>(p);
p += len;
}
else
{
*target++ = *reinterpret_cast<const uint32_t*>(p);
p += sizeof(uint32_t);
}
if (p >= end || (*p != isc_arg_number && *p != isc_arg_string))
break;
}
*target = isc_arg_end;
unsigned res = util->formatStatus(buffer, size, &dummy);
return res;
}
return 0;
}