Debian Codegraph @

lib/getdelim.c

raw
1 /* getdelim.c --- Implementation of replacement getdelim function.
2 Copyright (C) 1994, 1996-1998, 2001, 2003, 2005-2026 Free Software
3 Foundation, Inc.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Ported from glibc by Simon Josefsson. */
19
20 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
21 optimizes away the lineptr == NULL || n == NULL || fp == NULL tests below. */
22 #define _GL_ARG_NONNULL(params)
23
24 #include <config.h>
25
26 #include <stdio.h>
27
28 #include <limits.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <errno.h>
32
33 #if USE_UNLOCKED_IO
34 # include "unlocked-io.h"
35 # define getc_maybe_unlocked(fp) getc(fp)
36 #elif !HAVE_FLOCKFILE || !HAVE_FUNLOCKFILE || !HAVE_DECL_GETC_UNLOCKED
37 # undef flockfile
38 # undef funlockfile
39 # define flockfile(x) ((void) 0)
40 # define funlockfile(x) ((void) 0)
41 # define getc_maybe_unlocked(fp) getc(fp)
42 #else
43 # define getc_maybe_unlocked(fp) getc_unlocked(fp)
44 #endif
45
46 static void
47 alloc_failed (void)
48 {
49 #if defined _WIN32 && ! defined __CYGWIN__
50 /* Avoid errno problem without using the realloc module; see:
51 https://lists.gnu.org/r/bug-gnulib/2016-08/msg00025.html */
52 errno = ENOMEM;
53 #endif
54 }
55
56 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
57 NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
58 NULL), pointing to *N characters of space. It is realloc'ed as
59 necessary. Returns the number of characters read (not including
60 the null terminator), or -1 on error or EOF. */
61
62 ssize_t
63 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
64 {
65 if (lineptr == NULL || n == NULL
66 /* glibc already declares this function as __nonnull ((4)).
67 Avoid a gcc warning "‘nonnull’ argument ‘fp’ compared to NULL". */
68 #if !(__GLIBC__ >= 2)
69 || fp == NULL
70 #endif
71 )
72 {
73 errno = EINVAL;
74 return -1;
75 }
76
77 flockfile (fp);
78
79 ssize_t result;
80
81 if (*lineptr == NULL || *n == 0)
82 {
83 char *new_lineptr;
84 *n = 120;
85 new_lineptr = (char *) realloc (*lineptr, *n);
86 if (new_lineptr == NULL)
87 {
88 alloc_failed ();
89 result = -1;
90 goto unlock_return;
91 }
92 *lineptr = new_lineptr;
93 }
94
95 {
96 size_t cur_len = 0;
97 for (;;)
98 {
99 int i;
100
101 i = getc_maybe_unlocked (fp);
102 if (i == EOF)
103 {
104 result = -1;
105 break;
106 }
107
108 /* Make enough space for len+1 (for final NUL) bytes. */
109 if (cur_len + 1 >= *n)
110 {
111 size_t needed_max =
112 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
113 size_t needed = 2 * *n + 1; /* Be generous. */
114
115 if (needed_max < needed)
116 needed = needed_max;
117 if (cur_len + 1 >= needed)
118 {
119 result = -1;
120 errno = EOVERFLOW;
121 goto unlock_return;
122 }
123
124 char *new_lineptr = (char *) realloc (*lineptr, needed);
125 if (new_lineptr == NULL)
126 {
127 alloc_failed ();
128 result = -1;
129 goto unlock_return;
130 }
131
132 *lineptr = new_lineptr;
133 *n = needed;
134 }
135
136 (*lineptr)[cur_len] = i;
137 cur_len++;
138
139 if (i == delimiter)
140 break;
141 }
142 (*lineptr)[cur_len] = '\0';
143 result = cur_len ? cur_len : result;
144 }
145
146 unlock_return:
147 funlockfile (fp); /* doesn't set errno */
148
149 return result;
150 }