netcdf-c/ncgen/generr.c
Dennis Heimbigner 751300ec59 Fix more memory leaks in netcdf-c library
This is a follow up to PR https://github.com/Unidata/netcdf-c/pull/1173

Sorry that it is so big, but leak suppression can be complex.

This PR fixes all remaining memory leaks -- as determined by
-fsanitize=address, and with the exceptions noted below.

Unfortunately. there remains a significant leak that I cannot
solve. It involves vlens, and it is unclear if the leak is
occurring in the netcdf-c library or the HDF5 library.

I have added a check_PROGRAM to the ncdump directory to show the
problem.  The program is called tst_vlen_demo.c To exercise it,
build the netcdf library with -fsanitize=address enabled. Then
go into ncdump and do a "make clean check".  This should build
tst_vlen_demo without actually executing it.  Then do the
command "./tst_vlen_demo" to see the output of the memory
checker.  Note the the lost malloc is deep in the HDF5 library
(in H5Tvlen.c).

I am temporarily working around this error in the following way.
1. I modified several test scripts to not execute known vlen tests
   that fail as described above.
2. Added an environment variable called NC_VLEN_NOTEST.
   If set, then those specific tests are suppressed.

This should mean that the --disable-utilities option to
./configure should not need to be set to get a memory leak clean
build.  This should allow for detection of any new leaks.

Note: I used an environment variable rather than a ./configure
option to control the vlen tests. This is because it is
temporary (I hope) and because it is a bit tricky for shell
scripts to access ./configure options.

Finally, as before, this only been tested with netcdf-4 and hdf5 support.
2018-11-15 10:00:38 -07:00

127 lines
2.6 KiB
C

/*********************************************************************
* Copyright 1993, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/ncgen/generr.c,v 1.1 2009/09/25 18:22:22 dmh Exp $
*********************************************************************/
#include "includes.h"
#include <ctype.h> /* for isprint() */
int error_count;
#if 0
#define vastart(argv,fmt) va_start(argv,fmt)
#define vaend(argv,fmt) va_end(argv)
#endif
/*
* For logging error conditions.
* Designed to be called by other vararg procedures
*/
void
vderror(const char *fmt, va_list argv)
{
(void) vdwarn(fmt,argv);
error_count++;
}
/*
* For logging error conditions.
* Designed to be called by other vararg procedures
*/
void
vdwarn(const char *fmt, va_list argv)
{
(void) vfprintf(stderr,fmt,argv) ;
(void) fputc('\n',stderr) ;
(void) fflush(stderr); /* to ensure log files are current */
}
void
derror(const char *fmt, ...)
{
va_list argv;
va_start(argv,fmt);
vderror(fmt,argv);
}
/* Report version errors */
void
verror(const char *fmt, ...)
{
char newfmt[2048];
va_list argv;
va_start(argv,fmt);
strcpy(newfmt,"netCDF classic: not supported: ");
strncat(newfmt,fmt,2000);
vderror(newfmt,argv);
va_end(argv);
}
void
semwarn(const int lno, const char *fmt, ...)
{
va_list argv;
va_start(argv,fmt);
(void)fprintf(stderr,"%s: %s line %d: ", progname, cdlname, lno);
vdwarn(fmt,argv);
}
void
semerror(const int lno, const char *fmt, ...)
{
va_list argv;
va_start(argv,fmt);
(void)fprintf(stderr,"%s: %s line %d: ", progname, cdlname, lno);
vderror(fmt,argv);
finalize_netcdf(1); /* immediately fatal */
}
/* Capture potential version errors */
static char* markcdf4_msg = NULL;
void
markcdf4(const char* msg)
{
enhanced_flag = 1;
if(markcdf4_msg == NULL)
markcdf4_msg = (char*)msg;
}
char*
getmarkcdf4(void)
{
return markcdf4_msg;
}
/* Capture potential version errors */
static char* markcdf5_msg = NULL;
void
markcdf5(const char* msg)
{
cdf5_flag = 1;
if(markcdf5_msg == NULL)
markcdf5_msg = (char*)msg;
}
char*
getmarkcdf5(void)
{
return markcdf5_msg;
}
/**************************************************/
/* Provide a version of snprintf that panics if*/
/* the buffer is overrun*/
void
nprintf(char* buffer, size_t size, const char *fmt, ...)
{
long written;
va_list args;
va_start(args,fmt);
written = vsnprintf(buffer,size,fmt,args);
if(written < 0 || written >= size) {
PANIC("snprintf failure");
}
}