[kaffe] CVS kaffe (robilad): Use snprintf for Double and Float toString
Kaffe CVS
cvs-commits at kaffe.org
Fri Oct 22 12:36:35 PDT 2004
PatchSet 5324
Date: 2004/10/22 19:32:25
Author: robilad
Branch: HEAD
Tag: (none)
Log:
Use snprintf for Double and Float toString
2004-10-22 Eric Anholt <eta at lclark.edu>
* libraries/clib/native/Double.c,
libraries/clib/native/Float.c:
(toCharArrayWithPrecision): Added len parameter.
Use snprintf instead of sprintf. Take care not to
erase last decimal digit if necessary.
(java_lang_Double_toStringWithPrecision,
java_lang_Float_toStringWithPrecision) Make sure
that max_precision is always smaller than MAXNUMLEN.
Pass buffer length to toCharArrayWithPrecision.
Members:
ChangeLog:1.2876->1.2877
libraries/clib/native/Double.c:1.23->1.24
libraries/clib/native/Float.c:1.14->1.15
Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.2876 kaffe/ChangeLog:1.2877
--- kaffe/ChangeLog:1.2876 Fri Oct 22 15:38:49 2004
+++ kaffe/ChangeLog Fri Oct 22 19:32:25 2004
@@ -1,3 +1,15 @@
+2004-10-22 Eric Anholt <eta at lclark.edu>
+
+ * libraries/clib/native/Double.c,
+ libraries/clib/native/Float.c:
+ (toCharArrayWithPrecision): Added len parameter.
+ Use snprintf instead of sprintf. Take care not to
+ erase last decimal digit if necessary.
+ (java_lang_Double_toStringWithPrecision,
+ java_lang_Float_toStringWithPrecision) Make sure
+ that max_precision is always smaller than MAXNUMLEN.
+ Pass buffer length to toCharArrayWithPrecision.
+
2004-10-22 Ito Kazumitsu <kaz at maczuka.gcd.org>
* libraries/javalib/kaffe/io/CharToByteConverter.java,
Index: kaffe/libraries/clib/native/Double.c
diff -u kaffe/libraries/clib/native/Double.c:1.23 kaffe/libraries/clib/native/Double.c:1.24
--- kaffe/libraries/clib/native/Double.c:1.23 Mon Oct 11 21:36:51 2004
+++ kaffe/libraries/clib/native/Double.c Fri Oct 22 19:32:27 2004
@@ -35,7 +35,7 @@
* in Float.c as well.
*/
static char *
-toCharArrayWithPrecision(char * buf, jdouble val, jint precision) {
+toCharArrayWithPrecision(char * buf, int len, jdouble val, jint precision) {
const jlong bits = java_lang_Double_doubleToRawLongBits(val);
char *s;
int k;
@@ -45,16 +45,17 @@
if (bits & DSIGNBIT) {
val = -val;
*s++ = '-';
+ len--;
}
/* Print in normal or 'scientific' form according to value */
if (val == 0.0 || (val >= 1.0e-3 && val < 1.0e7)) {
/* Print in decimal notation */
- sprintf(s, "%.*f", (int) precision, (double) val);
+ snprintf(s, len, "%.*f", (int) precision, (double) val);
/* Remove trailing zeroes after the decimal point */
for (k = strlen(buf) - 1;
- buf[k] == '0' && buf[k - 1] != '.';
+ k > 1 && buf[k] == '0' && buf[k - 1] != '.';
k--) {
buf[k] = '\0';
}
@@ -62,7 +63,7 @@
char *t, *eptr, *eval;
/* Print in exponential notation */
- sprintf(s, "%.*E", (int) precision - 1, (double) val);
+ snprintf(s, len, "%.*E", (int) precision - 1, (double) val);
/* Find the exponent */
eptr = strchr(buf, 'E');
@@ -188,10 +189,14 @@
jint min_precision = 1;
jint precision = 0;
+ /* FreeBSD's snprintf dies on very large precision. */
+ if (max_precision > MAXNUMLEN)
+ max_precision = MAXNUMLEN;
+
/* perform a binary search over precision. */
while (max_precision != min_precision + 1) {
precision = (max_precision + min_precision) / 2;
- toCharArrayWithPrecision(buf, val, (int) precision);
+ toCharArrayWithPrecision(buf, MAXNUMLEN, val, (int) precision);
if (valueOfCharArray(buf) != val) {
min_precision = precision;
@@ -207,7 +212,7 @@
* is not equal to val.
*/
if (precision == min_precision) {
- toCharArrayWithPrecision(buf, val, (int) precision + 1);
+ toCharArrayWithPrecision(buf, MAXNUMLEN, val, (int) precision + 1);
}
return (checkPtr(stringC2Java(buf)));
Index: kaffe/libraries/clib/native/Float.c
diff -u kaffe/libraries/clib/native/Float.c:1.14 kaffe/libraries/clib/native/Float.c:1.15
--- kaffe/libraries/clib/native/Float.c:1.14 Fri May 21 15:19:24 2004
+++ kaffe/libraries/clib/native/Float.c Fri Oct 22 19:32:27 2004
@@ -50,7 +50,7 @@
* in Double.c as well.
*/
static char *
-toCharArrayWithPrecision(char * buf, jfloat val, jint precision) {
+toCharArrayWithPrecision(char * buf, int len, jfloat val, jint precision) {
const jint bits = java_lang_Float_floatToRawIntBits(val);
char *s;
int k;
@@ -60,16 +60,17 @@
if (bits & FSIGNBIT) {
val = -val;
*s++ = '-';
+ len--;
}
/* Print in normal or 'scientific' form according to value */
if (val == 0.0 || (val >= 1.0e-3 && val < 1.0e7)) {
/* Print in decimal notation */
- sprintf(s, "%.*f", (int) precision, (double) val);
+ snprintf(s, len, "%.*f", (int) precision, (double) val);
/* Remove trailing zeroes after the decimal point */
for (k = strlen(buf) - 1;
- buf[k] == '0' && buf[k - 1] != '.';
+ k > 1 && buf[k] == '0' && buf[k - 1] != '.';
k--) {
buf[k] = '\0';
}
@@ -77,7 +78,7 @@
char *t, *eptr, *eval;
/* Print in exponential notation */
- sprintf(s, "%.*E", (int) precision - 1, (double) val);
+ snprintf(s, len, "%.*E", (int) precision - 1, (double) val);
/* Find the exponent */
eptr = strchr(buf, 'E');
@@ -201,10 +202,14 @@
jint min_precision = 1;
jint precision = 0;
+ /* FreeBSD's snprintf dies on very large precision. */
+ if (max_precision > MAXNUMLEN)
+ max_precision = MAXNUMLEN;
+
/* perform a binary search over precision. */
while (max_precision != min_precision + 1) {
precision = (max_precision + min_precision) / 2;
- toCharArrayWithPrecision(buf, val, (int) precision);
+ toCharArrayWithPrecision(buf, MAXNUMLEN, val, (int) precision);
if (valueOfCharArray(buf) != val) {
min_precision = precision;
@@ -220,7 +225,7 @@
* is not equal to val.
*/
if (precision == min_precision) {
- toCharArrayWithPrecision(buf, val, (int) precision + 1);
+ toCharArrayWithPrecision(buf, MAXNUMLEN, val, (int) precision + 1);
}
return (checkPtr(stringC2Java(buf)));
More information about the kaffe
mailing list