diff --git a/libdispatch/dfile.c b/libdispatch/dfile.c index f696e9bf3..d198d1f00 100644 --- a/libdispatch/dfile.c +++ b/libdispatch/dfile.c @@ -125,7 +125,7 @@ NC_check_file_type(const char *path, int use_parallel, void *mpi_info, i = fread(magic, MAGIC_NUMBER_LEN, 1, fp); fclose(fp); if(i != 1) - return errno; + return NC_ENOTNC; } /* Ignore the first byte for HDF */ @@ -140,7 +140,10 @@ NC_check_file_type(const char *path, int use_parallel, void *mpi_info, *cdf = 1; /* netcdf classic version 1 */ else if(magic[3] == '\002') *cdf = 2; /* netcdf classic version 2 */ - } + else + return NC_ENOTNC; + } else + return NC_ENOTNC; return NC_NOERR; } @@ -1567,8 +1570,8 @@ NC_open(const char *path, int cmode, } else if(cdfversion != 0) { model = NC_DISPATCH_NC3; } - } - /* else ignore the file */ + } else /* presumably not a netcdf file */ + return NC_ENOTNC; } /* Look to the incoming cmode for hints */ diff --git a/oc2/dapparselex.h b/oc2/dapparselex.h index 34aaf9f57..852a72122 100644 --- a/oc2/dapparselex.h +++ b/oc2/dapparselex.h @@ -7,7 +7,6 @@ #include "ocinternal.h" #include "ocdebug.h" - /* For consistency with Java parser */ #define null NULL diff --git a/oc2/ocinternal.c b/oc2/ocinternal.c index 532ab6e65..6410741c8 100644 --- a/oc2/ocinternal.c +++ b/oc2/ocinternal.c @@ -343,23 +343,24 @@ createtempfile1(char* tmppath, char** tmpnamep) { int fd = 0; char* tmpname = NULL; - tmpname = (char*)malloc(strlen(tmppath)+strlen("dataddsXXXXXX")+1); + int tmpsize = strlen(tmppath)+strlen("dataddsXXXXXX"); + tmpname = (char*)malloc(tmpsize+1); if(tmpname == NULL) return -1; - strcpy(tmpname,tmppath); + strncpy(tmpname,tmppath,tmpsize); #ifdef HAVE_MKSTEMP - strcat(tmpname,"dataddsXXXXXX"); + strncat(tmpname,"dataddsXXXXXX",strlen("dataddsXXXXXX"))); /* Note Potential problem: old versions of this function leave the file in mode 0666 instead of 0600 */ fd = mkstemp(tmpname); #else /* !HAVE_MKSTEMP */ /* Need to simulate by using some kind of pseudo-random number */ - strcat(tmpname,"datadds"); + strncat(tmpname,"datadds",strlen("datadds")); { int rno = rand(); char spid[7]; if(rno < 0) rno = -rno; - sprintf(spid,"%06d",rno); - strcat(tmpname,spid); + snprintf(spid,sizeof(spid),"%06d",rno); + strncat(tmpname,spid,strlen(spid)); # ifdef WIN32 fd=open(tmpname,O_RDWR|O_BINARY|O_CREAT|O_EXCL|FILE_ATTRIBUTE_TEMPORARY, _S_IREAD|_S_IWRITE); # else @@ -371,7 +372,6 @@ createtempfile1(char* tmppath, char** tmpnamep) if(tmpnamep) *tmpnamep = tmpname; else free(tmpname); - return fd; } @@ -557,8 +557,8 @@ ocsetcurlproperties(OCstate* state) } } if(state->curlflags.useragent == NULL) { - size_t len = strlen(DFALTUSERAGENT) + strlen(VERSION) + 1; - char* agent = (char*)malloc(len); + size_t len = strlen(DFALTUSERAGENT) + strlen(VERSION); + char* agent = (char*)malloc(len+1); snprintf(agent,len,"%s%s",DFALTUSERAGENT,VERSION); state->curlflags.useragent = agent; } diff --git a/oc2/ocinternal.h b/oc2/ocinternal.h index 991f83fcb..42ec1e6ab 100644 --- a/oc2/ocinternal.h +++ b/oc2/ocinternal.h @@ -13,10 +13,12 @@ #include #include #include -#ifndef WIN32 +#ifdef HAVE_STRINGS_H #include #endif +#ifdef HAVE_STDARG_H #include +#endif #ifdef HAVE_UNISTD_H #include #endif diff --git a/oc2/ocutil.c b/oc2/ocutil.c index a78b61c2c..12736f3a4 100644 --- a/oc2/ocutil.c +++ b/oc2/ocutil.c @@ -551,3 +551,47 @@ ocdtmodestring(OCDT mode,int compact) } return result; } + + +/* +Instead of using snprintf to concatenate +multiple strings into a given target, +provide a direct concatenator. +So, this function concatenates n strings +into dst, being careful to not overrun size. +Note that size is assumed to include the null +terminator and that in the event of overrun, +the string will have a null at dst[size-1]. +Return -1 if overrun, 0 otherwise. +*/ +int +occoncat(char* dst, size_t size, size_t n, ...) +{ + va_list args; + size_t avail = size - 1; + int i; + int status = 0; // assume ok + char* p = dst; + + if(n == 0) { + if(size > 0) + dst[0] = '\0'; + return (size > 0 ? 0: -1); + } + + va_start(args,n); + for(i=0;i