projects
/
musl
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
fix strptime output for %C without %y
[musl]
/
src
/
stdio
/
getdelim.c
diff --git
a/src/stdio/getdelim.c
b/src/stdio/getdelim.c
index
5015c3a
..
1ccd802
100644
(file)
--- a/
src/stdio/getdelim.c
+++ b/
src/stdio/getdelim.c
@@
-1,4
+1,7
@@
#include "stdio_impl.h"
#include "stdio_impl.h"
+#include <string.h>
+#include <inttypes.h>
+#include <errno.h>
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#define MIN(a,b) ((a)<(b) ? (a) : (b))
@@
-10,29
+13,32
@@
ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restric
size_t i=0;
int c;
size_t i=0;
int c;
+ FLOCK(f);
+
if (!n || !s) {
if (!n || !s) {
+ f->flags |= F_ERR;
+ FUNLOCK(f);
errno = EINVAL;
return -1;
}
if (!*s) *n=0;
errno = EINVAL;
return -1;
}
if (!*s) *n=0;
- FLOCK(f);
-
for (;;) {
z = memchr(f->rpos, delim, f->rend - f->rpos);
k = z ? z - f->rpos + 1 : f->rend - f->rpos;
for (;;) {
z = memchr(f->rpos, delim, f->rend - f->rpos);
k = z ? z - f->rpos + 1 : f->rend - f->rpos;
- if (i+k >= *n) {
+ if (i+k
+1
>= *n) {
if (k >= SIZE_MAX/2-i) goto oom;
if (k >= SIZE_MAX/2-i) goto oom;
-
*n
= i+k+2;
- if (
*n < SIZE_MAX/4) *n *=
2;
- tmp = realloc(*s,
*n
);
+
size_t m
= i+k+2;
+ if (
!z && m < SIZE_MAX/4) m += m/
2;
+ tmp = realloc(*s,
m
);
if (!tmp) {
if (!tmp) {
-
*n
= i+k+2;
- tmp = realloc(*s,
*n
);
+
m
= i+k+2;
+ tmp = realloc(*s,
m
);
if (!tmp) goto oom;
}
*s = tmp;
if (!tmp) goto oom;
}
*s = tmp;
+ *n = m;
}
memcpy(*s+i, f->rpos, k);
f->rpos += k;
}
memcpy(*s+i, f->rpos, k);
f->rpos += k;
@@
-53,6
+59,7
@@
ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restric
return i;
oom:
return i;
oom:
+ f->flags |= F_ERR;
FUNLOCK(f);
errno = ENOMEM;
return -1;
FUNLOCK(f);
errno = ENOMEM;
return -1;