Use fewer hard-coded sizes

This commit is contained in:
Evan Miller 2020-12-28 22:04:06 -05:00
parent e1e5d9738e
commit c10232fdf1
4 changed files with 31 additions and 22 deletions

View File

@ -494,10 +494,10 @@ void mdb_data_dump(MdbTableDef *table)
{
unsigned int i;
int ret;
char *bound_values[MDB_MAX_COLS];
char **bound_values = calloc(table->num_cols, sizeof(char *));
for (i=0;i<table->num_cols;i++) {
bound_values[i] = (char *) g_malloc(256);
bound_values[i] = (char *) g_malloc(MDB_BIND_SIZE);
ret = mdb_bind_column(table, i+1, bound_values[i], NULL);
if (ret == -1) {
fprintf(stderr, "error binding column %d\n", i+1);
@ -516,6 +516,7 @@ void mdb_data_dump(MdbTableDef *table)
for (i=0;i<table->num_cols;i++) {
g_free(bound_values[i]);
}
free(bound_values);
}
int mdb_is_fixed_col(MdbColumn *col)

View File

@ -151,7 +151,7 @@ main(int argc, char **argv)
int i, row;
MdbHandle *mdb;
MdbTableDef *table;
MdbField fields[256];
MdbField *fields;
char line[MAX_ROW_SIZE];
int num_fields;
/* doesn't handle tables > 256 columns. Can that happen? */
@ -207,13 +207,14 @@ main(int argc, char **argv)
exit(1);
}
for (i=0;i<header_rows;i++)
if (!fgets(line, MAX_ROW_SIZE, in)) {
if (!fgets(line, sizeof(line), in)) {
fprintf(stderr, "Error while reading header column #%d. Check -H parameter.\n", i);
exit(1);
}
row = 1;
while (fgets(line, MAX_ROW_SIZE, in)) {
fields = calloc(table->num_cols, sizeof(MdbField));
while (fgets(line, sizeof(line), in)) {
num_fields = prep_row(table, line, fields, delimiter);
if (!num_fields) {
fprintf(stderr, "Aborting import at row %d\n", row);
@ -231,6 +232,7 @@ main(int argc, char **argv)
g_option_context_free(opt_context);
g_free(delimiter);
free(fields);
return 0;
}

View File

@ -25,7 +25,7 @@ main(int argc, char **argv)
{
MdbHandle *mdb;
MdbTableDef *table;
gchar name[256];
char *name;
gchar *propColName;
void *buf;
int col_num;
@ -54,10 +54,12 @@ main(int argc, char **argv)
mdb_read_columns(table);
mdb_rewind_table(table);
name = g_malloc(mdb->bind_size);
buf = g_malloc(mdb->bind_size);
mdb_bind_column_by_name(table, "Name", name, NULL);
buf = g_malloc(MDB_BIND_SIZE);
col_num = mdb_bind_column_by_name(table, propColName, buf, NULL);
if (col_num < 1) {
g_free(name);
g_free(buf);
mdb_free_tabledef(table);
mdb_close(mdb);
@ -83,6 +85,7 @@ main(int argc, char **argv)
free(kkd);
}
g_free(name);
g_free(buf);
mdb_free_tabledef(table);
mdb_close(mdb);

View File

@ -51,21 +51,21 @@ int main (int argc, char **argv) {
char *query_id;
size_t bind_size = QUERY_BIND_SIZE;
// variables for the msysqueries table. hopefully 256 is big enough
char *attribute = (char *) malloc(bind_size);
char *expression = (char *) malloc(bind_size);
char *flag = (char *) malloc(bind_size);
char *name1 = (char *) malloc(bind_size);
char *name2 = (char *) malloc(bind_size);
char *objectid = (char *) malloc(bind_size);
char *order = (char *) malloc(bind_size);
// variables for the msysqueries table
char *attribute = malloc(bind_size);
char *expression = malloc(bind_size);
char *flag = malloc(bind_size);
char *name1 = malloc(bind_size);
char *name2 = malloc(bind_size);
char *objectid = malloc(bind_size);
char *order = malloc(bind_size);
//variables for the generation of sql
char *sql_tables = (char *) malloc(bind_size);
char *sql_predicate = (char *) malloc(bind_size);
char *sql_columns = (char *) malloc(bind_size);
char *sql_where = (char *) malloc(bind_size);
char *sql_sorting = (char *) malloc(bind_size);
char *sql_tables = malloc(bind_size);
char *sql_predicate = malloc(bind_size);
char *sql_columns = malloc(bind_size);
char *sql_where = malloc(bind_size);
char *sql_sorting = malloc(bind_size);
int flagint;
/* see getopt(3) for more information on getopt and this will become clear */
@ -216,6 +216,7 @@ int main (int argc, char **argv) {
mdb_free_tabledef(table);
}
free(query_id);
} else {
fprintf(stderr,"Couldn't locate the specified query: %s\n",argv[optind+1]);
}
@ -269,8 +270,8 @@ char * mdb_get_query_id(MdbHandle *mdb, char *query) {
unsigned int i;
MdbCatalogEntry *entry = NULL;
MdbTableDef *table = NULL;
static char id[256];
char name[256];
char *id = malloc(mdb->bind_size);
char *name = malloc(mdb->bind_size);
/* loop over each entry in the catalog */
for (i=0; i < mdb->num_catalog; i++) {
@ -298,6 +299,8 @@ char * mdb_get_query_id(MdbHandle *mdb, char *query) {
}
mdb_free_tabledef(table);
}
free(name);
return id;
}