summaryrefslogtreecommitdiff
path: root/gl/getdelim.c
diff options
context:
space:
mode:
authorSimon Josefsson <simon@josefsson.org>2005-10-07 11:19:47 +0000
committerSimon Josefsson <simon@josefsson.org>2005-10-07 11:19:47 +0000
commit1a548fa5cab0aac1423e1b0c172e7f65313993e8 (patch)
tree45e97d060929366739697a937d00155310e1517e /gl/getdelim.c
parent84317cfe868b9f85621333a35ed8b817a985c4a0 (diff)
downloadgnutls-1a548fa5cab0aac1423e1b0c172e7f65313993e8.tar.gz
Update.
Diffstat (limited to 'gl/getdelim.c')
-rw-r--r--gl/getdelim.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/gl/getdelim.c b/gl/getdelim.c
index 76938121e6..ab9a4ebb68 100644
--- a/gl/getdelim.c
+++ b/gl/getdelim.c
@@ -23,11 +23,18 @@
# include <config.h>
#endif
+#include "getdelim.h"
+
+#include <limits.h>
#include <stdlib.h>
#include <errno.h>
-#include "getdelim.h"
-
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
+#endif
+#ifndef SSIZE_MAX
+# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
+#endif
#if !HAVE_FLOCKFILE
# undef flockfile
# define flockfile(x) ((void) 0)
@@ -46,9 +53,8 @@
ssize_t
getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
{
- int result = 0;
- ssize_t cur_len = 0;
- ssize_t len;
+ ssize_t result;
+ size_t cur_len = 0;
if (lineptr == NULL || n == NULL || fp == NULL)
{
@@ -71,23 +77,26 @@ getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
for (;;)
{
- char *t;
int i;
i = getc (fp);
if (i == EOF)
- {
- result = -1;
- break;
- }
+ {
+ result = -1;
+ break;
+ }
/* Make enough space for len+1 (for final NUL) bytes. */
if (cur_len + 1 >= *n)
{
- size_t needed = 2 * (cur_len + 1) + 1; /* Be generous. */
+ size_t needed_max =
+ SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
+ size_t needed = 2 * *n + 1; /* Be generous. */
char *new_lineptr;
- if (needed < cur_len)
+ if (needed_max < needed)
+ needed = needed_max;
+ if (cur_len + 1 >= needed)
{
result = -1;
goto unlock_return;