[64304] trunk/base/src
jmr at macports.org
jmr at macports.org
Mon Mar 1 09:35:48 PST 2010
Revision: 64304
http://trac.macports.org/changeset/64304
Author: jmr at macports.org
Date: 2010-03-01 09:35:46 -0800 (Mon, 01 Mar 2010)
Log Message:
-----------
error checking, use realloc
Modified Paths:
--------------
trunk/base/src/cregistry/entry.c
trunk/base/src/cregistry/registry.c
trunk/base/src/registry2.0/entry.c
trunk/base/src/registry2.0/graph.c
trunk/base/src/registry2.0/item.c
trunk/base/src/registry2.0/registry.c
trunk/base/src/registry2.0/util.c
Modified: trunk/base/src/cregistry/entry.c
===================================================================
--- trunk/base/src/cregistry/entry.c 2010-03-01 13:54:13 UTC (rev 64303)
+++ trunk/base/src/cregistry/entry.c 2010-03-01 17:35:46 UTC (rev 64304)
@@ -78,21 +78,24 @@
* @param [in,out] dst_space number of characters `dst` can hold
* @param [in] src string to concatenate to `dst`
*/
-void reg_strcat(char** dst, int* dst_len, int* dst_space, char* src) {
+static int reg_strcat(char** dst, int* dst_len, int* dst_space, char* src) {
int src_len = strlen(src);
int result_len = *dst_len + src_len;
if (result_len >= *dst_space) {
- char* old_dst = *dst;
+ char* new_dst;
*dst_space *= 2;
if (*dst_space < result_len) {
*dst_space = result_len;
}
- *dst = malloc(*dst_space * sizeof(char) + 1);
- memcpy(*dst, old_dst, *dst_len);
- free(old_dst);
+ new_dst = realloc(*dst, *dst_space * sizeof(char) + 1);
+ if (!new_dst)
+ return 0;
+ else
+ *dst = new_dst;
}
memcpy(*dst + *dst_len, src, src_len+1);
*dst_len = result_len;
+ return 1;
}
/**
@@ -104,16 +107,19 @@
* @param [in,out] dst_space number of elements `dst` can hold
* @param [in] src elements to append to `dst`
*/
-static void reg_listcat(void*** dst, int* dst_len, int* dst_space, void* src) {
+static int reg_listcat(void*** dst, int* dst_len, int* dst_space, void* src) {
if (*dst_len == *dst_space) {
- void** old_dst = *dst;
+ void** new_dst;
*dst_space *= 2;
- *dst = malloc(*dst_space * sizeof(void*));
- memcpy(*dst, old_dst, *dst_len);
- free(old_dst);
+ new_dst = realloc(*dst, *dst_space * sizeof(void*));
+ if (!new_dst)
+ return 0;
+ else
+ *dst = new_dst;
}
(*dst)[*dst_len] = src;
(*dst_len)++;
+ return 1;
}
/**
@@ -148,8 +154,8 @@
* @param [in] userdata sqlite3 database
* @param [out] entry entry described by `stmt`
* @param [in] stmt `sqlite3_stmt` with appropriate columns
- * @param [out] errPtr unused, since this function doesn't fail
- * @return true, since this function doesn't fail
+ * @param [out] errPtr unused
+ * @return true if success; false if failure
*/
static int reg_stmt_to_entry(void* userdata, void** entry, void* stmt,
reg_error* errPtr UNUSED) {
@@ -160,6 +166,9 @@
(const char*)&id, &is_new);
if (is_new) {
reg_entry* e = malloc(sizeof(reg_entry));
+ if (!e) {
+ return 0;
+ }
e->reg = reg;
e->id = id;
e->proc = NULL;
@@ -211,12 +220,14 @@
switch (r) {
case SQLITE_DONE:
entry = malloc(sizeof(reg_entry));
- entry->id = sqlite3_last_insert_rowid(reg->db);
- entry->reg = reg;
- entry->proc = NULL;
- hash = Tcl_CreateHashEntry(®->open_entries,
- (const char*)&entry->id, &is_new);
- Tcl_SetHashValue(hash, entry);
+ if (entry) {
+ entry->id = sqlite3_last_insert_rowid(reg->db);
+ entry->reg = reg;
+ entry->proc = NULL;
+ hash = Tcl_CreateHashEntry(®->open_entries,
+ (const char*)&entry->id, &is_new);
+ Tcl_SetHashValue(hash, entry);
+ }
break;
case SQLITE_BUSY:
break;
@@ -405,6 +416,9 @@
int result_count = 0;
int result_space = 10;
sqlite3_stmt* stmt;
+ if (!results) {
+ return -1;
+ }
if (sqlite3_prepare(reg->db, query, query_len, &stmt, NULL) == SQLITE_OK) {
int r;
reg_entry* entry;
@@ -413,8 +427,9 @@
switch (r) {
case SQLITE_ROW:
if (fn(reg, (void**)&entry, stmt, errPtr)) {
- reg_listcat(&results, &result_count, &result_space,
- entry);
+ if (!reg_listcat(&results, &result_count, &result_space, entry)) {
+ r = SQLITE_ERROR;
+ }
} else {
r = SQLITE_ERROR;
}
@@ -492,10 +507,16 @@
}
/* build the query */
query = strdup("SELECT id FROM registry.ports");
+ if (!query) {
+ return -1;
+ }
for (i=0; i<key_count; i++) {
char* cond = sqlite3_mprintf(op, keys[i], vals[i]);
- reg_strcat(&query, &query_len, &query_space, kwd);
- reg_strcat(&query, &query_len, &query_space, cond);
+ if (!cond || !reg_strcat(&query, &query_len, &query_space, kwd)
+ || !reg_strcat(&query, &query_len, &query_space, cond)) {
+ free(query);
+ return -1;
+ }
sqlite3_free(cond);
kwd = " AND ";
}
@@ -893,6 +914,9 @@
int r;
const char *text;
char* element;
+ if (!result) {
+ return -1;
+ }
do {
r = sqlite3_step(stmt);
switch (r) {
@@ -900,8 +924,9 @@
text = (const char*)sqlite3_column_text(stmt, 0);
if (text) {
element = strdup(text);
- reg_listcat((void***)&result, &result_count, &result_space,
- element);
+ if (!element || !reg_listcat((void***)&result, &result_count, &result_space, element)) {
+ r = SQLITE_ERROR;
+ }
}
break;
case SQLITE_DONE:
@@ -955,6 +980,9 @@
int r;
const char *text;
char* element;
+ if (!result) {
+ return -1;
+ }
do {
r = sqlite3_step(stmt);
switch (r) {
@@ -962,8 +990,9 @@
text = (const char*)sqlite3_column_text(stmt, 0);
if (text) {
element = strdup(text);
- reg_listcat((void***)&result, &result_count, &result_space,
- element);
+ if (!element || !reg_listcat((void***)&result, &result_count, &result_space, element)) {
+ r = SQLITE_ERROR;
+ }
}
break;
case SQLITE_DONE:
@@ -1242,7 +1271,7 @@
*
* @param [in] reg registry to fetch entries from
* @param [out] entries a list of open entries
- * @return the number of open entries
+ * @return the number of open entries, -1 on error
*/
int reg_all_open_entries(reg_registry* reg, reg_entry*** entries) {
reg_entry* entry;
@@ -1251,10 +1280,16 @@
Tcl_HashEntry* hash;
Tcl_HashSearch search;
*entries = malloc(10*sizeof(void*));
+ if (!*entries) {
+ return -1;
+ }
for (hash = Tcl_FirstHashEntry(®->open_entries, &search); hash != NULL;
hash = Tcl_NextHashEntry(&search)) {
entry = Tcl_GetHashValue(hash);
- reg_listcat((void***)entries, &entry_count, &entry_space, entry);
+ if (!reg_listcat((void***)entries, &entry_count, &entry_space, entry)) {
+ free(*entries);
+ return -1;
+ }
}
return entry_count;
}
Modified: trunk/base/src/cregistry/registry.c
===================================================================
--- trunk/base/src/cregistry/registry.c 2010-03-01 13:54:13 UTC (rev 64303)
+++ trunk/base/src/cregistry/registry.c 2010-03-01 17:35:46 UTC (rev 64304)
@@ -104,6 +104,9 @@
*/
int reg_open(reg_registry** regPtr, reg_error* errPtr) {
reg_registry* reg = malloc(sizeof(reg_registry));
+ if (!reg) {
+ return 0;
+ }
if (sqlite3_open(NULL, ®->db) == SQLITE_OK) {
if (init_db(reg->db, errPtr)) {
reg->status = reg_none;
Modified: trunk/base/src/registry2.0/entry.c
===================================================================
--- trunk/base/src/registry2.0/entry.c 2010-03-01 13:54:13 UTC (rev 64303)
+++ trunk/base/src/registry2.0/entry.c 2010-03-01 17:35:46 UTC (rev 64304)
@@ -151,8 +151,13 @@
if (list_handle) {
entry_list* list = *list_handle;
*list_handle = malloc(sizeof(entry_list*));
- (*list_handle)->entry = entry;
- (*list_handle)->next = list;
+ if (*list_handle) {
+ (*list_handle)->entry = entry;
+ (*list_handle)->next = list;
+ } else {
+ reg_entry_free(entry);
+ return TCL_ERROR;
+ }
} else {
reg_entry_free(entry);
}
@@ -280,6 +285,9 @@
}
keys = malloc(key_count * sizeof(char*));
vals = malloc(key_count * sizeof(char*));
+ if (!keys || !vals) {
+ return TCL_ERROR;
+ }
for (i=0; i<key_count; i+=1) {
keys[i] = Tcl_GetString(objv[2*i+start]);
vals[i] = Tcl_GetString(objv[2*i+start+1]);
Modified: trunk/base/src/registry2.0/graph.c
===================================================================
--- trunk/base/src/registry2.0/graph.c 2010-03-01 13:54:13 UTC (rev 64303)
+++ trunk/base/src/registry2.0/graph.c 2010-03-01 17:35:46 UTC (rev 64304)
@@ -90,8 +90,11 @@
== SQLITE_OK)
&& (sqlite3_step(stmt) == SQLITE_DONE))) {
graph* g = malloc(sizeof(graph));
+ sqlite3_free(query);
+ if (!g) {
+ return TCL_ERROR;
+ }
g->db = db;
- sqlite3_free(query);
if (objc == 4) {
/* graph create dbfile name */
if (SetGraph(interp, Tcl_GetString(objv[3]), g) == TCL_OK) {
@@ -100,7 +103,7 @@
}
} else {
/* graph create dbfile; generate a name */
- char* name = UniqueName(interp, "registry::graph");
+ char* name = unique_name(interp, "::registry::graph");
if (SetGraph(interp, name, g) == TCL_OK) {
Tcl_Obj* res = Tcl_NewStringObj(name, -1);
Tcl_SetObjResult(interp, res);
Modified: trunk/base/src/registry2.0/item.c
===================================================================
--- trunk/base/src/registry2.0/item.c 2010-03-01 13:54:13 UTC (rev 64303)
+++ trunk/base/src/registry2.0/item.c 2010-03-01 17:35:46 UTC (rev 64304)
@@ -58,6 +58,9 @@
static int set_item(Tcl_Interp* interp, char* name, sqlite_int64 rowid) {
sqlite3* db = registry_db(interp, 0);
item_t* new_item = malloc(sizeof(item_t));
+ if (!new_item) {
+ return TCL_ERROR;
+ }
new_item->rowid = rowid;
new_item->db = db;
if (set_object(interp, name, new_item, "item", item_obj_cmd, delete_item)
@@ -102,7 +105,7 @@
}
} else {
/* item create */
- char* name = unique_name(interp, "registry::item");
+ char* name = unique_name(interp, "::registry::item");
if (set_item(interp, name, item) == TCL_OK) {
Tcl_Obj* res = Tcl_NewStringObj(name, -1);
Tcl_SetObjResult(interp, res);
@@ -155,7 +158,7 @@
char* query_start = "SELECT proc FROM items";
char* insert;
int insert_size = query_size - strlen(query_start);
- if (db == NULL) {
+ if (db == NULL || query == NULL) {
return TCL_ERROR;
}
strncpy(query, query_start, query_size);
Modified: trunk/base/src/registry2.0/registry.c
===================================================================
--- trunk/base/src/registry2.0/registry.c 2010-03-01 13:54:13 UTC (rev 64303)
+++ trunk/base/src/registry2.0/registry.c 2010-03-01 17:35:46 UTC (rev 64304)
@@ -81,6 +81,9 @@
reg_entry** entries;
int entry_count = reg_all_open_entries(reg, &entries);
int i;
+ if (entry_count == -1) {
+ return 0;
+ }
for (i=0; i<entry_count; i++) {
if (entries[i]->proc) {
Tcl_DeleteCommand(interp, entries[i]->proc);
Modified: trunk/base/src/registry2.0/util.c
===================================================================
--- trunk/base/src/registry2.0/util.c 2010-03-01 13:54:13 UTC (rev 64303)
+++ trunk/base/src/registry2.0/util.c 2010-03-01 17:35:46 UTC (rev 64304)
@@ -55,6 +55,8 @@
char* result = malloc(result_size);
Tcl_CmdInfo info;
int i;
+ if (!result)
+ return NULL;
for (i=0; ; i++) {
snprintf(result, result_size, "%s%d", prefix, i);
if (Tcl_GetCommandInfo(interp, result, &info) == 0) {
@@ -175,6 +177,9 @@
errPtr)) {
int size = strlen(name) + 1;
entry->proc = malloc(size*sizeof(char));
+ if (!entry->proc) {
+ return 0;
+ }
memcpy(entry->proc, name, size);
return 1;
}
@@ -219,6 +224,9 @@
while (sqlite3_step(stmt) == SQLITE_ROW) {
sqlite_int64 rowid = sqlite3_column_int64(stmt, 0);
char* name = unique_name(interp, prefix);
+ if (!name) {
+ return TCL_ERROR;
+ }
if (setter(interp, name, rowid) == TCL_OK) {
Tcl_Obj* element = Tcl_NewStringObj(name, -1);
Tcl_ListObjAppendElement(interp, result, element);
@@ -249,6 +257,9 @@
void** inv, int inc, reg_error* errPtr) {
void** result = malloc(inc*sizeof(void*));
int i;
+ if (!result) {
+ return 0;
+ }
for (i=0; i<inc; i++) {
if (!fn(userdata, &result[i], inv[i], errPtr)) {
if (del != NULL) {
@@ -268,6 +279,9 @@
reg_error* errPtr) {
if (entry->proc == NULL) {
char* name = unique_name(interp, "::registry::entry");
+ if (!name) {
+ return 0;
+ }
if (!set_entry(interp, name, entry, errPtr)) {
free(name);
return 0;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20100301/3f7242c7/attachment.html>
More information about the macports-changes
mailing list