[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:
parent
282b53d51e
commit
f6c720c288
|
|
@ -15,27 +15,36 @@ Author: 1992 David A. Gates, U. C. Berkeley CAD Group
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create a string of the form "12,1,10".
|
* 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
|
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;
|
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.
|
* 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
|
* 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
|
* 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
|
* 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
|
void
|
||||||
indexstring(const int *dim_data, int n_dim, char *retstring)
|
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.
|
* Return 1 when all counters overflow at once.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
incindex(int *counts, int numcounts, int *dims, int numdims)
|
incindex(int *counts, int numcounts, const int *dims, int numdims)
|
||||||
{
|
{
|
||||||
int i, start;
|
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:
|
* Read a string of one of the following forms into a dimensions array:
|
||||||
* [12][1][10]
|
* [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.
|
* Skip to the first character that cannot be part of a dimension string.
|
||||||
*/
|
*/
|
||||||
|
|
@ -226,8 +239,10 @@ skipdims(char *p)
|
||||||
if (!p)
|
if (!p)
|
||||||
return NULL;
|
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++;
|
p++;
|
||||||
|
|
||||||
return (p);
|
return (p);
|
||||||
}
|
}
|
||||||
|
#endif /* COMPILE_UNUSED_FUNCTIONS */
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,15 @@
|
||||||
#ifndef ngspice_DIMENS_H
|
#ifndef ngspice_DIMENS_H
|
||||||
#define 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);
|
void indexstring(const int *dim_data, int n_dim, char *retstring);
|
||||||
int incindex(int *counts, int numcounts, int *dims, int numdims);
|
int incindex(int *counts, int numcounts, const int *dims, int numdims);
|
||||||
int emptydims(int *data, int length);
|
|
||||||
int atodims(char *p, int *data, int *outlength);
|
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);
|
char *skipdims(char *p);
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue