View | Details | Raw Unified | Return to bug 164674 | Differences between
and this patch

Collapse All | Expand All

(-)new/lib/libc/stdio/vfprintf.c (+10 lines)
Lines 265-278 Link Here
265
265
266
{
266
{
267
	int ret;
267
	int ret;
268
	short orig_err;
268
269
269
	FLOCKFILE(fp);
270
	FLOCKFILE(fp);
271
	/*
272
	 * store flags, unset errors, success of printf is
273
	 * independent of previous errors on the fd
274
	 */
275
	orig_err = fp->_flags & __SERR;
276
	fp->_flags &= ~__SERR;
277
270
	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
278
	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
271
	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
279
	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
272
	    fp->_file >= 0)
280
	    fp->_file >= 0)
273
		ret = __sbprintf(fp, fmt0, ap);
281
		ret = __sbprintf(fp, fmt0, ap);
274
	else
282
	else
275
		ret = __vfprintf(fp, fmt0, ap);
283
		ret = __vfprintf(fp, fmt0, ap);
284
285
	fp->_flags |= orig_err & __SERR;
276
	FUNLOCKFILE(fp);
286
	FUNLOCKFILE(fp);
277
	return (ret);
287
	return (ret);
278
}
288
}
(-)new/lib/libc/stdio/vfwprintf.c (+10 lines)
Lines 347-360 Link Here
347
347
348
{
348
{
349
	int ret;
349
	int ret;
350
	short orig_err;
350
351
351
	FLOCKFILE(fp);
352
	FLOCKFILE(fp);
353
	/*
354
	 * store flags, unset errors, success of wprintf is
355
	 * independent of previous errors on the fd
356
	 */
357
	orig_err = fp->_flags & __SERR;
358
	fp->_flags &= ~__SERR;
359
352
	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
360
	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
353
	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
361
	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
354
	    fp->_file >= 0)
362
	    fp->_file >= 0)
355
		ret = __sbprintf(fp, fmt0, ap);
363
		ret = __sbprintf(fp, fmt0, ap);
356
	else
364
	else
357
		ret = __vfwprintf(fp, fmt0, ap);
365
		ret = __vfwprintf(fp, fmt0, ap);
366
367
	fp->_flags |= orig_err & __SERR;
358
	FUNLOCKFILE(fp);
368
	FUNLOCKFILE(fp);
359
	return (ret);
369
	return (ret);
360
}
370
}
(-)new/tools/regression/lib/libc/stdio/Makefile (-1 / +1 lines)
Lines 1-6 Link Here
1
# $FreeBSD: stable/9/tools/regression/lib/libc/stdio/Makefile 189142 2009-02-28 06:39:39Z das $
1
# $FreeBSD: stable/9/tools/regression/lib/libc/stdio/Makefile 189142 2009-02-28 06:39:39Z das $
2
2
3
TESTS=	test-getdelim test-perror test-print-positional test-printbasic test-printfloat test-scanfloat
3
TESTS=	test-getdelim test-perror test-print-positional test-printbasic test-printfloat test-scanfloat test-printreturn
4
CFLAGS+= -lm
4
CFLAGS+= -lm
5
5
6
.PHONY: tests
6
.PHONY: tests
(-)new/tools/regression/lib/libc/stdio/test-printreturn.c (+148 lines)
Line 0 Link Here
1
/*-
2
 * Copyright (c) 2012 Matthew Story <matthewstory@gmail.com>
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *	notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *	notice, this list of conditions and the following disclaimer in the
12
 *	documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
27
/*
28
 * Tests for correct return codes from f(w)printf
29
 */
30
31
#include <assert.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <stdint.h>
35
#include <wchar.h>
36
#include <errno.h>
37
#include <string.h>
38
39
#define BATTERY_OK "ok %d - printreturn\n"
40
41
#define run_battery(void)  \
42
	_run_battery(__LINE__)
43
44
void smash_stack(void);
45
void _run_battery(int line);
46
int battery = 1;
47
48
int
49
main() {
50
	/*
51
	 * All tests occur on stdout, this makes the tests quite noisy.
52
	 * but keeps them contained -- e.g. no reliance on external 
53
	 * files, or on manipulating file struct internals.
54
	 */
55
56
	printf("1..12\n");
57
	/* 
58
	 * without errors
59
	 */
60
	clearerr(stdout);
61
62
	/* line-buffered */
63
	setvbuf(stdout, (char *)NULL, _IOLBF, 0);
64
	run_battery();
65
66
	/* unbuffered */
67
	setvbuf(stdout, (char *)NULL, _IONBF, 0);
68
	run_battery();
69
70
	/* fully-buffered */
71
	setvbuf(stdout, (char *)NULL, _IOFBF, 0);
72
	run_battery();
73
74
	/*
75
	 * success with errors, would rather not manipulate _flags * directly, but
76
	 * can't find another direct way to set error, without an actual error.
77
	 */
78
	stdout->_flags |= __SERR;
79
80
	/* line-buffered */
81
	setvbuf(stdout, (char *)NULL, _IOLBF, 0);
82
	run_battery();
83
84
	/* unbuffered */
85
	setvbuf(stdout, (char *)NULL, _IONBF, 0);
86
	run_battery();
87
88
	/* fully-buffered */
89
	setvbuf(stdout, (char *)NULL, _IOFBF, 0);
90
	run_battery();
91
92
	return 0;
93
}
94
95
/* taken from test-printbasic.c */
96
void
97
smash_stack(void)
98
{
99
	static uint32_t junk = 0xdeadbeef;
100
	uint32_t buf[512];
101
	int i;
102
103
	for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
104
		buf[i] = junk;
105
}
106
107
void
108
_run_battery(int line) {
109
#define BUF 100
110
	extern int battery;
111
	wchar_t ws[BUF], wfmt[BUF];	
112
	char s[BUF];
113
114
	/* regular width tests, no replacement, and replacement */
115
	smash_stack();
116
	sprintf(s, BATTERY_OK, battery);
117
	if (0 > fprintf(stdout, s)) {
118
		fprintf(stderr, 
119
			"%d: failure in no replace printf (%d)\n",
120
			line, errno);
121
		abort();
122
	}
123
	++battery;
124
	smash_stack();
125
	if (0 > fprintf(stdout, BATTERY_OK, battery)) {
126
		fprintf(stderr, "%d: failure in replace printf (%d)\n", line, errno);
127
		abort();
128
	}
129
	++battery;
130
131
	/* wide tests, no replacement, and replacement */
132
	smash_stack();
133
	mbstowcs(wfmt, BATTERY_OK, BUF - 1);
134
	swprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, battery);
135
	if (0 > fwprintf(stdout, ws)) {
136
		fprintf(stderr,
137
			"%d: failure in no replace wprintf (%d)\n",
138
			line, errno);
139
		abort();
140
	}
141
	++battery;
142
	smash_stack();
143
	if (0 > fwprintf(stdout, wfmt, battery)) {
144
		fprintf(stderr, "%d: failure in replace wprintf (%d)\n", line, errno);
145
		abort();
146
	}
147
	++battery;
148
}

Return to bug 164674