[PATCH #46] Fixed uninitialized buffer in case of 0 dim for dimstring()

and prevented the compilation of unused functions. Also a comment was
 corrected.
This commit is contained in:
Jim Monte 2019-05-01 16:56:22 +02:00 committed by Holger Vogt
parent 282b53d51e
commit f6c720c288
2 changed files with 56 additions and 37 deletions

View File

@ -15,27 +15,36 @@ Author: 1992 David A. Gates, U. C. Berkeley CAD Group
/*
* Create a string of the form "12,1,10".
*
* Parameters
* dim_data: Array of sizes of dimensions, [12, 1, 10] for the example
* n_dim: Number of elements in the array, 3 in the example
* retstring: Address of buffer where the string is returned.
* Remarks
* It is assumed that the buffer in retstring is large enough, which for
* MAXDIMS, would require MAXDIMS * 11 bytes in the worst case assuming
* 32-bit ints. A looser but more general bound only assuming 8-bit bytes
* would be MAXDIMS * (3 * sizeof(int) + 1).
*/
void
dimstring(int *data, int length, char *retstring)
dimstring(const int *dim_data, int n_dim, char *retstring)
{
/* Handle case of no dimensions */
if (dim_data == (int *) NULL || n_dim < 1) {
*retstring = '\0';
return;
}
/* Append each dimension */
retstring += sprintf(retstring, "%d", dim_data[0]); /* first */
int i;
char buf[BSIZE_SP];
for (i = 1; i < n_dim; i++) { /* rest are prefixed by a comma */
retstring += sprintf(retstring, ",%d", dim_data[i]);
}
} /* end of function dimstring */
if (!data || length < 1)
retstring = "";
buf[0] = '\0';
for (i = 0; i < length; i++)
sprintf(buf + strlen(buf), "%d%s", data[i], (i < length - 1) ? "," : "");
/* XXX Should I return a copy instead? */
/* qui ci devo fare una copia */
strcpy(retstring, buf);
}
/*
* Create a string of the form "[12][1][10]" in retstring.
@ -49,7 +58,7 @@ dimstring(int *data, int length, char *retstring)
* It is assumed that the buffer in retstring is large enough, which for
* MAXDIMS, would require MAXDIMS * 12 + 1 bytes in the worst case assuming
* 32-bit ints. A looser but more general bound only assuming 8-bit bytes
* would be MAXDIMS * 3 * sizeof(int) + 1.
* would be MAXDIMS * (3 * sizeof(int) + 2) + 1.
*/
void
indexstring(const int *dim_data, int n_dim, char *retstring)
@ -74,7 +83,7 @@ indexstring(const int *dim_data, int n_dim, char *retstring)
* Return 1 when all counters overflow at once.
*/
int
incindex(int *counts, int numcounts, int *dims, int numdims)
incindex(int *counts, int numcounts, const int *dims, int numdims)
{
int i, start;
@ -96,22 +105,6 @@ incindex(int *counts, int numcounts, int *dims, int numdims)
}
/*
* Count number of empty dimensions in an array.
*/
int
emptydims(int *data, int length)
{
int i, numempty = 0;
for (i = 0; i < length; i++)
if (data[i] == 0)
numempty++;
return (numempty);
}
/*
* Read a string of one of the following forms into a dimensions array:
* [12][1][10]
@ -217,6 +210,26 @@ atodims(char *p, int *data, int *outlength)
}
#ifdef COMPILE_UNUSED_FUNCTIONS
/* #ifdef COMPILE_UNUSED_FUNCTIONS added 2019-03-31 */
/*
* Count number of empty dimensions in an array.
*/
int
emptydims(int *data, int length)
{
int i, numempty = 0;
for (i = 0; i < length; i++)
if (data[i] == 0)
numempty++;
return (numempty);
}
/*
* Skip to the first character that cannot be part of a dimension string.
*/
@ -226,8 +239,10 @@ skipdims(char *p)
if (!p)
return NULL;
while (*p && (*p == '[' || *p == ']' || *p == ',' || isspace_c(*p) || isdigit_c(*p)))
while (*p && (*p == '[' || *p == ']' || *p == ',' ||
isspace_c(*p) || isdigit_c(*p)))
p++;
return (p);
}
#endif /* COMPILE_UNUSED_FUNCTIONS */

View File

@ -6,11 +6,15 @@
#ifndef ngspice_DIMENS_H
#define ngspice_DIMENS_H
void dimstring(int *data, int length, char *retstring);
void dimstring(const int *dim_data, int n_dim, char *retstring);
void indexstring(const int *dim_data, int n_dim, char *retstring);
int incindex(int *counts, int numcounts, int *dims, int numdims);
int emptydims(int *data, int length);
int incindex(int *counts, int numcounts, const int *dims, int numdims);
int atodims(char *p, int *data, int *outlength);
#ifdef COMPILE_UNUSED_FUNCTIONS
/* #ifdef COMPILE_UNUSED_FUNCTIONS added 2019-03-31 */
int emptydims(int *data, int length);
char *skipdims(char *p);
#endif
#endif