Skip to content

Commit

Permalink
Bug#32233543 [#2] REPLACE STRNCPY AND REMOVE STRINGOP-TRUNCATION COMP…
Browse files Browse the repository at this point in the history
…ILER WARNINGS

Remove some stringop-truncation warning using cstrbuf.

Change-Id: I3ab43f6dd8c8b0b784d919211b041ac3ad4fad40
  • Loading branch information
zmur committed Nov 29, 2021
1 parent caa46a3 commit c7c5f81
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
3 changes: 2 additions & 1 deletion storage/ndb/include/util/Properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ struct Property {
*/
class Properties {
public:
static const char delimiter;
static constexpr char delimiter = ':';
static constexpr char truncated_prefix_mark[] = "...:";
static const char version[];

Properties(bool case_insensitive= false);
Expand Down
11 changes: 9 additions & 2 deletions storage/ndb/src/common/logger/LogHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <time.h>

#include <NdbTick.h>
#include "util/cstrbuf.h"

//
// PUBLIC
Expand Down Expand Up @@ -60,8 +61,14 @@ LogHandler::append(const char* pCategory, Logger::LoggerLevel level,
append_impl(m_last_category, m_last_level, m_last_message, now);

m_last_level= level;
snprintf(m_last_category, sizeof(m_last_category), "%s", pCategory);
snprintf(m_last_message, sizeof(m_last_message), "%s", pMsg);
if (cstrbuf_copy(m_last_category, pCategory) == 1)
{
// truncated category
}
if (cstrbuf_copy(m_last_message, pMsg) == 1)
{
// truncated message
}
}
else // repeated message
{
Expand Down
20 changes: 17 additions & 3 deletions storage/ndb/src/common/transporter/Transporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <SocketAuthenticator.hpp>
#include <InputStream.hpp>
#include <OutputStream.hpp>
#include "util/cstrbuf.h"

#include <EventLogger.hpp>

Expand Down Expand Up @@ -97,8 +98,15 @@ Transporter::Transporter(TransporterRegistry &t_reg,
m_is_active = true;

assert(rHostName);
if (rHostName && strlen(rHostName) > 0){
snprintf(remoteHostName, sizeof(remoteHostName), "%s", rHostName);
if (rHostName && strlen(rHostName) > 0)
{
if (cstrbuf_copy(remoteHostName, rHostName) == 1)
{
ndbout << "Unable to setup transporter. Node " << rNodeId
<< " had a too long hostname '" << rHostName
<< "'. Update configuration." << endl;
exit(-1);
}
}
else
{
Expand All @@ -111,7 +119,13 @@ Transporter::Transporter(TransporterRegistry &t_reg,
}
remoteHostName[0]= 0;
}
snprintf(localHostName, sizeof(localHostName), "%s", lHostName);
if (cstrbuf_copy(localHostName, lHostName) == 1)
{
ndbout << "Unable to setup transporter. Node " << lNodeId
<< " had a too long hostname '" << lHostName
<< "'. Update configuration." << endl;
exit(-1);
}

DBUG_PRINT("info",("rId=%d lId=%d isServer=%d rHost=%s lHost=%s s_port=%d",
remoteNodeId, localNodeId, isServer,
Expand Down
32 changes: 18 additions & 14 deletions storage/ndb/src/common/util/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <string>
#include <algorithm>
#include <utility>
#include "util/cstrbuf.h"

static
char * f_strdup(const char * s){
Expand All @@ -42,7 +43,6 @@ char * f_strdup(const char * s){
* Note has to be a multiple of 4 bytes
*/
const char Properties::version[] = { 2, 0, 0, 1, 1, 1, 1, 4 };
const char Properties::delimiter = ':';

/**
* PropertyImpl
Expand Down Expand Up @@ -415,33 +415,37 @@ Properties::remove(const char * name) {
}

void
Properties::print(FILE * out, const char * prefix) const{
char buf[1024];
if(prefix == 0)
buf[0] = 0;
else
snprintf(buf, 1024, "%s", prefix);
Properties::print(FILE * out, const char * prefix) const
{
if (prefix == nullptr)
{
prefix = "";
}

for (auto i : impl->content){
switch(i.second.valueType){
case PropertiesType_Uint32:
fprintf(out, "%s%s = (Uint32) %d\n", buf, i.second.name,
fprintf(out, "%s%s = (Uint32) %d\n", prefix, i.second.name,
*(Uint32 *)i.second.value);
break;
case PropertiesType_Uint64:
fprintf(out, "%s%s = (Uint64) %lld\n", buf, i.second.name,
fprintf(out, "%s%s = (Uint64) %lld\n", prefix, i.second.name,
*(Uint64 *)i.second.value);
break;
case PropertiesType_char:
fprintf(out, "%s%s = (char*) \"%s\"\n", buf, i.second.name,
fprintf(out, "%s%s = (char*) \"%s\"\n", prefix, i.second.name,
(char *)i.second.value);
break;
case PropertiesType_Properties:
char buf2 [1024];
BaseString::snprintf(buf2, sizeof(buf2), "%s%s%c",buf, i.second.name,
Properties::delimiter);
((Properties *)i.second.value)->print(out, buf2);
{
cstrbuf<1024> new_prefix;
new_prefix.append(prefix);
new_prefix.append(i.second.name);
new_prefix.append(1, delimiter);
new_prefix.replace_end_if_truncated(truncated_prefix_mark);
((Properties *)i.second.value)->print(out, new_prefix.c_str());
break;
}
case PropertiesType_Undefined:
assert(0);
}
Expand Down
9 changes: 7 additions & 2 deletions storage/ndb/src/mgmapi/LocalConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <NdbAutoPtr.hpp>
#include <util/NdbOut.hpp>
#include <NdbTCP.h>
#include "util/cstrbuf.h"

#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -125,9 +126,13 @@ LocalConfig::init(const char *connectString,
LocalConfig::~LocalConfig(){
}

void LocalConfig::setError(int lineNumber, const char * _msg) {
void LocalConfig::setError(int lineNumber, const char * _msg)
{
error_line = lineNumber;
snprintf(error_msg, sizeof(error_msg), "%s", _msg);
if (cstrbuf_copy(error_msg, _msg) == 1)
{
// ignore truncated error message
}
}

bool
Expand Down
29 changes: 12 additions & 17 deletions storage/ndb/tools/ndb_move_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <NdbSleep.h>
#include <ndb_limits.h>
#include <ndb_lib_move_data.hpp>
#include "util/cstrbuf.h"

static const char* opt_dbname = "TEST_DB";
static bool opt_exclude_missing_columns = false;
Expand Down Expand Up @@ -310,39 +311,33 @@ checkopts(int argc, char** argv)

CHK2(argc == 2, "arguments are: source target");

memset(g_source, 0, MAX_TAB_NAME_SIZE);
memset(g_sourcedb, 0, MAX_TAB_NAME_SIZE);
memset(g_sourcename, 0, MAX_TAB_NAME_SIZE);
memset(g_target, 0, MAX_TAB_NAME_SIZE);
memset(g_targetdb, 0, MAX_TAB_NAME_SIZE);
memset(g_targetname, 0, MAX_TAB_NAME_SIZE);

CHK2(strlen(opt_dbname) < MAX_TAB_NAME_SIZE, "db name too long");
CHK2(strlen(argv[0]) < MAX_TAB_NAME_SIZE, "source name too long");
CHK2(strlen(argv[1]) < MAX_TAB_NAME_SIZE, "target name too long");
strcpy(g_source, argv[0]);
strcpy(g_target, argv[1]);

require(cstrbuf_copy(g_source, argv[0]) == 0);
require(cstrbuf_copy(g_target, argv[1]) == 0);

const char* p;
if ((p = strchr(g_source, '.')) == 0)
{
strcpy(g_sourcedb, opt_dbname);
strcpy(g_sourcename, g_source);
require(cstrbuf_copy(g_sourcedb, opt_dbname) == 0);
require(cstrbuf_copy(g_sourcename, g_source) == 0);
}
else
{
strncpy(g_sourcedb, g_source, p - g_source); // is null term
strcpy(g_sourcename, p + 1);
require(cstrbuf_copy(g_sourcedb, {g_source, size_t(p - g_source)}) == 0);
require(cstrbuf_copy(g_sourcename, p + 1) == 0);
}
if ((p = strchr(g_target, '.')) == 0)
{
strcpy(g_targetdb, opt_dbname);
strcpy(g_targetname, g_target);
require(cstrbuf_copy(g_targetdb, opt_dbname) == 0);
require(cstrbuf_copy(g_targetname, g_target) == 0);
}
else
{
strncpy(g_targetdb, g_target, p - g_target); // is null term
strcpy(g_targetname, p + 1);
require(cstrbuf_copy(g_targetdb, {g_target, size_t(p - g_target)}) == 0);
require(cstrbuf_copy(g_targetname, p + 1) == 0);
}
}
while (0);
Expand Down

0 comments on commit c7c5f81

Please sign in to comment.