@@ -118,7 +118,7 @@ void RestoreArchive(Archive* AHX, RestoreOptions *ropt)
118118_printTocEntry (AH ,te ,ropt );
119119
120120if (AH -> PrintTocDataPtr != NULL && (reqs & 2 )!= 0 ) {
121- #ifndef HAVE_ZLIB
121+ #ifndef HAVE_LIBZ
122122if (AH -> compression != 0 )
123123die_horribly ("%s: Unable to restore data from a compressed archive\n" ,progname );
124124#endif
@@ -415,11 +415,14 @@ int archprintf(Archive* AH, const char *fmt, ...)
415415{
416416char * p = NULL ;
417417va_list ap ;
418- int bSize = strlen (fmt )+ 1024 ;
418+ int bSize = strlen (fmt )+ 256 ;
419419int cnt = -1 ;
420420
421421va_start (ap ,fmt );
422- while (cnt < 0 ) {
422+
423+ /* This is paranoid: deal with the possibility that vsnprintf is willing to ignore trailing null */
424+ /* or returns > 0 even if string does not fit. It may be the case that it returns cnt = bufsize */
425+ while (cnt < 0 || cnt >= (bSize - 1 ) ) {
423426if (p != NULL )free (p );
424427bSize *=2 ;
425428if ((p = malloc (bSize ))== NULL )
@@ -443,7 +446,7 @@ int archprintf(Archive* AH, const char *fmt, ...)
443446OutputContext SetOutput (ArchiveHandle * AH ,char * filename ,int compression )
444447{
445448OutputContext sav ;
446- #ifdef HAVE_ZLIB
449+ #ifdef HAVE_LIBZ
447450char fmode [10 ];
448451#endif
449452int fn = 0 ;
@@ -464,7 +467,7 @@ OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression)
464467 }
465468
466469/* If compression explicitly requested, use gzopen */
467- #ifdef HAVE_ZLIB
470+ #ifdef HAVE_LIBZ
468471if (compression != 0 )
469472 {
470473sprintf (fmode ,"wb%d" ,compression );
@@ -482,7 +485,7 @@ OutputContext SetOutput(ArchiveHandle* AH, char *filename, int compression)
482485AH -> OF = fopen (filename ,PG_BINARY_W );
483486}
484487AH -> gzOut = 0 ;
485- #ifdef HAVE_ZLIB
488+ #ifdef HAVE_LIBZ
486489 }
487490#endif
488491
@@ -509,11 +512,13 @@ int ahprintf(ArchiveHandle* AH, const char *fmt, ...)
509512{
510513char * p = NULL ;
511514va_list ap ;
512- int bSize = strlen (fmt )+ 1024 ;/* Should be enough */
515+ int bSize = strlen (fmt )+ 256 ;/* Should be enough */
513516int cnt = -1 ;
514517
515518va_start (ap ,fmt );
516- while (cnt < 0 ) {
519+ /* This is paranoid: deal with the possibility that vsnprintf is willing to ignore trailing null */
520+ /* or returns > 0 even if string does not fit. It may be the case that it returns cnt = bufsize */
521+ while (cnt < 0 || cnt >= (bSize - 1 ) ) {
517522if (p != NULL )free (p );
518523bSize *=2 ;
519524p = (char * )malloc (bSize );
@@ -681,6 +686,7 @@ int _discoverArchiveFormat(ArchiveHandle* AH)
681686int cnt ;
682687int wantClose = 0 ;
683688
689+
684690if (AH -> fSpec ) {
685691wantClose = 1 ;
686692fh = fopen (AH -> fSpec ,PG_BINARY_R );
@@ -693,16 +699,11 @@ int _discoverArchiveFormat(ArchiveHandle* AH)
693699
694700cnt = fread (sig ,1 ,5 ,fh );
695701
696- if (cnt != 5 ) {
697- fprintf (stderr ,"Archiver: input file is too short, or is unreadable\n" );
698- exit (1 );
699- }
702+ if (cnt != 5 )
703+ die_horribly ("%s: input file is too short, or is unreadable\n" ,progname );
700704
701705if (strncmp (sig ,"PGDMP" ,5 )!= 0 )
702- {
703- fprintf (stderr ,"Archiver: input file does not appear to be a valid archive\n" );
704- exit (1 );
705- }
706+ die_horribly ("%s: input file does not appear to be a valid archive\n" ,progname );
706707
707708AH -> vmaj = fgetc (fh );
708709AH -> vmin = fgetc (fh );
@@ -739,7 +740,7 @@ static ArchiveHandle* _allocAH(const char* FileSpec, ArchiveFormat fmt,
739740int compression ,ArchiveMode mode ) {
740741ArchiveHandle * AH ;
741742
742- AH = (ArchiveHandle * )malloc ( sizeof (ArchiveHandle ));
743+ AH = (ArchiveHandle * )calloc ( 1 , sizeof (ArchiveHandle ));
743744if (!AH )
744745die_horribly ("Archiver: Could not allocate archive handle\n" );
745746
@@ -759,7 +760,7 @@ static ArchiveHandle* _allocAH(const char* FileSpec, ArchiveFormat fmt,
759760AH -> currToc = NULL ;
760761AH -> currUser = "" ;
761762
762- AH -> toc = (TocEntry * )malloc ( sizeof (TocEntry ));
763+ AH -> toc = (TocEntry * )calloc ( 1 , sizeof (TocEntry ));
763764if (!AH -> toc )
764765die_horribly ("Archiver: Could not allocate TOC header\n" );
765766
@@ -996,7 +997,7 @@ void WriteHead(ArchiveHandle* AH)
996997 (* AH -> WriteBytePtr )(AH ,AH -> intSize );
997998 (* AH -> WriteBytePtr )(AH ,AH -> format );
998999
999- #ifndef HAVE_ZLIB
1000+ #ifndef HAVE_LIBZ
10001001if (AH -> compression != 0 )
10011002fprintf (stderr ,"%s: WARNING - requested compression not available in this installation - "
10021003"archive will be uncompressed \n" ,progname );
@@ -1016,43 +1017,43 @@ void ReadHead(ArchiveHandle* AH)
10161017char tmpMag [7 ];
10171018int fmt ;
10181019
1019- if (AH -> readHeader )
1020- return ;
1020+ if (!AH -> readHeader ) {
10211021
1022- (* AH -> ReadBufPtr )(AH ,tmpMag ,5 );
1022+ (* AH -> ReadBufPtr )(AH ,tmpMag ,5 );
10231023
1024- if (strncmp (tmpMag ,"PGDMP" ,5 )!= 0 )
1025- die_horribly ("Archiver: Did not fing magic PGDMP in file header\n" );
1024+ if (strncmp (tmpMag ,"PGDMP" ,5 )!= 0 )
1025+ die_horribly ("Archiver: Did not fing magic PGDMP in file header\n" );
10261026
1027- AH -> vmaj = (* AH -> ReadBytePtr )(AH );
1028- AH -> vmin = (* AH -> ReadBytePtr )(AH );
1027+ AH -> vmaj = (* AH -> ReadBytePtr )(AH );
1028+ AH -> vmin = (* AH -> ReadBytePtr )(AH );
10291029
1030- if (AH -> vmaj > 1 || ( (AH -> vmaj == 1 )&& (AH -> vmin > 0 ) ) )/* Version > 1.0 */
1031- {
1032- AH -> vrev = (* AH -> ReadBytePtr )(AH );
1033- }else {
1034- AH -> vrev = 0 ;
1035- }
1030+ if (AH -> vmaj > 1 || ( (AH -> vmaj == 1 )&& (AH -> vmin > 0 ) ) )/* Version > 1.0 */
1031+ {
1032+ AH -> vrev = (* AH -> ReadBytePtr )(AH );
1033+ }else {
1034+ AH -> vrev = 0 ;
1035+ }
10361036
1037- AH -> version = ( (AH -> vmaj * 256 + AH -> vmin )* 256 + AH -> vrev )* 256 + 0 ;
1037+ AH -> version = ( (AH -> vmaj * 256 + AH -> vmin )* 256 + AH -> vrev )* 256 + 0 ;
10381038
10391039
1040- if (AH -> version < K_VERS_1_0 || AH -> version > K_VERS_MAX )
1041- die_horribly ("Archiver: unsupported version (%d.%d) in file header\n" ,AH -> vmaj ,AH -> vmin );
1040+ if (AH -> version < K_VERS_1_0 || AH -> version > K_VERS_MAX )
1041+ die_horribly ("Archiver: unsupported version (%d.%d) in file header\n" ,AH -> vmaj ,AH -> vmin );
10421042
1043- AH -> intSize = (* AH -> ReadBytePtr )(AH );
1044- if (AH -> intSize > 32 )
1045- die_horribly ("Archiver: sanity check on integer size (%d) failes\n" ,AH -> intSize );
1043+ AH -> intSize = (* AH -> ReadBytePtr )(AH );
1044+ if (AH -> intSize > 32 )
1045+ die_horribly ("Archiver: sanity check on integer size (%d) failes\n" ,AH -> intSize );
10461046
1047- if (AH -> intSize > sizeof (int ))
1048- fprintf (stderr ,"\nWARNING: Backup file was made on a machine with larger integers, "
1049- "some operations may fail\n" );
1047+ if (AH -> intSize > sizeof (int ))
1048+ fprintf (stderr ,"\nWARNING: Backup file was made on a machine with larger integers, "
1049+ "some operations may fail\n" );
10501050
1051- fmt = (* AH -> ReadBytePtr )(AH );
1051+ fmt = (* AH -> ReadBytePtr )(AH );
10521052
1053- if (AH -> format != fmt )
1054- die_horribly ("Archiver: expected format (%d) differs from format found in file (%d)\n" ,
1055- AH -> format ,fmt );
1053+ if (AH -> format != fmt )
1054+ die_horribly ("Archiver: expected format (%d) differs from format found in file (%d)\n" ,
1055+ AH -> format ,fmt );
1056+ }
10561057
10571058if (AH -> version >=K_VERS_1_2 )
10581059 {
@@ -1061,8 +1062,9 @@ void ReadHead(ArchiveHandle* AH)
10611062AH -> compression = Z_DEFAULT_COMPRESSION ;
10621063 }
10631064
1064- #ifndef HAVE_ZLIB
1065- fprintf (stderr ,"%s: WARNING - archive is compressed - any data will not be available\n" ,progname );
1065+ #ifndef HAVE_LIBZ
1066+ if (AH -> compression != 0 )
1067+ fprintf (stderr ,"%s: WARNING - archive is compressed - any data will not be available\n" ,progname );
10661068#endif
10671069
10681070}