Skip to content

Commit 7afe00d

Browse files
authored
Addressed static code analyis issues (prefast) (#1227)
1 parent 53aaab8 commit 7afe00d

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

source/shared/core_sqlsrv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ namespace data_classification {
14441444
struct name_id_pair;
14451445
struct sensitivity_metadata;
14461446

1447-
void name_id_pair_free(name_id_pair * pair);
1447+
void name_id_pair_free(_Inout_ name_id_pair * pair);
14481448
void parse_sensitivity_name_id_pairs(_Inout_ sqlsrv_stmt* stmt, _Inout_ USHORT& numpairs, _Inout_ std::vector<name_id_pair*, sqlsrv_allocator<name_id_pair*>>* pairs, _Inout_ unsigned char **pptr);
14491449
void parse_column_sensitivity_props(_Inout_ sensitivity_metadata* meta, _Inout_ unsigned char **pptr, _In_ bool getRankInfo);
14501450
USHORT fill_column_sensitivity_array(_Inout_ sqlsrv_stmt* stmt, _In_ SQLSMALLINT colno, _Inout_ zval *column_data);

source/shared/core_stmt.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ void format_decimal_numbers(_In_ SQLSMALLINT decimals_places, _In_ SQLSMALLINT f
23362336
//
23372337

23382338
// Check if it's a negative number and if necessary to add the leading zero
2339-
bool is_negative = (*field_value == '-');
2339+
short is_negative = (*field_value == '-') ? 1 : 0;
23402340
char *src = field_value + is_negative;
23412341
bool add_leading_zero = false;
23422342

@@ -2354,12 +2354,12 @@ void format_decimal_numbers(_In_ SQLSMALLINT decimals_places, _In_ SQLSMALLINT f
23542354
scale = field_scale;
23552355
}
23562356

2357-
char buffer[50] = " "; // A buffer with two blank spaces, as leeway
2358-
int offset = 1 + is_negative;
2357+
char buffer[50] = " "; // A buffer with TWO blank spaces, as leeway
2358+
int offset = 1 + is_negative; // for cases like 9.* to 10.* and the minus sign if needed
23592359
int src_length = strnlen_s(src);
23602360

23612361
if (add_leading_zero) {
2362-
buffer[offset++] = '0';
2362+
buffer[offset++] = '0'; // leading zero added
23632363
}
23642364
// Copy the original numerical value to the buffer
23652365
memcpy_s(buffer + offset, src_length, src, src_length);
@@ -2375,10 +2375,11 @@ void format_decimal_numbers(_In_ SQLSMALLINT decimals_places, _In_ SQLSMALLINT f
23752375
}
23762376
}
23772377

2378-
// Remove the extra white space if not used
2379-
char *p = buffer;
2380-
offset = 0;
2381-
while (isspace(*p++)) {
2378+
// Remove the extra white space if not used. For a negative number,
2379+
// the first pos is always a space
2380+
offset = is_negative;
2381+
char *p = buffer + offset;
2382+
while (*p++ == ' ') {
23822383
offset++;
23832384
}
23842385
if (is_negative) {
@@ -3017,23 +3018,23 @@ void adjustDecimalPrecision(_Inout_ zval* param_z, _In_ SQLSMALLINT decimal_digi
30173018
return;
30183019
}
30193020

3020-
// If std::stold() succeeds, 'idx' is the position of the first character after the numerical value
3021+
// If std::stold() succeeds, 'index' is the position of the first character after the numerical value
30213022
long double d = 0;
3022-
size_t idx;
3023+
size_t index;
30233024
try {
3024-
d = std::stold(std::string(value), &idx);
3025+
d = std::stold(std::string(value), &index);
30253026
}
30263027
catch (const std::logic_error& ) {
30273028
return; // invalid input caused the conversion to throw an exception
30283029
}
3029-
if (idx < value_len) {
3030+
if (index < value_len) {
30303031
return; // the input contains something else apart from the numerical value
30313032
}
30323033

30333034
// Navigate to the first digit or the decimal point
3034-
bool is_negative = (d < 0);
3035+
short is_negative = (d < 0) ? 1 : 0;
30353036
char *src = value + is_negative;
3036-
while (*src != DECIMAL_POINT && !isdigit(*src)) {
3037+
while (*src != DECIMAL_POINT && !isdigit(static_cast<unsigned int>(*src))) {
30373038
src++;
30383039
}
30393040

source/shared/core_util.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ SQLCHAR SSPWARN[] = "01SSP";
7272
// the script (sqlsrv_configure).
7373
void write_to_log( _In_ unsigned int severity, _In_ const char* msg, ...)
7474
{
75-
SQLSRV_ASSERT( !(g_driver_severity == NULL), "Must register a driver checker function." );
75+
SQLSRV_ASSERT(g_driver_severity != NULL, "Must register a driver checker function.");
7676
if (!g_driver_severity(severity)) {
7777
return;
7878
}
@@ -491,11 +491,11 @@ namespace data_classification {
491491
const char* ID = "id";
492492
const char* RANK = "rank";
493493

494-
void convert_sensivity_field(_Inout_ sqlsrv_stmt* stmt, _In_ SQLSRV_ENCODING encoding, _In_ unsigned char *ptr, _In_ int len, _Inout_updates_bytes_(cchOutLen) char** field_name)
494+
void convert_sensivity_field(_Inout_ sqlsrv_stmt* stmt, _In_ SQLSRV_ENCODING encoding, _In_ unsigned char *ptr, _In_ int len, _Inout_updates_bytes_(field_name_len) char** field_name, _Out_ SQLLEN& field_name_len)
495495
{
496496
sqlsrv_malloc_auto_ptr<SQLWCHAR> temp_field_name;
497497
int temp_field_len = len * sizeof(SQLWCHAR);
498-
SQLLEN field_name_len = 0;
498+
field_name_len = 0;
499499

500500
if (len == 0) {
501501
*field_name = reinterpret_cast<char*>(sqlsrv_malloc(1));
@@ -538,6 +538,7 @@ namespace data_classification {
538538
while (npairs--) {
539539
int namelen, idlen;
540540
unsigned char *nameptr, *idptr;
541+
SQLLEN field_len;
541542

542543
sqlsrv_malloc_auto_ptr<name_id_pair> pair;
543544
pair = new(sqlsrv_malloc(sizeof(name_id_pair))) name_id_pair();
@@ -549,7 +550,7 @@ namespace data_classification {
549550
nameptr = ptr;
550551

551552
pair->name_len = namelen;
552-
convert_sensivity_field(stmt, encoding, nameptr, namelen, (char**)&name);
553+
convert_sensivity_field(stmt, encoding, nameptr, namelen, (char**)&name, field_len);
553554
pair->name = name;
554555

555556
ptr += namelen * 2;
@@ -558,7 +559,7 @@ namespace data_classification {
558559
ptr += idlen * 2;
559560

560561
pair->id_len = idlen;
561-
convert_sensivity_field(stmt, encoding, idptr, idlen, (char**)&id);
562+
convert_sensivity_field(stmt, encoding, idptr, idlen, (char**)&id, field_len);
562563
pair->id = id;
563564

564565
pairs->push_back(pair.get());

0 commit comments

Comments
 (0)