Skip to content

Commit d95e8a0

Browse files
authored
Merge pull request #5975 from larsewi/buffer
ENT-13522: verify_databases.c: Various bug fixes in CreateTableColumns()
2 parents ff09a71 + 6b6a90d commit d95e8a0

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

cf-agent/cf_sql.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ void CfCloseDB(CfdbConn *cfdb)
384384

385385
/*****************************************************************************/
386386

387-
void CfVoidQueryDB(CfdbConn *cfdb, char *query)
387+
void CfVoidQueryDB(CfdbConn *cfdb, const char *query)
388388
{
389389
if (!cfdb->connected)
390390
{
@@ -398,7 +398,7 @@ void CfVoidQueryDB(CfdbConn *cfdb, char *query)
398398

399399
/*****************************************************************************/
400400

401-
void CfNewQueryDB(CfdbConn *cfdb, char *query)
401+
void CfNewQueryDB(CfdbConn *cfdb, const char *query)
402402
{
403403
cfdb->result = false;
404404
cfdb->row = 0;

cf-agent/cf_sql.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ typedef struct
4242

4343
void CfConnectDB(CfdbConn *cfdb, DatabaseType dbtype, char *remotehost, char *dbuser, char *passwd, char *db);
4444
void CfCloseDB(CfdbConn *cfdb);
45-
void CfVoidQueryDB(CfdbConn *cfdb, char *query);
46-
void CfNewQueryDB(CfdbConn *cfdb, char *query);
45+
void CfVoidQueryDB(CfdbConn *cfdb, const char *query);
46+
void CfNewQueryDB(CfdbConn *cfdb, const char *query);
4747
char **CfFetchRow(CfdbConn *cfdb);
4848
char *CfFetchColumn(CfdbConn *cfdb, int col);
4949
void CfDeleteQuery(CfdbConn *cfdb);

cf-agent/verify_databases.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -743,48 +743,50 @@ static int TableExists(CfdbConn *cfdb, char *name)
743743

744744
static bool CreateTableColumns(CfdbConn *cfdb, char *table, Rlist *columns)
745745
{
746-
char entry[CF_MAXVARSIZE], query[CF_BUFSIZE];
747746
int i, *size_table, *done;
748747
char **name_table, **type_table;
749748
int no_of_cols = RlistLen(columns);
750749

751-
Log(LOG_LEVEL_ERR, "Trying to create table '%s'", table);
750+
Log(LOG_LEVEL_VERBOSE, "Trying to create table '%s'", table);
752751

753752
if (!NewSQLColumns(table, columns, &name_table, &type_table, &size_table, &done))
754753
{
755754
return false;
756755
}
757756

758-
if (no_of_cols > 0)
757+
if (no_of_cols <= 0)
759758
{
760-
snprintf(query, CF_BUFSIZE - 1, "create table %s(", table);
761-
762-
for (i = 0; i < no_of_cols; i++)
763-
{
764-
Log(LOG_LEVEL_VERBOSE, "Forming column template %s %s %d", name_table[i], type_table[i],
765-
size_table[i]);;
759+
Log(LOG_LEVEL_ERR, "Attempted to create table '%s' without any columns", table);
760+
return false;
761+
}
766762

767-
if (size_table[i] > 0)
768-
{
769-
snprintf(entry, CF_MAXVARSIZE - 1, "%s %s(%d)", name_table[i], type_table[i], size_table[i]);
770-
}
771-
else
772-
{
773-
snprintf(entry, CF_MAXVARSIZE - 1, "%s %s", name_table[i], type_table[i]);
774-
}
763+
Buffer *query = BufferNew();
764+
BufferPrintf(query, "create table %s(", table);
775765

776-
strcat(query, entry);
766+
for (i = 0; i < no_of_cols; i++)
767+
{
768+
Log(LOG_LEVEL_VERBOSE, "Forming column template %s %s %d", name_table[i], type_table[i],
769+
size_table[i]);;
777770

778-
if (i < no_of_cols - 1)
779-
{
780-
strcat(query, ",");
781-
}
771+
if (size_table[i] > 0)
772+
{
773+
BufferAppendF(query, "%s %s(%d)", name_table[i], type_table[i], size_table[i]);
774+
}
775+
else
776+
{
777+
BufferAppendF(query, "%s %s", name_table[i], type_table[i]);
782778
}
783779

784-
strcat(query, ")");
780+
if (i < no_of_cols - 1)
781+
{
782+
BufferAppendChar(query, ',');
783+
}
785784
}
786785

787-
CfVoidQueryDB(cfdb, query);
786+
BufferAppendChar(query, ')');
787+
788+
CfVoidQueryDB(cfdb, BufferData(query));
789+
BufferDestroy(query);
788790
DeleteSQLColumns(name_table, type_table, size_table, done, no_of_cols);
789791
return true;
790792
}

0 commit comments

Comments
 (0)