static char *data_object_to_tempfile(gpgme_data_t data, FILE **ret_fp)
{
int err;
- char tempfile[_POSIX_PATH_MAX];
+ char tempfile[_POSIX_PATH_MAX], buf[BUFSIZ];
FILE *fp;
ssize_t nread = 0;
err = ((gpgme_data_seek (data, 0, SEEK_SET) == -1)
? gpgme_error_from_errno (errno) : 0);
- if (!err) {
- char buf[4096];
+ if (err) {
+ mutt_perror(_("Can't create temporary file"));
+ goto error;
+ }
- while ((nread = gpgme_data_read(data, buf, sizeof(buf)))) {
- if (fwrite (buf, nread, 1, fp) != 1) {
- mutt_perror (_("Can't create temporary file"));
- m_fclose(&fp);
- unlink (tempfile);
- return NULL;
- }
+ while ((nread = gpgme_data_read(data, buf, sizeof(buf)))) {
+ if (fwrite (buf, nread, 1, fp) != 1) {
+ mutt_perror (_("Can't create temporary file"));
+ goto error;
}
}
if (nread == -1) {
mutt_error (_("error reading data object: %s\n"), gpgme_strerror (err));
- unlink (tempfile);
- m_fclose(&fp);
- return NULL;
+ goto error;
}
if (ret_fp) {
rewind(fp);
m_fclose(&fp);
}
return m_strdup(tempfile);
+
+ error:
+ m_fclose(&fp);
+ unlink (tempfile);
+ return NULL;
}