Skip to content

Commit a1dc633

Browse files
committed
noexcept in fb_utils
+ Add fb_exception.h to common.vcxproj + delete status_exception operator= instead of hiding it
1 parent 8e52fde commit a1dc633

File tree

6 files changed

+86
-84
lines changed

6 files changed

+86
-84
lines changed

builds/win32/msvc15/common.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
<ClInclude Include="..\..\..\src\common\utils_proto.h" />
237237
<ClInclude Include="..\..\..\src\common\xdr.h" />
238238
<ClInclude Include="..\..\..\src\common\xdr_proto.h" />
239+
<ClInclude Include="..\..\..\src\include\fb_exception.h" />
239240
<ClInclude Include="..\..\..\src\include\firebird\impl\dsc_pub.h" />
240241
</ItemGroup>
241242
<PropertyGroup Label="Globals">

builds/win32/msvc15/common.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,5 +623,8 @@
623623
<ClInclude Include="..\..\..\src\include\firebird\impl\dsc_pub.h">
624624
<Filter>headers</Filter>
625625
</ClInclude>
626+
<ClInclude Include="..\..\..\src\include\fb_exception.h">
627+
<Filter>headers</Filter>
628+
</ClInclude>
626629
</ItemGroup>
627630
</Project>

src/common/fb_exception.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ const char* fatal_exception::what() const noexcept
300300
va_list args;
301301
va_start(args, format);
302302
char buffer[1024];
303-
VSNPRINTF(buffer, sizeof(buffer), format, args);
303+
vsnprintf(buffer, sizeof(buffer), format, args);
304304
buffer[sizeof(buffer) - 1] = 0;
305305
va_end(args);
306306
throw fatal_exception(buffer);

src/common/utils.cpp

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@
5656
#include "../common/os/os_utils.h"
5757
#include "firebird/impl/sqlda_pub.h"
5858
#include "../common/classes/ClumpletReader.h"
59-
#include "../common/StatusArg.h"
6059
#include "../common/TimeZoneUtil.h"
6160
#include "../common/config/config.h"
6261
#include "../common/ThreadStart.h"
6362

63+
#include <utility>
64+
6465
#ifdef WIN_NT
6566
#include <direct.h>
6667
#include <io.h> // isatty()
@@ -87,10 +88,10 @@
8788
namespace fb_utils
8889
{
8990

90-
bool implicit_name(const char* name, const char* prefix, int prefix_len);
91+
bool implicit_name(const char* name, const char* prefix, int prefix_len) noexcept;
9192

9293

93-
char* copy_terminate(char* dest, const char* src, size_t bufsize)
94+
char* copy_terminate(char* dest, const char* src, size_t bufsize) noexcept
9495
{
9596
/**************************************
9697
*
@@ -102,7 +103,7 @@ char* copy_terminate(char* dest, const char* src, size_t bufsize)
102103
* Do the same as strncpy but ensure the null terminator is written.
103104
*
104105
**************************************/
105-
if (!bufsize) // Was it a joke?
106+
if (!bufsize)
106107
return dest;
107108

108109
--bufsize;
@@ -111,8 +112,10 @@ char* copy_terminate(char* dest, const char* src, size_t bufsize)
111112
return dest;
112113
}
113114

115+
// blank character, ASCII(32)
116+
static inline constexpr char SPACE = '\x20';
114117

115-
char* exact_name(char* const name)
118+
char* exact_name(char* const name) noexcept
116119
{
117120
/**************************************
118121
*
@@ -135,14 +138,14 @@ char* exact_name(char* const name)
135138
++p;
136139
// Now, let's go back
137140
--p;
138-
while (p >= name && *p == '\x20') // blank character, ASCII(32)
141+
while (p >= name && *p == SPACE)
139142
--p;
140143
*(p + 1) = '\0';
141144
return name;
142145
}
143146

144147

145-
char* exact_name_limit(char* const name, size_t bufsize)
148+
char* exact_name_limit(char* const name, size_t bufsize) noexcept
146149
{
147150
/**************************************
148151
*
@@ -168,7 +171,7 @@ char* exact_name_limit(char* const name, size_t bufsize)
168171
++p;
169172
// Now, let's go back
170173
--p;
171-
while (p >= name && *p == '\x20') // blank character, ASCII(32)
174+
while (p >= name && *p == SPACE)
172175
--p;
173176
*(p + 1) = '\0';
174177
return name;
@@ -180,7 +183,7 @@ char* exact_name_limit(char* const name, size_t bufsize)
180183
// *****************************
181184
// Determines if a domain or index is of the form RDB$<n[...n]>[<spaces>]
182185
// This may be true for implicit domains and for unique and non-unique indices except PKs.
183-
bool implicit_domain(const char* domain_name)
186+
bool implicit_domain(const char* domain_name) noexcept
184187
{
185188
return implicit_name(domain_name, IMPLICIT_DOMAIN_PREFIX, IMPLICIT_DOMAIN_PREFIX_LEN);
186189
}
@@ -190,7 +193,7 @@ bool implicit_domain(const char* domain_name)
190193
// i m p l i c i t _ i n t e g r i t y
191194
// ***********************************
192195
// Determines if a table integrity constraint domain is of the form INTEG_<n[...n]>[<spaces>]
193-
bool implicit_integrity(const char* integ_name)
196+
bool implicit_integrity(const char* integ_name) noexcept
194197
{
195198
return implicit_name(integ_name, IMPLICIT_INTEGRITY_PREFIX, IMPLICIT_INTEGRITY_PREFIX_LEN);
196199
}
@@ -200,7 +203,7 @@ bool implicit_integrity(const char* integ_name)
200203
// i m p l i c i t _ p k
201204
// ***********************************
202205
// Determines if an index is of the form RDB$PRIMARY<n[...n]>[<spaces>]
203-
bool implicit_pk(const char* pk_name)
206+
bool implicit_pk(const char* pk_name) noexcept
204207
{
205208
return implicit_name(pk_name, IMPLICIT_PK_PREFIX, IMPLICIT_PK_PREFIX_LEN);
206209
}
@@ -211,7 +214,7 @@ bool implicit_pk(const char* pk_name)
211214
// ***********************************
212215
// Determines if a name is of the form prefix<n[...n]>[<spaces>]
213216
// where prefix has a fixed known length.
214-
bool implicit_name(const char* name, const char* prefix, int prefix_len)
217+
bool implicit_name(const char* name, const char* prefix, int prefix_len) noexcept
215218
{
216219
if (strncmp(name, prefix, prefix_len) != 0)
217220
return false;
@@ -230,7 +233,7 @@ bool implicit_name(const char* name, const char* prefix, int prefix_len)
230233
}
231234

232235

233-
int name_length(const TEXT* const name)
236+
int name_length(const TEXT* const name) noexcept
234237
{
235238
/**************************************
236239
*
@@ -247,7 +250,7 @@ int name_length(const TEXT* const name)
247250
const TEXT* q = name - 1;
248251
for (const TEXT* p = name; *p; p++)
249252
{
250-
if (*p != ' ') {
253+
if (*p != SPACE) {
251254
q = p;
252255
}
253256
}
@@ -260,11 +263,11 @@ int name_length(const TEXT* const name)
260263
// n a m e _ l e n g t h _ l i m i t
261264
// *********************************
262265
// Compute length without trailing blanks. The second parameter is maximum length.
263-
int name_length_limit(const TEXT* const name, size_t bufsize)
266+
int name_length_limit(const TEXT* const name, size_t bufsize) noexcept
264267
{
265268
const char* p = name + bufsize - 1;
266269
// Now, let's go back
267-
while (p >= name && *p == ' ') // blank character, ASCII(32)
270+
while (p >= name && *p == SPACE)
268271
--p;
269272
return (p + 1) - name;
270273
}
@@ -282,7 +285,7 @@ bool readenv(const char* env_name, Firebird::string& env_value)
282285
if (rc)
283286
{
284287
env_value.reserve(rc - 1);
285-
DWORD rc2 = GetEnvironmentVariable(env_name, env_value.begin(), rc);
288+
const DWORD rc2 = GetEnvironmentVariable(env_name, env_value.begin(), rc);
286289
if (rc2 < rc && rc2 != 0)
287290
{
288291
env_value.recalculate_length();
@@ -345,23 +348,13 @@ bool setenv(const char* name, const char* value, bool overwrite)
345348
// s n p r i n t f
346349
// ***************
347350
// Provide a single place to deal with vsnprintf and error detection.
348-
int snprintf(char* buffer, size_t count, const char* format...)
351+
int snprintf(char* buffer, size_t count, const char* format...) noexcept
349352
{
350353
va_list args;
351354
va_start(args, format);
352-
const int rc = VSNPRINTF(buffer, count, format, args);
355+
const int rc = vsnprintf(buffer, count, format, args);
353356
buffer[count - 1] = 0;
354357
va_end(args);
355-
#if defined(DEV_BUILD) && !defined(HAVE_VSNPRINTF)
356-
// We don't have the safe functions, then check if we overflowed the buffer.
357-
// I would prefer to make this functionality available in prod build, too.
358-
// If the docs are right, the null terminator is not counted => rc < count.
359-
#if defined(fb_assert_continue)
360-
fb_assert_continue(rc >= 0 && rc < count);
361-
#else
362-
fb_assert(rc >= 0 && rc < count);
363-
#endif
364-
#endif
365358
return rc;
366359
}
367360

@@ -375,7 +368,7 @@ int snprintf(char* buffer, size_t count, const char* format...)
375368
// However, there are several usages through fb_utils::get_passwd(char* arg);
376369
char* cleanup_passwd(char* arg)
377370
{
378-
if (! arg)
371+
if (!arg)
379372
{
380373
return arg;
381374
}
@@ -399,7 +392,7 @@ char* cleanup_passwd(char* arg)
399392
// With sufficient privileges, we can add 'Global\' prefix for
400393
// names of all kernel objects we use.
401394

402-
bool prefix_kernel_object_name(char* name, size_t bufsize)
395+
bool prefix_kernel_object_name(char* name, size_t bufsize) noexcept
403396
{
404397
static bool bGlobalPrefix = false;
405398
static bool bInitDone = false;
@@ -415,7 +408,7 @@ bool prefix_kernel_object_name(char* name, size_t bufsize)
415408
// recommended in firebird.conf) additional prefix is not added
416409
if (bGlobalPrefix && !strchr(name, '\\'))
417410
{
418-
const char* prefix = "Global\\";
411+
constexpr const char* prefix = "Global\\";
419412
const size_t len_prefix = strlen(prefix);
420413
const size_t len_name = strlen(name) + 1;
421414

@@ -431,7 +424,7 @@ bool prefix_kernel_object_name(char* name, size_t bufsize)
431424
return true;
432425
}
433426

434-
bool isGlobalKernelPrefix()
427+
bool isGlobalKernelPrefix() noexcept
435428
{
436429
// The strategy of this function is as follows: use Global\ kernel namespace
437430
// for engine objects if we can.
@@ -499,7 +492,7 @@ class PrivateNamespace
499492
}
500493

501494
// Add namespace prefix to the name, returns true on success.
502-
bool addPrefix(char* name, size_t bufsize)
495+
bool addPrefix(char* name, size_t bufsize) noexcept
503496
{
504497
if (!isReady())
505498
return false;
@@ -518,16 +511,16 @@ class PrivateNamespace
518511
return true;
519512
}
520513

521-
bool isReady() const
514+
bool isReady() const noexcept
522515
{
523516
return (m_hNamespace != NULL) || (m_hTestEvent != NULL);
524517
}
525518

526519
private:
527-
const char* sPrivateNameSpace = "FirebirdCommon";
528-
const char* sBoundaryName = "FirebirdCommonBoundary";
520+
const char* const sPrivateNameSpace = "FirebirdCommon";
521+
const char* const sBoundaryName = "FirebirdCommonBoundary";
529522

530-
void raiseError(const char* apiRoutine)
523+
[[noreturn]] void raiseError(const char* apiRoutine)
531524
{
532525
(Firebird::Arg::Gds(isc_sys_request) << apiRoutine << Firebird::Arg::OsError()).raise();
533526
}
@@ -557,7 +550,7 @@ class PrivateNamespace
557550
LocalFree(strSid);
558551
}
559552
else
560-
strncpy(strSecDesc, "D:(A;;GA;;;WD)", sizeof(strSecDesc));
553+
copy_terminate(strSecDesc, "D:(A;;GA;;;WD)", sizeof(strSecDesc));
561554

562555
if (!ConvertStringSecurityDescriptorToSecurityDescriptor(strSecDesc, SDDL_REVISION_1,
563556
&sa.lpSecurityDescriptor, NULL))
@@ -667,7 +660,7 @@ Firebird::PathName get_process_name()
667660
return buffer;
668661
}
669662

670-
SLONG genUniqueId()
663+
SLONG genUniqueId() noexcept
671664
{
672665
static Firebird::AtomicCounter cnt;
673666
return ++cnt;
@@ -677,11 +670,11 @@ void getCwd(Firebird::PathName& pn)
677670
{
678671
char* buffer = pn.getBuffer(MAXPATHLEN);
679672
#if defined(WIN_NT)
680-
_getcwd(buffer, MAXPATHLEN);
673+
std::ignore = _getcwd(buffer, MAXPATHLEN);
681674
#elif defined(HAVE_GETCWD)
682-
FB_UNUSED(getcwd(buffer, MAXPATHLEN));
675+
std::ignore = getcwd(buffer, MAXPATHLEN);
683676
#else
684-
FB_UNUSED(getwd(buffer));
677+
std::ignore = getwd(buffer);
685678
#endif
686679
pn.recalculate_length();
687680
}
@@ -744,8 +737,8 @@ namespace {
744737
}
745738
}
746739

747-
FILE* getStdioFile() { return f; }
748-
bool operator!() { return !f; }
740+
FILE* getStdioFile() noexcept { return f; }
741+
bool operator!() noexcept { return !f; }
749742

750743
private:
751744
FILE* f;
@@ -1166,7 +1159,7 @@ void copyStatus(Firebird::CheckStatusWrapper* to, const Firebird::IStatus* from)
11661159
{
11671160
to->init();
11681161

1169-
unsigned flags = from->getState();
1162+
const unsigned flags = from->getState();
11701163
if (flags & Firebird::IStatus::STATE_ERRORS)
11711164
to->setErrors(from->getErrors());
11721165
if (flags & Firebird::IStatus::STATE_WARNINGS)
@@ -1404,7 +1397,7 @@ bool isRunningCheck(const UCHAR* items, unsigned int length)
14041397
return state == S_RUN;
14051398
}
14061399

1407-
static inline char conv_bin2ascii(ULONG l)
1400+
static inline char conv_bin2ascii(ULONG l) noexcept
14081401
{
14091402
return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[l & 0x3f];
14101403
}
@@ -1452,7 +1445,7 @@ void random64(Firebird::string& randomValue, FB_SIZE_T length)
14521445
abort();
14531446
}
14541447

1455-
UCHAR sqlTypeToDscType(SSHORT sqlType)
1448+
UCHAR sqlTypeToDscType(SSHORT sqlType) noexcept
14561449
{
14571450
switch (sqlType)
14581451
{
@@ -1553,7 +1546,7 @@ const ISC_STATUS* nextCode(const ISC_STATUS* v) noexcept
15531546
return v;
15541547
}
15551548

1556-
bool containsErrorCode(const ISC_STATUS* v, ISC_STATUS code)
1549+
bool containsErrorCode(const ISC_STATUS* v, ISC_STATUS code) noexcept
15571550
{
15581551
for (; v[0] == isc_arg_gds; v = nextCode(v))
15591552
{
@@ -1635,7 +1628,7 @@ bool isBpbSegmented(unsigned parLength, const unsigned char* par)
16351628
if (!bpb.find(isc_bpb_type))
16361629
return true;
16371630

1638-
int type = bpb.getInt();
1631+
const int type = bpb.getInt();
16391632

16401633
return type & isc_bpb_type_stream ? false : true;
16411634
}

0 commit comments

Comments
 (0)