TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: NEIL HELLER
from: JONATHAN DE BOYNE POLLARD
date: 1998-02-04 16:44:00
subject: Beware of buffer overflow!

 AT>> char s[40];
 AT>> fgets (s, 40, stdin);
 NH> That will STILL give a case of the nasties if the user enters 40
 NH> characters.  You must make room for the terminating null.
 NH> char s[41];
 NH> fgets(s,40,stdin);
fgets() already makes room for the terminating NUL.  It reads at most *one 
less than* the number of characters specified in its second argument from the 
input stream given in its third, thus leaving room for a NUL to be appended 
without overflowing the buffer.
 ¯ JdeBP ®
--- FleetStreet 1.19 NR
---------------
* Origin: JdeBP's point, using Squish (2:440/4.3)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.