From: mdw Date: Wed, 5 Oct 2005 09:40:35 +0000 (+0000) Subject: Fix segfault in retagging. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/694fc3a8ae67caa4d57f043c0d66e6928a7ce299 Fix segfault in retagging. --- diff --git a/keyutil.c b/keyutil.c index 6146ae4..269a85d 100644 --- a/keyutil.c +++ b/keyutil.c @@ -1166,7 +1166,9 @@ static int cmd_add(int argc, char *argv[]) int err; key *kk; if (k.f & f_retag) { - if ((kk = key_bytag(&f, tag)) != 0 && strcmp(kk->tag, tag) == 0) + if ((kk = key_bytag(&f, tag)) != 0 && + kk->tag && + strcmp(kk->tag, tag) == 0) key_settag(&f, kk, 0); } if ((err = key_settag(&f, k.k, tag)) != 0) @@ -1929,6 +1931,8 @@ static int cmd_extract(int argc, char *argv[]) int i; int rc = 0; key_filter kf = { 0, 0 }; + dstr d = DSTR_INIT; + const char *outfile = 0; FILE *fp; for (;;) { @@ -1957,9 +1961,13 @@ static int cmd_extract(int argc, char *argv[]) die(EXIT_FAILURE, "Usage: extract [-f FILTER] FILE [TAG...]"); if (strcmp(*argv, "-") == 0) fp = stdout; - else if (!(fp = fopen(*argv, "w"))) { - die(EXIT_FAILURE, "couldn't open `%s' for writing: %s", - *argv, strerror(errno)); + else { + outfile = *argv; + dstr_putf(&d, "%s.new", outfile); + if (!(fp = fopen(d.buf, "w"))) { + die(EXIT_FAILURE, "couldn't open `%s' for writing: %s", + d.buf, strerror(errno)); + } } doopen(&f, KOPEN_READ); @@ -1978,8 +1986,9 @@ static int cmd_extract(int argc, char *argv[]) } } } - if (fclose(fp)) + if (fclose(fp) || (outfile && rename(d.buf, outfile))) die(EXIT_FAILURE, "error writing file: %s", strerror(errno)); + dstr_destroy(&d); doclose(&f); return (rc); }