[comp.lang.c] Secure 'C' library - string I/O

peter@ficc.uu.net (Peter da Silva) (11/10/88)

What is really needed is a whole secure 'C' library. Consider this gem:

..........
#include <utmp.h>
#define LOGNAMESIZE 8

	char buffer[LOGNAMESIZE*2+3];
	struct utmp *up;

	sprintf(x, "%s %s\n", up->ut_line, up->ut_name);
..........

In general, sprintf should have an argument describing the size of the
output buffer. A better solution still would be to can sprintf and replace
it with something like:

	FILE *stropen(buffer, length, mode);

	Opens a string as a file. Mode is one of:

		"r"	Read.
		"w"	Write.
		"a"	Append.
		"r+"	Read/update.
		"w+"	Write/update.
		"a+"	Append/update.

	Any of the normal I/O functions will operate on this internal file.
	A null will be written at the high water mark when the file pointer
	is fclosed. fseek operates within the string. In 'append' mode the
	string's "end" will be considered to be the first "null". In "Write"
	and "Append" mode the string will be null-filled on open.

It's safer and cleaner. 

There are other goodies like this, have a look...
-- 
Peter da Silva  `-_-'  Ferranti International Controls Corporation
"Have you hugged  U  your wolf today?"     uunet.uu.net!ficc!peter
Disclaimer: My typos are my own damn business.   peter@ficc.uu.net

chris@mimsy.UUCP (Chris Torek) (11/11/88)

In article <2185@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>In general, sprintf should have an argument describing the size of the
>output buffer. A better solution still would be to can sprintf and replace
>it with something like:
>
>	FILE *stropen(buffer, length, mode);

[mimsy73] grep fmemopen /usr/include/stdio.h
FILE	*fmemopen _PROTO_((char *addr, unsigned int len, const char *mode));
[mimsy74]

(The `_PROTO_(x)' macro expands to `x' if __STDC__, else to `'.)  [The
prototypes in our <stdio.h> wreak minor havoc with GCC.  GCC needs more
type-correctness internally.]

Something similar should appear in 4.4BSD.

>	Any of the normal I/O functions will operate on this internal file.
>	A null will be written at the high water mark when the file pointer
>	is fclosed. fseek operates within the string.

fseek does not currently work on my `fmem' function (for no good
reason); it also does not append a NUL (you can do that yourself
directly with putc() and by using sizeof(str)-1 and setting
str[sizeof(str)-1]=0).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

mike@stolaf.UUCP (Mike Haertel) (11/13/88)

In article <14486@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
>(The `_PROTO_(x)' macro expands to `x' if __STDC__, else to `'.)  [The
>prototypes in our <stdio.h> wreak minor havoc with GCC.  GCC needs more
>type-correctness internally.]

Have you reported this directly to RMS (bug-gcc@prep.ai.mit.edu)?
If you do (and better, if you send him a fix) he will most likely act
on it within the week.  (My experience this summer was that he fixed
bugs the same day I complained about them . . .)

---
Mike Haertel
Really mike@stolaf.UUCP, but I read mail at mike@wheaties.ai.mit.edu.