Bug in iobuf_cancel() (patch included)

Michael Roth mroth at nessie.de
Sun Sep 6 01:18:31 CEST 1998


Hi,

their is a bug in iobuf_cancel(): When you create a IOBUF for stdout with
iobuf_create() then this IOBUF get the filename "[stdout]" assigned.
Then at some time later if iobuf_cancel() get called on this IOBUF in case
of a failure or so, iobuf_cancel() try to remove a file named "[stdout]". 
If their is a file with this name in the current directory say goodbye to
this file... :-\

I included a patch that adds a new flag to the file_filter_ctx_t to
determine if the filename is really a true filename.


cu
		Michael


-------------- next part --------------
diff -ur gnupg-0.3.4/include/iobuf.h gnupg-0.3.4.new/include/iobuf.h
--- gnupg-0.3.4/include/iobuf.h	Thu Jul  2 00:46:15 1998
+++ gnupg-0.3.4.new/include/iobuf.h	Sat Sep  5 23:05:19 1998
@@ -103,6 +103,8 @@
 
 u32 iobuf_get_filelength( IOBUF a );
 const char *iobuf_get_fname( IOBUF a );
+const char *iobuf_get_fname_realfname( IOBUF a, int );
+
 
 void iobuf_set_block_mode( IOBUF a, size_t n );
 void iobuf_set_partial_block_mode( IOBUF a, size_t len );
diff -ur gnupg-0.3.4/util/iobuf.c gnupg-0.3.4.new/util/iobuf.c
--- gnupg-0.3.4/util/iobuf.c	Tue Aug  4 03:18:01 1998
+++ gnupg-0.3.4.new/util/iobuf.c	Sat Sep  5 23:22:48 1998
@@ -34,6 +34,7 @@
 
 typedef struct {
     FILE *fp;	   /* open file handle */
+    int flags;	   /* 0x01 = name of the file isn't the real name ([stdin], [stdout]) */
     char fname[1]; /* name of the file */
 } file_filter_ctx_t ;
 
@@ -451,7 +452,7 @@
     const char *s;
 
     if( a && a->usage == 2 ) {
-	s = iobuf_get_fname(a);
+	s = iobuf_get_fname_realfname(a, 1);
 	if( s && *s )
 	    remove(s);	/* remove the file. Fixme: this will fail for MSDOZE*/
     }			/* because the file is still open */
@@ -486,17 +487,25 @@
     FILE *fp;
     file_filter_ctx_t *fcx;
     size_t len;
+    const char *logic_fname;
+    int fname_flag;
 
     if( !fname || (*fname=='-' && !fname[1])  ) {
 	fp = stdin; /* fixme: set binary mode for msdoze */
-	fname = "[stdin]";
+	logic_fname = "[stdin]";
+	fname_flag = 0x01;
+    }
+    else {
+        if( !(fp = fopen(fname, "rb")) )
+	    return NULL;
+	logic_fname = fname;
+	fname_flag = 0x00;
     }
-    else if( !(fp = fopen(fname, "rb")) )
-	return NULL;
     a = iobuf_alloc(1, 8192 );
-    fcx = m_alloc( sizeof *fcx + strlen(fname) );
+    fcx = m_alloc( sizeof *fcx + strlen(logic_fname) );
     fcx->fp = fp;
-    strcpy(fcx->fname, fname );
+    fcx->flags = fname_flag;
+    strcpy(fcx->fname, logic_fname );
     a->filter = file_filter;
     a->filter_ov = fcx;
     file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len );
@@ -517,17 +526,26 @@
     FILE *fp;
     file_filter_ctx_t *fcx;
     size_t len;
+    const char *logic_fname;
+    int fname_flag;
+    
 
     if( !fname || (*fname=='-' && !fname[1]) ) {
 	fp = stdout;
-	fname = "[stdout]";
+	logic_fname = "[stdout]";
+	fname_flag = 0x01;
+    }
+    else {
+        if( !(fp = fopen(fname, "wb")) )
+	    return NULL;
+	logic_fname = fname;
+	fname_flag = 0x00;
     }
-    else if( !(fp = fopen(fname, "wb")) )
-	return NULL;
     a = iobuf_alloc(2, 8192 );
-    fcx = m_alloc( sizeof *fcx + strlen(fname) );
+    fcx = m_alloc( sizeof *fcx + strlen(logic_fname) );
     fcx->fp = fp;
-    strcpy(fcx->fname, fname );
+    fcx->flags = fname_flag;
+    strcpy(fcx->fname, logic_fname );
     a->filter = file_filter;
     a->filter_ov = fcx;
     file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len );
@@ -557,6 +575,7 @@
     a = iobuf_alloc(2, 8192 );
     fcx = m_alloc( sizeof *fcx + strlen(fname) );
     fcx->fp = fp;
+    fcx->flags = 0x00;
     strcpy(fcx->fname, fname );
     a->filter = file_filter;
     a->filter_ov = fcx;
@@ -583,6 +602,7 @@
     a = iobuf_alloc(2, 8192 );
     fcx = m_alloc( sizeof *fcx + strlen(fname) );
     fcx->fp = fp;
+    fcx->flags = 0x00;
     strcpy(fcx->fname, fname );
     a->filter = file_filter;
     a->filter_ov = fcx;
@@ -1089,16 +1109,28 @@
  * Retrieve the filename
  */
 const char *
-iobuf_get_fname( IOBUF a )
+iobuf_get_fname_realfname( IOBUF a, int only_realfname )
 {
     for( ; a; a = a->chain )
 	if( !a->chain && a->filter == file_filter ) {
 	    file_filter_ctx_t *b = a->filter_ov;
-	    return b->fname;
+	    if ( only_realfname && (b->flags & 0x01) )
+	        return NULL;
+	    else
+	        return b->fname;
 	}
 
     return NULL;
 }
+
+const char *
+iobuf_get_fname( IOBUF a )
+{
+   return iobuf_get_fname_realfname(a, 0);
+}
+
+
+
 
 /****************
  * Start the block write mode, see rfc1991.new for details.


More information about the Gnupg-devel mailing list