提交 db928983 编写于 作者: D Daniel Gustafsson

Check for overflow in connection string construction

When constructing the connectionstring for connecting to the QEs,
ensure we don't overflow the assigned buffer.
Reviewed-by: NGang Xiong <gxiong@pivotal.io>
上级 dc7c370d
......@@ -669,10 +669,11 @@ makeOptions(void)
* to be passed to a qExec that is being started. NB: Can be called in a
* thread, so mustn't use palloc/elog/ereport/etc.
*/
void
bool
build_gpqeid_param(char *buf, int bufsz,
bool is_writer, int gangId, int hostSegs)
{
int len;
#ifdef HAVE_INT64_TIMESTAMP
#define TIMESTAMP_FORMAT INT64_FORMAT
#else
......@@ -683,9 +684,11 @@ build_gpqeid_param(char *buf, int bufsz,
#endif
#endif
snprintf(buf, bufsz, "%d;" TIMESTAMP_FORMAT ";%s;%d;%d",
gp_session_id, PgStartTime,
(is_writer ? "true" : "false"), gangId, hostSegs);
len = snprintf(buf, bufsz, "%d;" TIMESTAMP_FORMAT ";%s;%d;%d",
gp_session_id, PgStartTime,
(is_writer ? "true" : "false"), gangId, hostSegs);
return (len > 0 && len < bufsz);
}
static bool
......
......@@ -107,6 +107,7 @@ create_gang_retry:
{
for (i = 0; i < size; i++)
{
bool ret;
char gpqeid[100];
char *options;
......@@ -122,10 +123,15 @@ create_gang_retry:
* early enough now some locks are taken before command line
* options are recognized.
*/
build_gpqeid_param(gpqeid, sizeof(gpqeid),
type == GANGTYPE_PRIMARY_WRITER,
gang_id,
segdbDesc->segment_database_info->hostSegs);
ret = build_gpqeid_param(gpqeid, sizeof(gpqeid),
type == GANGTYPE_PRIMARY_WRITER,
gang_id,
segdbDesc->segment_database_info->hostSegs);
if (!ret)
ereport(ERROR,
(errcode(ERRCODE_GP_INTERCONNECTION_ERROR),
errmsg("failed to construct connectionstring")));
options = makeOptions();
......
......@@ -299,6 +299,7 @@ thread_DoConnect(void *arg)
*/
for (i = 0; i < db_count; i++)
{
bool ret;
char gpqeid[100];
segdbDesc = segdbDescPtrArray[i];
......@@ -314,10 +315,19 @@ thread_DoConnect(void *arg)
* early enough now some locks are taken before command line options
* are recognized.
*/
build_gpqeid_param(gpqeid, sizeof(gpqeid),
pParms->type == GANGTYPE_PRIMARY_WRITER,
pParms->gangId,
segdbDesc->segment_database_info->hostSegs);
ret = build_gpqeid_param(gpqeid, sizeof(gpqeid),
pParms->type == GANGTYPE_PRIMARY_WRITER,
pParms->gangId,
segdbDesc->segment_database_info->hostSegs);
if (!ret)
{
segdbDesc->errcode = ERRCODE_INTERNAL_ERROR;
appendPQExpBuffer(&segdbDesc->error_message,
"Internal error: unable to construct connection string");
write_log("thread_DoConnect: unable to construct connection string for segdb %i", i);
continue;
}
/* check the result in createGang */
cdbconn_doConnect(segdbDesc, gpqeid, pParms->connectOptions);
......
......@@ -103,7 +103,7 @@ extern struct SegmentDatabaseDescriptor *getSegmentDescriptorFromGang(const Gang
bool isPrimaryWriterGangAlive(void);
Gang *buildGangDefinition(GangType type, int gang_id, int size, int content);
void build_gpqeid_param(char *buf, int bufsz, bool is_writer, int gangId, int hostSegs);
bool build_gpqeid_param(char *buf, int bufsz, bool is_writer, int gangId, int hostSegs);
char *makeOptions(void);
extern bool segment_failure_due_to_recovery(const char *error_message);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册