Blame src/stdio/sscanf_long.c

nsz 462b4f
#include <stdlib.h>
nsz 462b4f
#include <stdio.h>
nsz 462b4f
#include <string.h>
nsz 462b4f
#include <math.h>
nsz 462b4f
#include <errno.h>
nsz 462b4f
#include <sys/resource.h>
nsz 462b4f
#include "test.h"
nsz 462b4f
nsz 462b4f
static void setrl(int r, long lim)
nsz 462b4f
{
nsz 462b4f
	struct rlimit rl;
nsz 462b4f
nsz 462b4f
	if (getrlimit(r, &rl))
nsz 462b4f
		error("getrlimit %d: %s\n", r, strerror(errno));
nsz 462b4f
	rl.rlim_cur = lim;
nsz 462b4f
	if (setrlimit(r, &rl))
nsz 462b4f
		error("setrlimit %d: %s\n", r, strerror(errno));
nsz 462b4f
}
nsz 462b4f
nsz 462b4f
int main(void)
nsz 462b4f
{
nsz 462b4f
	enum {n = 8*1024*1024};
nsz 462b4f
	char *s = malloc(n);
nsz 462b4f
	int i;
nsz 462b4f
	float f;
nsz 462b4f
	char c;
nsz 462b4f
nsz 462b4f
	if (!s)
nsz 462b4f
		return error("out of memory");
nsz 462b4f
	setrl(RLIMIT_STACK, 128*1024);
nsz 462b4f
nsz 462b4f
	for (i = 0; i < n; i++) s[i] = '1';
nsz 462b4f
	s[n-3] = ' ';
nsz 462b4f
	s[n-1] = 0;
nsz 462b4f
nsz 462b4f
	/*
nsz 462b4f
	 * stack overflow if scanf copies s on the stack (glibc)
nsz 462b4f
	 * same issue with %d except then storing the conversion
nsz 462b4f
	 * result is undefined behaviour
nsz 462b4f
	 */
nsz 462b4f
	i = sscanf(s, "%f %c", &f, &c);
nsz 462b4f
nsz 462b4f
	if (i != 2)
nsz 462b4f
		error("sscanf returned %d, want 2\n", i);
nsz 462b4f
	if (f != INFINITY)
nsz 462b4f
		error("sscanf(longnum, \"%%f\") read %f, want inf\n", f);
nsz 462b4f
	if (c != '1')
nsz 462b4f
		error("sscanf(\"1\", %%c) read '%c', want '1'\n", c);
nsz 462b4f
	free(s);
nsz 462b4f
	return test_status;
nsz 462b4f
}