/*
Description: A strncpy generates a string with a missing NUL termination. A NUL is explicitely added to ensure that the string is NUL terminated.
Keywords: Port C Size0 Complex1 BufferOverflow Stack Strcpy NoNul
ValidArg: "a"*30
ValidArg: "a"*100
Copyright 2005 Fortify Software.
Permission is hereby granted, without written agreement or royalty fee, to
use, copy, modify, and distribute this software and its documentation for
any purpose, provided that the above copyright notice and the following
three paragraphs appear in all copies of this software.
IN NO EVENT SHALL FORTIFY SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF FORTIFY SOFTWARE HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMANGE.
FORTIFY SOFTWARE SPECIFICALLY DISCLAIMS ANY WARRANTIES INCLUDING, BUT NOT
LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
THE SOFTWARE IS PROVIDED ON AN "AS-IS" BASIS AND FORTIFY SOFTWARE HAS NO
OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
*/
#include <stdio.h>
#include <string.h>
/*
* we pick a round buffer size in hopes that the compiler lays these
* out next to each other without padding. Other layouts may
* inadvertantly NUL terminate the buffer with stack garbage.
*/
#define MAXSIZE 32
void
test(char *str)
{
char buf3[MAXSIZE];
char buf2[MAXSIZE];
char buf1[MAXSIZE];
/* strncpy does not NUL terminate if buffer isnt large enough */
strncpy(buf1, str, sizeof buf1); /* OK */
buf1[MAXSIZE-1] = '\0';
strncpy(buf2, "This is a Test string", sizeof buf2);
strcpy(buf3, buf1); /* OK */
}
int
main(int argc, char **argv)
{
char *userstr;
if(argc > 1) {
userstr = argv[1];
test(userstr);
}
return 0;
}
See the comments