1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-23 00:08:50 -05:00

pacman: use dynamic string allocation where it makes sense

None of these are hot-code paths, and at least the target reading has
little need for an arbitrary length limitation (however crazy it might
be to have longer arguments).

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2011-09-19 19:57:29 -05:00
parent 0f92fc5963
commit 8e3b39a9e0
3 changed files with 33 additions and 10 deletions

View File

@ -821,13 +821,13 @@ int main(int argc, char *argv[])
/* we support reading targets from stdin if a cmdline parameter is '-' */ /* we support reading targets from stdin if a cmdline parameter is '-' */
if(!isatty(fileno(stdin)) && alpm_list_find_str(pm_targets, "-")) { if(!isatty(fileno(stdin)) && alpm_list_find_str(pm_targets, "-")) {
char line[PATH_MAX]; size_t current_size = PATH_MAX, i = 0;
int i = 0; char *line = malloc(current_size);
/* remove the '-' from the list */ /* remove the '-' from the list */
pm_targets = alpm_list_remove_str(pm_targets, "-", NULL); pm_targets = alpm_list_remove_str(pm_targets, "-", NULL);
while(i < PATH_MAX && (line[i] = (char)fgetc(stdin)) != EOF) { while((line[i] = (char)fgetc(stdin)) != EOF) {
if(isspace((unsigned char)line[i])) { if(isspace((unsigned char)line[i])) {
/* avoid adding zero length arg when multiple spaces separate args */ /* avoid adding zero length arg when multiple spaces separate args */
if(i > 0) { if(i > 0) {
@ -837,11 +837,23 @@ int main(int argc, char *argv[])
} }
} else { } else {
i++; i++;
/* we may be at the end of our allocated buffer now */
if(i >= current_size) {
char *new = realloc(line, current_size * 2);
if(new) {
line = new;
current_size *= 2;
} else {
free(line);
line = NULL;
break;
}
}
} }
} }
/* check for buffer overflow */ /* check for memory exhaustion */
if(i >= PATH_MAX) { if(!line) {
pm_printf(ALPM_LOG_ERROR, _("buffer overflow detected in arg parsing\n")); pm_printf(ALPM_LOG_ERROR, _("memory exhausted in argument parsing\n"));
cleanup(EXIT_FAILURE); cleanup(EXIT_FAILURE);
} }
@ -850,6 +862,7 @@ int main(int argc, char *argv[])
line[i] = '\0'; line[i] = '\0';
pm_targets = alpm_list_add(pm_targets, strdup(line)); pm_targets = alpm_list_add(pm_targets, strdup(line));
} }
free(line);
if(!freopen(ctermid(NULL), "r", stdin)) { if(!freopen(ctermid(NULL), "r", stdin)) {
pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"), pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
strerror(errno)); strerror(errno));

View File

@ -122,7 +122,7 @@ static int sync_cleandb(const char *dbpath, int keep_used)
static int sync_cleandb_all(void) static int sync_cleandb_all(void)
{ {
const char *dbpath; const char *dbpath;
char newdbpath[PATH_MAX]; char *newdbpath;
int ret = 0; int ret = 0;
dbpath = alpm_option_get_dbpath(config->handle); dbpath = alpm_option_get_dbpath(config->handle);
@ -135,8 +135,12 @@ static int sync_cleandb_all(void)
* only the unused sync dbs in dbpath/sync/ */ * only the unused sync dbs in dbpath/sync/ */
ret += sync_cleandb(dbpath, 0); ret += sync_cleandb(dbpath, 0);
sprintf(newdbpath, "%s%s", dbpath, "sync/"); if(asprintf(&newdbpath, "%s%s", dbpath, "sync/") < 0) {
ret += 1;
return ret;
}
ret += sync_cleandb(newdbpath, 1); ret += sync_cleandb(newdbpath, 1);
free(newdbpath);
printf(_("Database directory cleaned up\n")); printf(_("Database directory cleaned up\n"));
return ret; return ret;

View File

@ -680,8 +680,9 @@ void signature_display(const char *title, alpm_siglist_t *siglist)
} else { } else {
size_t i; size_t i;
for(i = 0; i < siglist->count; i++) { for(i = 0; i < siglist->count; i++) {
char sigline[PATH_MAX]; char *sigline;
const char *status, *validity, *name; const char *status, *validity, *name;
int ret;
alpm_sigresult_t *result = siglist->results + i; alpm_sigresult_t *result = siglist->results + i;
/* Don't re-indent the first result */ /* Don't re-indent the first result */
if(i != 0) { if(i != 0) {
@ -726,10 +727,15 @@ void signature_display(const char *title, alpm_siglist_t *siglist)
break; break;
} }
name = result->key.uid ? result->key.uid : result->key.fingerprint; name = result->key.uid ? result->key.uid : result->key.fingerprint;
snprintf(sigline, PATH_MAX, _("%s, %s from \"%s\""), ret = pm_asprintf(&sigline, _("%s, %s from \"%s\""),
status, validity, name); status, validity, name);
if(ret < 1) {
pm_fprintf(stderr, ALPM_LOG_ERROR, _("failed to allocate string\n"));
continue;
}
indentprint(sigline, len); indentprint(sigline, len);
printf("\n"); printf("\n");
free(sigline);
} }
} }
} }