Skip to content

Commit 5a85185

Browse files
committed
Bug#32233543 [mysql#3] REPLACE STRNCPY AND REMOVE STRINGOP-TRUNCATION COMPILER WARNINGS
Remove stringop-truncation warning in ndb_config.cpp by refactoring. Change-Id: I1eea7fe190926a85502e73ca7ebf07d984af9a09
1 parent f5090f8 commit 5a85185

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

storage/ndb/tools/ndb_config.cpp

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@
7676
#include "storage/ndb/include/mgmcommon/NdbMgm.hpp"
7777
#include "../src/mgmapi/mgmapi_configuration.hpp"
7878
#include "../src/mgmsrv/ConfigInfo.hpp"
79+
#include "../src/mgmsrv/InitConfigFileParser.hpp"
7980
#include <NdbAutoPtr.hpp>
8081
#include <NdbTCP.h>
8182
#include <inttypes.h>
83+
#include "util/cstrbuf.h"
8284

8385
#include "my_alloc.h"
8486

@@ -380,7 +382,6 @@ print_diff(const ndb_mgm_configuration_iterator& iter)
380382
Uint64 val64;
381383
const char* config_value;
382384
const char* node_type = nullptr;
383-
char str[300] = {0};
384385

385386
if (iter.get(CFG_TYPE_OF_SECTION, &val32) == 0)
386387
{
@@ -418,58 +419,41 @@ print_diff(const ndb_mgm_configuration_iterator& iter)
418419
||
419420
(g_section == CFG_SECTION_SYSTEM))
420421
{
422+
cstrbuf<20 + 1> str_buf; // enough for 64-bit decimal number
423+
const char* str = nullptr;
421424
if (iter.get(ConfigInfo::m_ParamInfo[p]._paramId, &val32) == 0)
422425
{
423-
sprintf(str, "%u", val32);
426+
require(str_buf.appendf("%u", val32) == 0);
427+
str = str_buf.c_str();
424428
}
425429
else if (iter.get(ConfigInfo::m_ParamInfo[p]._paramId, &val64) == 0)
426430
{
427-
sprintf(str, "%llu", val64);
431+
require(str_buf.appendf("%ju", uintmax_t{val64}) == 0);
432+
str = str_buf.c_str();
428433
}
429434
else if (iter.get(ConfigInfo::m_ParamInfo[p]._paramId, &config_value) == 0)
430435
{
431-
strncpy(str, config_value,300);
436+
str = config_value;
432437
}
433438
else
434439
{
435440
continue;
436441
}
442+
require(str != nullptr);
437443

438444
if ((MANDATORY != ConfigInfo::m_ParamInfo[p]._default)
439445
&& (ConfigInfo::m_ParamInfo[p]._default)
440446
&& strlen(ConfigInfo::m_ParamInfo[p]._default) > 0
441-
&& !strcmp(node_type, ConfigInfo::m_ParamInfo[p]._section)
442-
&& strcmp(str, ConfigInfo::m_ParamInfo[p]._default) )
447+
&& strcmp(node_type, ConfigInfo::m_ParamInfo[p]._section) == 0
448+
&& strcmp(str, ConfigInfo::m_ParamInfo[p]._default) != 0)
443449
{
444-
char parse_str[300] = {0};
445-
bool convert_bytes = false;
446-
uint64 memory_convert = 0;
447-
uint64 def_value = 0;
448-
uint len = strlen(ConfigInfo::m_ParamInfo[p]._default) - 1;
449-
strncpy(parse_str, ConfigInfo::m_ParamInfo[p]._default,299);
450-
if (parse_str[len] == 'M' || parse_str[len] == 'm')
450+
Uint64 value;
451+
if (InitConfigFileParser::convertStringToUint64(str, value))
451452
{
452-
memory_convert = 1048576;
453-
convert_bytes = true;
454-
}
455-
if (parse_str[len] == 'K' || parse_str[len] == 'k')
456-
{
457-
memory_convert = 1024;
458-
convert_bytes = true;
459-
}
460-
if (parse_str[len] == 'G' || parse_str[len] == 'g')
461-
{
462-
memory_convert = 1099511627776ULL;
463-
convert_bytes = true;
464-
}
465-
466-
if (convert_bytes)
467-
{
468-
parse_str[len] = '\0';
469-
def_value = atoi(parse_str);
470-
memory_convert = memory_convert * def_value;
471-
BaseString::snprintf(parse_str, 299, "%" PRIu64, memory_convert);
472-
if (!strcmp(str, parse_str))
453+
const char* def_str = ConfigInfo::m_ParamInfo[p]._default;
454+
Uint64 def_value;
455+
require(InitConfigFileParser::convertStringToUint64(def_str, def_value));
456+
if (value == def_value)
473457
{
474458
continue;
475459
}

0 commit comments

Comments
 (0)