summaryrefslogtreecommitdiffstats
path: root/src/H5system.c
diff options
context:
space:
mode:
authorQuincey Koziol <koziol@hdfgroup.org>2013-02-02 01:53:32 (GMT)
committerQuincey Koziol <koziol@hdfgroup.org>2013-02-02 01:53:32 (GMT)
commita3e98d0e36a0fbd6caae0a4a83863a78c2e25f50 (patch)
tree14e6c86e83d0b65e7033f9d31b0163404b41e36d /src/H5system.c
parent5140343f45312d4d2e486e6cc7645c7bc42b1267 (diff)
downloadhdf5-a3e98d0e36a0fbd6caae0a4a83863a78c2e25f50.zip
hdf5-a3e98d0e36a0fbd6caae0a4a83863a78c2e25f50.tar.gz
hdf5-a3e98d0e36a0fbd6caae0a4a83863a78c2e25f50.tar.bz2
[svn-r23219] Description:
Bring reviewed changes from Coverity branch back to trunk (QK & JK): r20457: Coverity issue 691: return of H5duo could be negative. Fixed by using STDOUT_FILENO and redesign parse_command_line and main to cleanup file allocations. The output_file var is null when using stdout. In cleanup do not close output_file if NULL. r20510: Initialize ufid = -1 and predicate HDclose call on ufid != -1 r20511: Purpose: Fix coverity issue 1715 Description: Free "file" and nested data on failure in H5FD_core_open. r20512: Initialize ifid = -1 and predicate HDclose call on ifid != -1 r20514: Initialize h5fid = -1 and predicate HDclose call on h5fid != -1 r20516: Added else branch to the if (ret_value < 0) check. r20522: Addressed coverity issues 930-933, 850, 836, 835, 1307. All minor potential buffer overwrite bugs, or coverity errors. Fixed by replacing strcpy and sprintf with strncpy and snprintf. r20523: fixed coverity issues 68, 1120, 1116i r20524: Check H5Z_SZIP->encoder_present < 1 assuming 0 represents absence. r20601: Purpose: Fix coverity issues 1703-1705 Description: Modified the cleanup code in test_free in accum.c to reset allocated buffers to NULL after they are freed, and modified the error cleanup code to check if these buffers are NULL before freeing them. Also fixed some unrelated warnings in accum.c. r20602: Use HDsnprintf and HDstrncat r20603: Purpose: Fix coverity issues 808-809 Description: Modified test_core in vfd.c to check the returns from malloc, and keep track of whether points and check are allocated by setting them to NULL when they are not. Added code to free points and check on error if they are not NULL. Also fixed unrelated warnings in vfd.c. r20604: Use HDstrncpy. r20605: Use HDstrncpy and HDstrncat. r20606: Purpose: Fix coverity issue 807 Description: Modified long_compact in stab.c to keep track of whether objname is allocated by setting it to NULL when it is not. Added code to free objname on error if it is not NULL. r20607: Changed string function calls to use versions that specify the string length to fix coverity issues 832 and 839. Tested on: Mac OSX/64 10.8.2 (amazon) (Too minor to require h5committest)
Diffstat (limited to 'src/H5system.c')
-rw-r--r--src/H5system.c526
1 files changed, 267 insertions, 259 deletions
diff --git a/src/H5system.c b/src/H5system.c
index c0baee1..83cecba 100644
--- a/src/H5system.c
+++ b/src/H5system.c
@@ -122,24 +122,24 @@ HDfprintf(FILE *stream, const char *fmt, ...)
va_start (ap, fmt);
while (*fmt) {
- fwidth = prec = 0;
- zerofill = 0;
- leftjust = 0;
- plussign = 0;
- prefix = 0;
- ldspace = 0;
- modifier[0] = '\0';
-
- if ('%'==fmt[0] && '%'==fmt[1]) {
- HDputc ('%', stream);
- fmt += 2;
- nout++;
- } else if ('%'==fmt[0]) {
- s = fmt + 1;
-
- /* Flags */
- while(HDstrchr ("-+ #", *s)) {
- switch(*s) {
+ fwidth = prec = 0;
+ zerofill = 0;
+ leftjust = 0;
+ plussign = 0;
+ prefix = 0;
+ ldspace = 0;
+ modifier[0] = '\0';
+
+ if ('%'==fmt[0] && '%'==fmt[1]) {
+ HDputc ('%', stream);
+ fmt += 2;
+ nout++;
+ } else if ('%'==fmt[0]) {
+ s = fmt + 1;
+
+ /* Flags */
+ while(HDstrchr ("-+ #", *s)) {
+ switch(*s) {
case '-':
leftjust = 1;
break;
@@ -155,244 +155,251 @@ HDfprintf(FILE *stream, const char *fmt, ...)
case '#':
prefix = 1;
break;
- } /* end switch */ /*lint !e744 Switch statement doesn't _need_ default */
- s++;
- } /* end while */
-
- /* Field width */
- if (HDisdigit (*s)) {
- zerofill = ('0'==*s);
- fwidth = (int)HDstrtol (s, &rest, 10);
- s = rest;
- } else if ('*'==*s) {
- fwidth = va_arg (ap, int);
- if (fwidth<0) {
- leftjust = 1;
- fwidth = -fwidth;
- }
- s++;
- }
+ } /* end switch */ /*lint !e744 Switch statement doesn't _need_ default */
+ s++;
+ } /* end while */
+
+ /* Field width */
+ if(HDisdigit(*s)) {
+ zerofill = ('0' == *s);
+ fwidth = (int)HDstrtol (s, &rest, 10);
+ s = rest;
+ } /* end if */
+ else if ('*'==*s) {
+ fwidth = va_arg (ap, int);
+ if(fwidth < 0) {
+ leftjust = 1;
+ fwidth = -fwidth;
+ }
+ s++;
+ }
+
+ /* Precision */
+ if('.'==*s) {
+ s++;
+ if(HDisdigit(*s)) {
+ prec = (int)HDstrtol(s, &rest, 10);
+ s = rest;
+ } else if('*'==*s) {
+ prec = va_arg(ap, int);
+ s++;
+ }
+ if(prec < 1)
+ prec = 1;
+ }
+
+ /* Extra type modifiers */
+ if(HDstrchr("ZHhlqLI", *s)) {
+ switch(*s) {
+ /*lint --e{506} Don't issue warnings about constant value booleans */
+ /*lint --e{774} Don't issue warnings boolean within 'if' always evaluates false/true */
+ case 'H':
+ if(sizeof(hsize_t) < sizeof(long))
+ modifier[0] = '\0';
+ else if(sizeof(hsize_t) == sizeof(long))
+ HDstrncpy(modifier, "l", 2);
+ else
+ HDstrncpy(modifier, H5_PRINTF_LL_WIDTH, HDstrlen(H5_PRINTF_LL_WIDTH) + 1);
+ break;
- /* Precision */
- if ('.'==*s) {
- s++;
- if (HDisdigit (*s)) {
- prec = (int)HDstrtol (s, &rest, 10);
- s = rest;
- } else if ('*'==*s) {
- prec = va_arg (ap, int);
- s++;
- }
- if (prec<1) prec = 1;
- }
+ case 'Z':
+ if(sizeof(size_t) < sizeof(long))
+ modifier[0] = '\0';
+ else if(sizeof(size_t) == sizeof(long))
+ HDstrncpy(modifier, "l", 2);
+ else
+ HDstrncpy(modifier, H5_PRINTF_LL_WIDTH, HDstrlen(H5_PRINTF_LL_WIDTH) + 1);
+ break;
- /* Extra type modifiers */
- if (HDstrchr ("ZHhlqLI", *s)) {
- switch (*s) {
- /*lint --e{506} Don't issue warnings about constant value booleans */
- /*lint --e{774} Don't issue warnings boolean within 'if' always evaluates false/true */
- case 'H':
- if (sizeof(hsize_t)<sizeof(long)) {
- modifier[0] = '\0';
- } else if (sizeof(hsize_t)==sizeof(long)) {
- HDstrcpy (modifier, "l");
- } else {
- HDstrcpy (modifier, H5_PRINTF_LL_WIDTH);
- }
- break;
- case 'Z':
- if (sizeof(size_t)<sizeof(long)) {
- modifier[0] = '\0';
- } else if (sizeof(size_t)==sizeof(long)) {
- HDstrcpy (modifier, "l");
- } else {
- HDstrcpy (modifier, H5_PRINTF_LL_WIDTH);
- }
- break;
- default:
- /* Handle 'I64' modifier for Microsoft's "__int64" type */
- if(*s=='I' && *(s+1)=='6' && *(s+2)=='4') {
- modifier[0] = *s;
- modifier[1] = *(s+1);
- modifier[2] = *(s+2);
- modifier[3] = '\0';
- s+=2; /* Increment over 'I6', the '4' is taken care of below */
- } /* end if */
- else {
- /* Handle 'll' for long long types */
- if(*s=='l' && *(s+1)=='l') {
+ default:
+ /* Handle 'I64' modifier for Microsoft's "__int64" type */
+ if(*s=='I' && *(s+1)=='6' && *(s+2)=='4') {
modifier[0] = *s;
- modifier[1] = *s;
- modifier[2] = '\0';
- s++; /* Increment over first 'l', second is taken care of below */
+ modifier[1] = *(s+1);
+ modifier[2] = *(s+2);
+ modifier[3] = '\0';
+ s += 2; /* Increment over 'I6', the '4' is taken care of below */
} /* end if */
else {
- modifier[0] = *s;
- modifier[1] = '\0';
+ /* Handle 'll' for long long types */
+ if(*s=='l' && *(s+1)=='l') {
+ modifier[0] = *s;
+ modifier[1] = *s;
+ modifier[2] = '\0';
+ s++; /* Increment over first 'l', second is taken care of below */
+ } /* end if */
+ else {
+ modifier[0] = *s;
+ modifier[1] = '\0';
+ } /* end else */
} /* end else */
- } /* end else */
- break;
- }
- s++;
- }
-
- /* Conversion */
- conv = *s++;
-
- /* Create the format template */
- sprintf (format_templ, "%%%s%s%s%s%s",
- leftjust?"-":"", plussign?"+":"",
- ldspace?" ":"", prefix?"#":"", zerofill?"0":"");
- if (fwidth>0)
- sprintf (format_templ+HDstrlen(format_templ), "%d", fwidth);
- if (prec>0)
- sprintf (format_templ+HDstrlen(format_templ), ".%d", prec);
- if (*modifier)
- sprintf (format_templ+HDstrlen(format_templ), "%s", modifier);
- sprintf (format_templ+HDstrlen(format_templ), "%c", conv);
-
-
- /* Conversion */
- switch (conv) {
- case 'd':
- case 'i':
- if (!HDstrcmp(modifier, "h")) {
- short x = (short)va_arg (ap, int);
- n = fprintf (stream, format_templ, x);
- } else if (!*modifier) {
- int x = va_arg (ap, int);
- n = fprintf (stream, format_templ, x);
- } else if (!HDstrcmp (modifier, "l")) {
- long x = va_arg (ap, long);
- n = fprintf (stream, format_templ, x);
- } else {
- int64_t x = va_arg(ap, int64_t);
- n = fprintf (stream, format_templ, x);
- }
- break;
-
- case 'o':
- case 'u':
- case 'x':
- case 'X':
- if (!HDstrcmp (modifier, "h")) {
- unsigned short x = (unsigned short)va_arg (ap, unsigned int);
- n = fprintf (stream, format_templ, x);
- } else if (!*modifier) {
- unsigned int x = va_arg (ap, unsigned int); /*lint !e732 Loss of sign not really occuring */
- n = fprintf (stream, format_templ, x);
- } else if (!HDstrcmp (modifier, "l")) {
- unsigned long x = va_arg (ap, unsigned long); /*lint !e732 Loss of sign not really occuring */
- n = fprintf (stream, format_templ, x);
- } else {
- uint64_t x = va_arg(ap, uint64_t); /*lint !e732 Loss of sign not really occuring */
- n = fprintf (stream, format_templ, x);
- }
- break;
-
- case 'f':
- case 'e':
- case 'E':
- case 'g':
- case 'G':
- if (!HDstrcmp (modifier, "h")) {
- float x = (float) va_arg (ap, double);
- n = fprintf (stream, format_templ, x);
- } else if (!*modifier || !HDstrcmp (modifier, "l")) {
- double x = va_arg (ap, double);
- n = fprintf (stream, format_templ, x);
- } else {
- /*
- * Some compilers complain when `long double' and
- * `double' are the same thing.
- */
+ break;
+ }
+ s++;
+ }
+
+ /* Conversion */
+ conv = *s++;
+
+ /* Create the format template */
+ sprintf(format_templ, "%%%s%s%s%s%s", (leftjust ? "-" : ""),
+ (plussign ? "+" : ""), (ldspace ? " " : ""),
+ (prefix ? "#" : ""), (zerofill ? "0" : ""));
+ if(fwidth > 0)
+ sprintf(format_templ+HDstrlen(format_templ), "%d", fwidth);
+ if(prec > 0)
+ sprintf(format_templ+HDstrlen(format_templ), ".%d", prec);
+ if(*modifier)
+ sprintf(format_templ+HDstrlen(format_templ), "%s", modifier);
+ sprintf (format_templ+HDstrlen(format_templ), "%c", conv);
+
+
+ /* Conversion */
+ switch (conv) {
+ case 'd':
+ case 'i':
+ if(!HDstrcmp(modifier, "h")) {
+ short x = (short)va_arg (ap, int);
+ n = fprintf (stream, format_templ, x);
+ } else if(!*modifier) {
+ int x = va_arg (ap, int);
+ n = fprintf (stream, format_templ, x);
+ } else if(!HDstrcmp (modifier, "l")) {
+ long x = va_arg (ap, long);
+ n = fprintf (stream, format_templ, x);
+ } else {
+ int64_t x = va_arg(ap, int64_t);
+ n = fprintf (stream, format_templ, x);
+ }
+ break;
+
+ case 'o':
+ case 'u':
+ case 'x':
+ case 'X':
+ if(!HDstrcmp(modifier, "h")) {
+ unsigned short x = (unsigned short)va_arg (ap, unsigned int);
+ n = fprintf(stream, format_templ, x);
+ } else if(!*modifier) {
+ unsigned int x = va_arg (ap, unsigned int); /*lint !e732 Loss of sign not really occuring */
+ n = fprintf(stream, format_templ, x);
+ } else if(!HDstrcmp(modifier, "l")) {
+ unsigned long x = va_arg (ap, unsigned long); /*lint !e732 Loss of sign not really occuring */
+ n = fprintf(stream, format_templ, x);
+ } else {
+ uint64_t x = va_arg(ap, uint64_t); /*lint !e732 Loss of sign not really occuring */
+ n = fprintf(stream, format_templ, x);
+ }
+ break;
+
+ case 'f':
+ case 'e':
+ case 'E':
+ case 'g':
+ case 'G':
+ if(!HDstrcmp(modifier, "h")) {
+ float x = (float)va_arg(ap, double);
+ n = fprintf(stream, format_templ, x);
+ } else if(!*modifier || !HDstrcmp(modifier, "l")) {
+ double x = va_arg(ap, double);
+ n = fprintf(stream, format_templ, x);
+ } else {
+ /*
+ * Some compilers complain when `long double' and
+ * `double' are the same thing.
+ */
#if H5_SIZEOF_LONG_DOUBLE != H5_SIZEOF_DOUBLE
- long double x = va_arg (ap, long double);
- n = fprintf (stream, format_templ, x);
+ long double x = va_arg(ap, long double);
+ n = fprintf(stream, format_templ, x);
#else
- double x = va_arg (ap, double);
- n = fprintf (stream, format_templ, x);
+ double x = va_arg(ap, double);
+ n = fprintf(stream, format_templ, x);
#endif
- }
- break;
-
- case 'a':
- {
- haddr_t x = va_arg (ap, haddr_t); /*lint !e732 Loss of sign not really occuring */
- if (H5F_addr_defined(x)) {
- sprintf(format_templ, "%%%s%s%s%s%s",
- leftjust?"-":"", plussign?"+":"",
- ldspace?" ":"", prefix?"#":"",
- zerofill?"0":"");
- if (fwidth>0)
- sprintf(format_templ+HDstrlen(format_templ), "%d", fwidth);
-
- /*lint --e{506} Don't issue warnings about constant value booleans */
- /*lint --e{774} Don't issue warnings boolean within 'if' always evaluates false/true */
- if (sizeof(x)==H5_SIZEOF_INT) {
- HDstrcat(format_templ, "u");
- } else if (sizeof(x)==H5_SIZEOF_LONG) {
- HDstrcat(format_templ, "lu");
- } else if (sizeof(x)==H5_SIZEOF_LONG_LONG) {
- HDstrcat(format_templ, H5_PRINTF_LL_WIDTH);
- HDstrcat(format_templ, "u");
- }
- n = fprintf(stream, format_templ, x);
+ }
+ break;
+
+ case 'a':
+ {
+ haddr_t x = va_arg (ap, haddr_t); /*lint !e732 Loss of sign not really occuring */
+
+ if(H5F_addr_defined(x)) {
+ sprintf(format_templ, "%%%s%s%s%s%s",
+ (leftjust ? "-" : ""), (plussign ? "+" : ""),
+ (ldspace ? " " : ""), (prefix ? "#" : ""),
+ (zerofill ? "0" : ""));
+ if(fwidth > 0)
+ sprintf(format_templ + HDstrlen(format_templ), "%d", fwidth);
+
+ /*lint --e{506} Don't issue warnings about constant value booleans */
+ /*lint --e{774} Don't issue warnings boolean within 'if' always evaluates false/true */
+ if(sizeof(x) == H5_SIZEOF_INT)
+ HDstrcat(format_templ, "u");
+ else if(sizeof(x) == H5_SIZEOF_LONG)
+ HDstrcat(format_templ, "lu");
+ else if(sizeof(x) == H5_SIZEOF_LONG_LONG) {
+ HDstrcat(format_templ, H5_PRINTF_LL_WIDTH);
+ HDstrcat(format_templ, "u");
+ }
+ n = fprintf(stream, format_templ, x);
+ } else {
+ HDstrcpy(format_templ, "%");
+ if(leftjust)
+ HDstrcat(format_templ, "-");
+ if(fwidth)
+ sprintf(format_templ+HDstrlen(format_templ), "%d", fwidth);
+ HDstrcat(format_templ, "s");
+ fprintf(stream, format_templ, "UNDEF");
+ }
+ }
+ break;
+
+ case 'c':
+ {
+ char x = (char)va_arg(ap, int);
+ n = fprintf(stream, format_templ, x);
+ }
+ break;
+
+ case 's':
+ case 'p':
+ {
+ char *x = va_arg(ap, char*); /*lint !e64 Type mismatch not really occuring */
+ n = fprintf(stream, format_templ, x);
+ }
+ break;
+
+ case 'n':
+ format_templ[HDstrlen(format_templ) - 1] = 'u';
+ n = fprintf(stream, format_templ, nout);
+ break;
+
+ case 't':
+ {
+ htri_t tri_var = va_arg(ap, htri_t);
+
+ if(tri_var > 0)
+ fprintf (stream, "TRUE");
+ else if(!tri_var)
+ fprintf(stream, "FALSE");
+ else
+ fprintf(stream, "FAIL(%d)", (int)tri_var);
+ }
+ break;
+
+ default:
+ HDfputs(format_templ, stream);
+ n = (int)HDstrlen(format_templ);
+ break;
+ }
+ nout += n;
+ fmt = s;
} else {
- HDstrcpy(format_templ, "%");
- if (leftjust)
- HDstrcat(format_templ, "-");
- if (fwidth)
- sprintf(format_templ+HDstrlen(format_templ), "%d", fwidth);
- HDstrcat(format_templ, "s");
- fprintf(stream, format_templ, "UNDEF");
+ HDputc(*fmt, stream);
+ fmt++;
+ nout++;
}
}
- break;
-
- case 'c':
- {
- char x = (char)va_arg (ap, int);
- n = fprintf (stream, format_templ, x);
- }
- break;
-
- case 's':
- case 'p':
- {
- char *x = va_arg (ap, char*); /*lint !e64 Type mismatch not really occuring */
- n = fprintf (stream, format_templ, x);
- }
- break;
-
- case 'n':
- format_templ[HDstrlen(format_templ)-1] = 'u';
- n = fprintf (stream, format_templ, nout);
- break;
-
- case 't':
- {
- htri_t tri_var = va_arg (ap, htri_t);
- if (tri_var > 0) fprintf (stream, "TRUE");
- else if (!tri_var) fprintf (stream, "FALSE");
- else fprintf (stream, "FAIL(%d)", (int)tri_var);
- }
- break;
-
- default:
- HDfputs (format_templ, stream);
- n = (int)HDstrlen (format_templ);
- break;
- }
- nout += n;
- fmt = s;
- } else {
- HDputc (*fmt, stream);
- fmt++;
- nout++;
- }
- }
- va_end (ap);
+ va_end(ap);
return nout;
} /* end HDfprintf() */
@@ -713,23 +720,23 @@ H5_build_extpath(const char *name, char **extpath/*out*/)
if(NULL == (new_name = (char *)H5MM_strdup(name)))
HGOTO_ERROR(H5E_INTERNAL, H5E_NOSPACE, FAIL, "memory allocation failed")
- /*
- * Windows: name[0-1] is "<drive-letter>:"
- * Get current working directory on the drive specified in NAME
- * Unix: does not apply
+ /*
+ * Windows: name[0-1] is "<drive-letter>:"
+ * Get current working directory on the drive specified in NAME
+ * Unix: does not apply
* OpenVMS: does not apply
- */
+ */
if(CHECK_ABS_DRIVE(name)) {
drive = name[0] - 'A' + 1;
retcwd = HDgetdcwd(drive, cwdpath, MAX_PATH_LEN);
HDstrcpy(new_name, &name[2]);
} /* end if */
- /*
- * Windows: name[0] is a '/' or '\'
- * Get current drive
- * Unix: does not apply
- * OpenVMS: does not apply
- */
+ /*
+ * Windows: name[0] is a '/' or '\'
+ * Get current drive
+ * Unix: does not apply
+ * OpenVMS: does not apply
+ */
else if(CHECK_ABS_PATH(name) && (0 != (drive = HDgetdrive()))) {
sprintf(cwdpath, "%c:%c", (drive+'A'-1), name[0]);
retcwd = cwdpath;
@@ -749,7 +756,7 @@ H5_build_extpath(const char *name, char **extpath/*out*/)
if(NULL == (full_path = (char *)H5MM_malloc(path_len)))
HGOTO_ERROR(H5E_INTERNAL, H5E_NOSPACE, FAIL, "memory allocation failed")
- HDstrcpy(full_path, cwdpath);
+ HDstrncpy(full_path, cwdpath, cwdlen + 1);
#ifdef H5_VMS
/* If the file name contains relative path, cut off the beginning bracket. Also cut off the
* ending bracket of CWDPATH to combine the full path name. i.g.
@@ -759,15 +766,16 @@ H5_build_extpath(const char *name, char **extpath/*out*/)
*/
if(new_name[0] == '[') {
char *tmp = new_name;
+
full_path[cwdlen - 1] = '\0';
HDstrcat(full_path, ++tmp);
} /* end if */
else
- HDstrcat(full_path, new_name);
+ HDstrncat(full_path, new_name, HDstrlen(new_name));
#else
if(!CHECK_DELIMITER(cwdpath[cwdlen - 1]))
- HDstrcat(full_path, DIR_SEPS);
- HDstrcat(full_path, new_name);
+ HDstrncat(full_path, DIR_SEPS, HDstrlen(DIR_SEPS));
+ HDstrncat(full_path, new_name, HDstrlen(new_name));
#endif
} /* end if */
} /* end else */