Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ dnl Checks for library functions.
AC_TYPE_GETGROUPS
AC_FUNC_MMAP

AC_CHECK_FUNCS(strerror strtol lstat setrlimit sigrelse sighold sigaction \
sysconf sigsetjmp getrusage mmap mprotect)
AC_CHECK_FUNCS(strerror strtol lseek lstat setrlimit sigrelse sighold \
sigaction sysconf sigsetjmp getrusage mmap mprotect)

AC_CACHE_CHECK(whether getenv can be redefined, es_cv_local_getenv,
[if test "$ac_cv_header_stdlib_h" = no || test "$ac_cv_header_stdc" = no; then
Expand Down
34 changes: 29 additions & 5 deletions prim-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,35 @@ PRIM(read) {
freebuffer(buffer);
buffer = openbuffer(0);

while ((c = read1(fd)) != EOF && c != '\n')
if (c == '\0')
fail("$&read", "%%read: null character encountered");
else
buffer = bufputc(buffer, c);
#if HAVE_LSEEK
if (lseek(fd, 0, SEEK_CUR) < 0) {
#endif
while ((c = read1(fd)) != EOF && c != '\n')
if (c == '\0')
fail("$&read", "%%read: null character encountered");
else
buffer = bufputc(buffer, c);
#if HAVE_LSEEK
} else {
int n;
char *p;
char s[BUFSIZE];
c = EOF;
while ((n = eread(fd, s, BUFSIZE)) > 0) {
c = 0;
if ((p = memchr(s, '\0', n)) != NULL) {
lseek(fd, 1 + ((p - s) - n), SEEK_CUR);
fail("$&read", "%%read: null character encountered");
} else if ((p = strchr(s, '\n')) != NULL) {
buffer = bufncat(buffer, s, (p - s));
lseek(fd, 1 + ((p - s) - n), SEEK_CUR);
break;
} else {
buffer = bufncat(buffer, s, n);
}
}
}
#endif

if (c == EOF && buffer->current == 0) {
freebuffer(buffer);
Expand Down
38 changes: 38 additions & 0 deletions test/tests/read.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/local/bin/es

test 'null reading' {
let (tmp = `{mktemp test-nul.XXXXXX})
unwind-protect {
echo first line > $tmp
./testrun 0 >> $tmp

let (fl = (); ex = (); remainder = ()) {
catch @ e {
ex = $e
remainder = <=%read
} {
fl = <=%read
%read
} < $tmp
assert {~ $fl 'first line'} 'seeking read reads valid line'
assert {~ $ex(3) *'null character encountered'*} 'seeking read throws exception correctly'
assert {~ $remainder 'sult 6'} 'seeking read leaves file in correct state:' $remainder
}

let ((fl ex remainder) = `` \n {
let (fl = ())
cat $tmp | catch @ e {
echo $fl\n$e(3)\n^<=%read
} {
fl = <=%read
%read
}
}) {
assert {~ $fl 'first line'} 'non-seeking read reads valid line'
assert {~ $ex *'null character encountered'*} 'non-seeking read throws exception correctly'
assert {~ $remainder 'sult 6'} 'non-seeking read leaves file in correct state'
}
} {
rm -f $tmp
}
}