diff --git a/libc/lib_internal.h b/libc/lib_internal.h index bf73b72a4d..1b7f232c52 100644 --- a/libc/lib_internal.h +++ b/libc/lib_internal.h @@ -186,7 +186,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream); /* Defined in lib_libfgets.c */ -FAR char *lib_fgets(FAR char *buf, int buflen, FILE *stream, +FAR char *lib_fgets(FAR char *buf, size_t buflen, FILE *stream, bool keepnl, bool consume); /* Defined in lib_libfflush.c */ diff --git a/libc/stdio/lib_fgets.c b/libc/stdio/lib_fgets.c index 3aff54db9e..47f35597c1 100644 --- a/libc/stdio/lib_fgets.c +++ b/libc/stdio/lib_fgets.c @@ -69,7 +69,17 @@ char *fgets(FAR char *buf, int buflen, FILE *stream) { + /* Handle negative buffer size */ + + if (buflen < 0) + { + return NULL; + } + /* Let lib_fgets() do the heavy lifting */ - return lib_fgets(buf, buflen, stream, true, false); + else + { + return lib_fgets(buf, (size_t)buflen, stream, true, false); + } } diff --git a/libc/stdio/lib_gets.c b/libc/stdio/lib_gets.c index e9e36987ba..d15c8bd08a 100644 --- a/libc/stdio/lib_gets.c +++ b/libc/stdio/lib_gets.c @@ -73,5 +73,5 @@ FAR char *gets(FAR char *s) { /* Let lib_fgets() do the heavy lifting */ - return lib_fgets(s, RSIZE_MAX, stdin, false, false); + return lib_fgets(s, (size_t)INT_MAX, stdin, false, false); } diff --git a/libc/stdio/lib_gets_s.c b/libc/stdio/lib_gets_s.c index 3f9ecc0cb2..54f8891f91 100644 --- a/libc/stdio/lib_gets_s.c +++ b/libc/stdio/lib_gets_s.c @@ -92,5 +92,5 @@ FAR char *gets_s(FAR char *s, rsize_t n) /* Then let lib_fgets() do the heavy lifting */ - return lib_fgets(s, n, stdin, false, true); + return lib_fgets(s, (size_t)n, stdin, false, true); } diff --git a/libc/stdio/lib_libfgets.c b/libc/stdio/lib_libfgets.c index 6a8cdeb448..bc41d94e51 100644 --- a/libc/stdio/lib_libfgets.c +++ b/libc/stdio/lib_libfgets.c @@ -139,7 +139,7 @@ static void consume_eol(FILE *stream, bool consume) * **************************************************************************/ -FAR char *lib_fgets(FAR char *buf, int buflen, FILE *stream, +FAR char *lib_fgets(FAR char *buf, size_t buflen, FILE *stream, bool keepnl, bool consume) { int nch = 0;