From ccc1c731529de16f6fa4064fd992a8f63d7cfc26 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 11 Jan 2008 00:01:58 -0600 Subject: Use dynamic string allocation in package structures This also affects all structures with static strings, such as depmiss, conflict, etc. This should help a lot with memory usage, and hopefully make things a bit more "idiot proof". Currently our pactest pass/fail rate is identical before and after this patch. This is not to say it is a perfect patch- I have yet to pull valgrind out. However, this should be quite safe to use in all situations from here on out, and we can start plugging the memleaks. Original-work-by: Aaron Griffin Signed-off-by: Dan McGee --- lib/libalpm/conflict.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'lib/libalpm/conflict.c') diff --git a/lib/libalpm/conflict.c b/lib/libalpm/conflict.c index c093705a..aec2080a 100644 --- a/lib/libalpm/conflict.c +++ b/lib/libalpm/conflict.c @@ -49,12 +49,19 @@ pmconflict_t *_alpm_conflict_new(const char *package1, const char *package2) MALLOC(conflict, sizeof(pmconflict_t), RET_ERR(PM_ERR_MEMORY, NULL)); - strncpy(conflict->package1, package1, PKG_NAME_LEN); - strncpy(conflict->package2, package2, PKG_NAME_LEN); + STRDUP(conflict->package1, package1, RET_ERR(PM_ERR_MEMORY, NULL)); + STRDUP(conflict->package2, package2, RET_ERR(PM_ERR_MEMORY, NULL)); return(conflict); } +void _alpm_conflict_free(pmconflict_t *conflict) +{ + FREE(conflict->package2); + FREE(conflict->package1); + FREE(conflict); +} + int _alpm_conflict_isin(pmconflict_t *needle, alpm_list_t *haystack) { alpm_list_t *i; @@ -110,7 +117,7 @@ static void add_conflict(alpm_list_t **baddeps, const char *pkg1, if(conflict && !_alpm_conflict_isin(conflict, *baddeps)) { *baddeps = alpm_list_add(*baddeps, conflict); } else { - FREE(conflict); + _alpm_conflict_free(conflict); } } @@ -293,15 +300,15 @@ static alpm_list_t *add_fileconflict(alpm_list_t *conflicts, const char* name1, const char* name2) { pmfileconflict_t *conflict; - MALLOC(conflict, sizeof(pmfileconflict_t), return(conflicts)); + MALLOC(conflict, sizeof(pmfileconflict_t), RET_ERR(PM_ERR_MEMORY, NULL)); conflict->type = type; - strncpy(conflict->target, name1, PKG_NAME_LEN); - strncpy(conflict->file, filestr, CONFLICT_FILE_LEN); + STRDUP(conflict->target, name1, RET_ERR(PM_ERR_MEMORY, NULL)); + STRDUP(conflict->file, filestr, RET_ERR(PM_ERR_MEMORY, NULL)); if(name2) { - strncpy(conflict->ctarget, name2, PKG_NAME_LEN); + STRDUP(conflict->ctarget, name2, RET_ERR(PM_ERR_MEMORY, NULL)); } else { - conflict->ctarget[0] = '\0'; + conflict->ctarget = ""; } conflicts = alpm_list_add(conflicts, conflict); @@ -311,6 +318,16 @@ static alpm_list_t *add_fileconflict(alpm_list_t *conflicts, return(conflicts); } +void _alpm_fileconflict_free(pmfileconflict_t *conflict) +{ + if(strlen(conflict->ctarget) > 0) { + FREE(conflict->ctarget); + } + FREE(conflict->file);; + FREE(conflict->target); + FREE(conflict); +} + /* Find file conflicts that may occur during the transaction with two checks: * 1: check every target against every target * 2: check every target against the filesystem */ -- cgit v1.2.3-24-g4f1b