[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [leafnode-list] Leafnode 2.0b8: XPAT causes segfault



Matthias Andree wrote:

> krasel@xxxxxxxxxxxxxxxxxxxxxxxxxxxx (Cornelius Krasel) writes:
> > Stefan Wiens <s.wi@xxxxxxx> writes:
> > > There are two additional details concerning the getheader() functions:
> > 
> > These were easy to fix (for a change :-).
> 
> Which way did you go? Use the first?

Yes.

Attached is a diff of my current version of artutil.c against the 2.0b8
version. It also incorporates the changes made by Matthias up to 2.0b8ma2.
As suggested on this list recently, all calls to strdup() have been
replaced by an own function critstrdup() which calls exit() if strdup()
returns NULL.

%snip
--- leafnode-2.0b8/artutil.c	Wed Jan 10 10:16:06 2001
+++ leafnode-current/artutil.c	Wed Jun 27 20:29:14 2001
@@ -16,6 +16,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <utime.h>
 
 char * lookup_xref( char * xref, const char * group );
 
@@ -66,22 +67,25 @@
     p = NULL;
     hlen = strlen( header );
     while ( ( p = getaline( f )) && *p ) {	/* read only headers */
-	/* allow for multiline headers */
 	if ( isspace((unsigned char)*p) && hdr && next ) {
+	    /* multiline header */
 	    hdr = critrealloc( hdr, strlen( hdr ) + strlen( p ) + 2,
 			     "Allocating space for multiline headers" );
-	    strcat( hdr , "\n" );
 	    strcat( hdr , p );
+	} else if (hdr) {
+	    /* we found the same header already */
+	    break;
 	} else if ( ( strncasecmp( p, header, hlen ) == 0 ) ) {
 	    p += hlen;
 	    if ( *p && *p == ':' )
 		p++;
 	    while ( p && *p && isspace((unsigned char)*p) )
 		p++;
-	    hdr = strdup( p );
+	    hdr = critstrdup(p, "fgetheader(): header");
 	    next = 1;
-	} else
+	} else {
 	    next = 0;
+	}
     }
     debug = debugmode;
     rewind( f );
@@ -91,14 +95,18 @@
 char * getheader( const char * filename, const char * header ) {
     FILE * f;
     char * hdr;
-    struct stat st ;
+    struct stat st;
+    struct utimbuf u;
 
     if ( stat( filename, &st ) || !S_ISREG( st.st_mode ) )
 	return NULL;
+    u.actime = st.st_atime;	/* save atime */
+    u.modtime = st.st_mtime;	/* save mtime */
     if ( ( f = fopen( filename, "r" ) ) == 0 )
 	return NULL;
     hdr = fgetheader( f, header );
     fclose( f );
+    utime(filename, &u);	/* restore mtime and atime */
     return hdr;
 }
 
@@ -136,8 +144,11 @@
 	fclose( outfile );
     } else
 	xref = NULL;
-    if ( ( outfile = fopen( outname, "w" ) ) == NULL )
+    if ( ( outfile = fopen( outname, "w" ) ) == NULL ) {
+	if ( xref )
+	    free(xref);
 	return;
+    }
 
     debug = 0;
     /* copy article headers except Xref: line */
@@ -155,17 +166,17 @@
 	q = strchr( p, ',' );
 	if ( q )
 	    *q++ = '\0';
-	if ( *p && ( isinteresting( p ) || create_all_links ) ) {
+	if ( *p && (isinteresting(p) || islocalgroup(p) || create_all_links) ) {
 	    cg = findgroup( p );
 	    if ( cg && chdirgroup( p, TRUE ) ) {
 		if (xref)
-		    cxref = strdup( xref );
+		    cxref = critstrdup(xref, "storearticle(): Xref");
 		if (( xrefno = lookup_xref( cxref, cg->name )) != NULL ) {
 		    strcpy( tmp, xrefno );
 		    link( outname, tmp );
 		} else {
 		    do {
-			sprintf( tmp, "%ld", ++cg->last );
+			snprintf( tmp, 10, "%ld", ++cg->last );
 			errno = 0;
 		    } while ( (link(outname, tmp) < 0) && errno==EEXIST );
 		    if ( errno )
@@ -196,7 +207,8 @@
     debug = debugmode;
     fclose( infile );
     fclose( outfile );
-    
+    free( xref );
+
     if ( ( ssmid = getheader( outname, "Supersedes" ) ) ) {
 	supersede( ssmid );
 	free( ssmid );
@@ -274,10 +286,10 @@
     }
     
     /* unlink the message-id hardlink */
-    if ( stat( filename, &st ) )
+    if ( stat(filename, &st) )
         syslog( LOG_ERR, "%s does not exist: %m", filename );
     else if ( st.st_nlink > 1 )
-        syslog( LOG_ERR, "%s: link count is %d", filename, st.st_nlink );
+        syslog( LOG_ERR, "%s: link count is %ld", filename, (long)st.st_nlink );
     else if ( unlink( filename ) )
         syslog( LOG_ERR, "Failed to unlink %s: %m", filename );
     else {
@@ -323,7 +335,7 @@
 	    }
 	    if ( cg ) {
 		do {
-		    sprintf(tmp, "%lu", ++cg->last);
+		    snprintf(tmp, 10, "%lu", ++cg->last);
 		    errno = 0;
 		    if ( verbose > 2 )
 			printf( "..as article %lu in %s\n",
@@ -333,7 +345,7 @@
 		    syslog( LOG_ERR, "error linking %s into %s: %m",
 			    filename, p );
 		else {
-		    sprintf( x, " %s:%lu", cg->name, cg->last );
+		    snprintf( x, 4096, " %s:%lu", cg->name, cg->last );
 		    x += strlen( x );
 		}
 	    } else {
%snip

--Cornelius.

-- 
/* Cornelius Krasel, U Wuerzburg, Dept. of Pharmacology, Versbacher Str. 9 */
/* D-97078 Wuerzburg, Germany   email: krasel@xxxxxxxxxxxxxxxxxxxxxxxxxxxx */
/* "Science is the game we play with God to find out what His rules are."  */

-- 
leafnode-list@xxxxxxxxxxxxxxxxxxxxxxxxxxxx -- mailing list for leafnode
To unsubscribe, send mail with "unsubscribe" in the subject to the list