so, the only check in the line "if (gets_s( buff, BUFFSIZE ) == NULL )" is to see if there is an absence of input in which the error is caught and handled. But does not check whether or not the amount of input is more than the specified buffer size, instead it just bombs out.
I rewrote the gets_s() function to behave the way that it should. It works but does anyone see any problems with it?
Code:
#include <stdio.h>
char * getss ( char *s, int size ) ;
int main ( int argc, char *argv[] )
{
char buffer[10] ;
printf ("Enter a string: ") ;
if (getss(buffer, 10) == NULL) printf ("Buffer Overloaded!") ;
else printf ("The line entered was: %s\n", buffer) ;
return 0 ;
}
char *getss(char *s, int size)
{
char *stringBuilder = (char *) malloc( size * sizeof( char )) ;
int i, k;
/* Read and store characters until reaching a newline or end-of-file */
for (i = 0; k != EOF && k != '\n'; i++)
{
k = getchar() ;
/* If a read error occurs, the resulting buffer is not usable. */
if (i > size) return NULL ;
if (k == EOF && !feof(stdin)) return NULL;
stringBuilder[i] = k;
}
/* Null-terminate and return the buffer on success.
The newline is not stored in the buffer. */
//stringBuilder[i] = '\0';
strcpy( s, stringBuilder ) ;
return s;
}