sprintf.patch

_ wanabe, 10/26/2008 10:35 AM

Download (4.24 KB)

View differences:

include/ruby/win32.h (working copy)
248 248
extern int rb_w32_vsnprintf(char *, size_t, const char *, va_list);
249 249
extern int rb_w32_snprintf(char *, size_t, const char *, ...);
250 250

  
251
#if (defined(_MSC_VER) && defined(_DLL)) || defined(__MSVCRT__)
252
#undef HAVE_VSNPRINTF
253
#define _vsnprintf BSD_vsnprintf
254
extern int BSD_vsnprintf(char *, size_t, const char *, va_list);
255
#endif
256

  
251 257
extern int chown(const char *, int, int);
252 258
extern int link(const char *, const char *);
253 259
extern int gettimeofday(struct timeval *, struct timezone *);
numeric.c (working copy)
530 530
    else if(isnan(value))
531 531
	return rb_usascii_str_new2("NaN");
532 532

  
533
    sprintf(buf, "%#.15g", value); /* ensure to print decimal point */
533
    snprintf(buf, 32, "%#.15g", value); /* ensure to print decimal point */
534 534
    if (!(e = strchr(buf, 'e'))) {
535 535
	e = buf + strlen(buf);
536 536
    }
537 537
    if (!ISDIGIT(e[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
538
	sprintf(buf, "%#.14e", value);
538
	snprintf(buf, 32, "%#.14e", value);
539 539
	if (!(e = strchr(buf, 'e'))) {
540 540
	    e = buf + strlen(buf);
541 541
	}
......
1548 1548
	    char buf[24];
1549 1549
	    char *s;
1550 1550

  
1551
	    sprintf(buf, "%-.10g", RFLOAT_VALUE(val));
1551
	    snprintf(buf, 24, "%-.10g", RFLOAT_VALUE(val));
1552 1552
	    if ((s = strchr(buf, ' ')) != 0) *s = '\0';
1553 1553
	    rb_raise(rb_eRangeError, "float %s out of range of integer", buf);
1554 1554
	}
......
1694 1694
	    char buf[24];
1695 1695
	    char *s;
1696 1696

  
1697
	    sprintf(buf, "%-.10g", RFLOAT_VALUE(val));
1697
	    snprintf(buf, 24, "%-.10g", RFLOAT_VALUE(val));
1698 1698
	    if ((s = strchr(buf, ' ')) != 0) *s = '\0';
1699 1699
	    rb_raise(rb_eRangeError, "float %s out of range of long long", buf);
1700 1700
	}
sprintf.c (working copy)
1018 1018
		need += 20;
1019 1019

  
1020 1020
		CHECK(need);
1021
		sprintf(&buf[blen], fbuf, fval);
1021
		snprintf(&buf[blen], need, fbuf, fval);
1022 1022
		blen += strlen(&buf[blen]);
1023 1023
	    }
1024 1024
	    break;
missing/vsnprintf.c (working copy)
781 781
			flags |= FPT;
782 782
			cp = cvt(_double, prec, flags, &softsign,
783 783
				&expt, ch, &ndig);
784
			if ((ch == 'e' || ch == 'E') && ndig < prec)
785
				ndig = prec;
784 786
			if (ch == 'g' || ch == 'G') {
785
				if (expt <= -4 || expt > prec)
787
				if (expt <= -4 || (expt > prec && expt > 1))
786 788
					ch = (ch == 'g') ? 'e' : 'E';
787 789
				else
788 790
					ch = 'g';
......
798 800
					size = expt;
799 801
					if (prec || flags & ALT)
800 802
						size += prec + 1;
801
				} else	/* "0.X" */
803
				} else if (!prec) /* "0" */
804
				  size = 1;
805
				else	/* "0.X" */
802 806
					size = prec + 2;
803 807
			} else if (expt >= ndig) {	/* fixed g fmt */
804 808
				size = expt;
......
1008 1012
			if (ch >= 'f') {	/* 'f' or 'g' */
1009 1013
				if (_double == 0) {
1010 1014
				/* kludge for __dtoa irregularity */
1011
					if (prec == 0 ||
1015
					if (ndig <= 1 &&
1012 1016
					    (flags & ALT) == 0) {
1013 1017
						PRINT("0", 1);
1014 1018
					} else {
1015 1019
						PRINT("0.", 2);
1016 1020
						PAD(ndig - 1, zeroes);
1017 1021
					}
1022
				} else if (expt == 0 && ndig == 0 && (flags & ALT) == 0) {
1023
					PRINT("0", 1);
1018 1024
				} else if (expt <= 0) {
1019 1025
					PRINT("0.", 2);
1020 1026
					PAD(-expt, zeroes);
......
1036 1042
					ox[1] = '.';
1037 1043
					PRINT(ox, 2);
1038 1044
					if (_double || flags & ALT == 0) {
1039
						PRINT(cp, ndig-1);
1045
						int i;
1046
						for (i = 0; i < ndig - 1; i++) {
1047
							if (cp[i] == '\0')
1048
								break;
1049
						}
1050
						PRINT(cp, i);
1051
						if (i < ndig - 1)
1052
							PAD(ndig - 1 - i, zeroes);
1040 1053
					} else	/* 0.[0..] */
1041 1054
						/* __dtoa irregularity */
1042 1055
						PAD(ndig - 1, zeroes);
......
1141 1154

  
1142 1155
#ifndef HAVE_VSNPRINTF
1143 1156
int
1157
#if (defined(_MSC_VER) && defined(_DLL)) || defined(__MSVCRT__)
1158
BSD_vsnprintf(str, n, fmt, ap)
1159
#else
1144 1160
vsnprintf(str, n, fmt, ap)
1161
#endif
1145 1162
	char *str;
1146 1163
	size_t n;
1147 1164
	const char *fmt;