feat: add bounded read-to-end and compatible file paths
This commit is contained in:
+4
-4
@@ -19,8 +19,8 @@ CloseError :: enum {
|
||||
close_failed
|
||||
}
|
||||
|
||||
#! open a caller-owned handle; release it with close.
|
||||
open proc(io Io, path [;0]u8, mode FileMode) File ! OpenError {
|
||||
#! open a caller-owned handle from a borrowed zero-terminated path; release it with close.
|
||||
open proc(io Io, path [*;0]u8, mode FileMode) File ! OpenError {
|
||||
handle Handle :: try io.vtable.open(io.context, path, mode)
|
||||
return File{ io = io, handle = handle }
|
||||
}
|
||||
@@ -83,7 +83,7 @@ system_write proc(_ ?@mut anyopaque, handle Handle, bytes []u8) usize ! WriteErr
|
||||
}
|
||||
|
||||
@hide:file
|
||||
system_open proc(_ ?@mut anyopaque, path [;0]u8, mode FileMode) Handle ! OpenError {
|
||||
system_open proc(_ ?@mut anyopaque, path [*;0]u8, mode FileMode) Handle ! OpenError {
|
||||
flags := c.O_RDONLY
|
||||
match mode {
|
||||
.read_only: flags = c.O_RDONLY
|
||||
@@ -91,7 +91,7 @@ system_open proc(_ ?@mut anyopaque, path [;0]u8, mode FileMode) Handle ! OpenErr
|
||||
.read_write: flags = c.O_RDWR
|
||||
}
|
||||
while true {
|
||||
fd c_int :: c.open(ptrcast!(path.ptr), flags)
|
||||
fd c_int :: c.open(ptrcast!(path), flags)
|
||||
if (fd >= 0) return Handle{ file_desc = fd }
|
||||
if (c.__error()?^ != c.EINTR) return .open_failed
|
||||
}
|
||||
|
||||
+36
-1
@@ -1,5 +1,6 @@
|
||||
import "@ffi/c"
|
||||
import "@std/mem"
|
||||
import "@std/arraylist"
|
||||
# typeinfo! requires this import even without a qualified meta reference.
|
||||
import "@std/meta"
|
||||
|
||||
@@ -8,6 +9,12 @@ ReadError :: enum {
|
||||
read_failed
|
||||
}
|
||||
|
||||
ReadLimitError :: enum {
|
||||
limit_exceeded
|
||||
}
|
||||
|
||||
ReadAllError :: alias (mem.AllocError | ReadError | ReadLimitError)
|
||||
|
||||
WriteError :: enum {
|
||||
not_open_for_writing
|
||||
write_failed
|
||||
@@ -34,7 +41,7 @@ Io :: struct {
|
||||
IoVTable :: struct {
|
||||
read @proc(context ?@mut anyopaque, handle Handle, buffer []mut u8) usize ! ReadError
|
||||
write @proc(context ?@mut anyopaque, handle Handle, bytes []u8) usize ! WriteError
|
||||
open @proc(context ?@mut anyopaque, path [;0]u8, mode FileMode) Handle ! OpenError
|
||||
open @proc(context ?@mut anyopaque, path [*;0]u8, mode FileMode) Handle ! OpenError
|
||||
close @proc(context ?@mut anyopaque, handle Handle) void ! CloseError
|
||||
stdin @proc(context ?@mut anyopaque) Handle
|
||||
stdout @proc(context ?@mut anyopaque) Handle
|
||||
@@ -64,6 +71,34 @@ read proc(input Reader, buffer []mut u8) usize ! ReadError {
|
||||
return count
|
||||
}
|
||||
|
||||
#! read to EOF, accepting at most `maximum` bytes; the reader remains caller-owned.
|
||||
#! exceeding the limit consumes one extra byte and returns limit_exceeded.
|
||||
#! returned storage retains `allocator`; release it with arraylist.deinit.
|
||||
#! all allocated storage is released on error.
|
||||
read_all proc(allocator mem.Allocator, input Reader, maximum usize) arraylist.ArrayList(u8) ! ReadAllError {
|
||||
bytes := arraylist.init(u8, allocator)
|
||||
errdefer arraylist.deinit(&bytes)
|
||||
while true {
|
||||
length :: bytes.items.len
|
||||
if (length == maximum) {
|
||||
probe [1]mut u8 := undefined
|
||||
if (try read(input, (&probe)[..]) != 0) return .limit_exceeded
|
||||
return bytes
|
||||
}
|
||||
if (length == bytes.capacity) {
|
||||
growth := usize(4096)
|
||||
if (growth > maximum - length) growth = maximum - length
|
||||
try arraylist.reserve(&bytes, length + growth)
|
||||
}
|
||||
storage :: bytes.items.ptr[..bytes.capacity]
|
||||
end := bytes.capacity
|
||||
if (end > maximum) end = maximum
|
||||
count :: try read(input, storage[length..end])
|
||||
if (count == 0) return bytes
|
||||
bytes.items = storage[..length + count]
|
||||
}
|
||||
}
|
||||
|
||||
write proc(output Writer, bytes []u8) usize ! WriteError {
|
||||
if (bytes.len == 0) return 0
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import "@std/mem"
|
||||
import "@std/arraylist"
|
||||
import "@std/testing"
|
||||
|
||||
@hide:file
|
||||
Input :: struct {
|
||||
bytes []u8
|
||||
offset usize = 0
|
||||
chunk usize = 137
|
||||
fail_at usize = maxval!(usize)
|
||||
remaining usize = maxval!(usize)
|
||||
live usize = 0
|
||||
}
|
||||
|
||||
@hide:file
|
||||
input_state proc(context ?@mut anyopaque) @mut Input {
|
||||
if (context) |pointer| return ptrcast!(pointer)
|
||||
unreachable
|
||||
}
|
||||
|
||||
@hide:file
|
||||
input_read proc(context ?@mut anyopaque, _ Handle, buffer []mut u8) usize ! ReadError {
|
||||
state :: input_state(context)
|
||||
if (state.offset >= state.fail_at) return .read_failed
|
||||
count := state.bytes.len - state.offset
|
||||
if (count > buffer.len) count = buffer.len
|
||||
if (count > state.chunk) count = state.chunk
|
||||
memcopy!(buffer[..count], state.bytes[state.offset..state.offset + count])
|
||||
state.offset += count
|
||||
return count
|
||||
}
|
||||
|
||||
@hide:file
|
||||
input_reader proc(state @mut Input) Reader {
|
||||
return Reader{ context = state, handle = Handle{ file_desc = -1 }, read = input_read }
|
||||
}
|
||||
|
||||
@hide:file
|
||||
tracked_alloc proc(context ?@mut anyopaque, size usize, alignment usize) ?*mut u8 {
|
||||
state :: input_state(context)
|
||||
if (state.remaining == 0) return null
|
||||
state.remaining -= 1
|
||||
memory :: mem.raw_alloc(mem.c_allocator, size, alignment)
|
||||
if (memory) |_| state.live += 1
|
||||
return memory
|
||||
}
|
||||
|
||||
@hide:file
|
||||
tracked_realloc proc(context ?@mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
|
||||
state :: input_state(context)
|
||||
if (state.remaining == 0) return null
|
||||
state.remaining -= 1
|
||||
resized :: mem.raw_realloc(mem.c_allocator, memory, old_size, new_size, alignment)
|
||||
if (resized) |_| {
|
||||
if (memory) |_| {} else state.live += 1
|
||||
}
|
||||
return resized
|
||||
}
|
||||
|
||||
@hide:file
|
||||
tracked_free proc(context ?@mut anyopaque, memory ?*mut u8, size usize, alignment usize) void {
|
||||
state :: input_state(context)
|
||||
if (memory) |_| state.live -= 1
|
||||
mem.raw_free(mem.c_allocator, memory, size, alignment)
|
||||
}
|
||||
|
||||
@hide:file
|
||||
tracked_vtable :: mem.AllocatorVTable{ alloc = tracked_alloc, realloc = tracked_realloc, free = tracked_free }
|
||||
|
||||
read_all_preserves_short_reads_across_buffer_growth test {
|
||||
source [9000]mut u8 := undefined
|
||||
for (0..source.len) |i| source[i] = u8(rem!(i, 251))
|
||||
state := Input{ bytes = source[..] }
|
||||
bytes := try read_all(mem.c_allocator, input_reader(&state), maxval!(usize))
|
||||
defer arraylist.deinit(&bytes)
|
||||
try testing.expect_equal(source[..], bytes.items)
|
||||
}
|
||||
|
||||
read_all_accepts_exact_limits_and_probes_for_excess test {
|
||||
for (usize(0)..4) |maximum| {
|
||||
state := Input{ bytes = "abc"[..maximum], chunk = 1 }
|
||||
bytes := try read_all(mem.c_allocator, input_reader(&state), maximum)
|
||||
defer arraylist.deinit(&bytes)
|
||||
try testing.expect_equal(state.bytes, bytes.items)
|
||||
|
||||
excess := Input{ bytes = "abcd", chunk = 1 }
|
||||
unexpected := read_all(mem.c_allocator, input_reader(&excess), maximum) catch |err| {
|
||||
try testing.expect(err == .limit_exceeded)
|
||||
try testing.expect_equal(maximum + 1, excess.offset)
|
||||
continue
|
||||
}
|
||||
arraylist.deinit(&unexpected)
|
||||
try testing.expect(false)
|
||||
}
|
||||
}
|
||||
|
||||
read_all_releases_partial_storage_on_read_failure_and_limit_excess test {
|
||||
for (0..2) |failure| {
|
||||
state := Input{ bytes = "abcdef", chunk = 2 }
|
||||
if (failure == 0) state.fail_at = 2
|
||||
allocator :: mem.Allocator{ context = &state, vtable = &tracked_vtable }
|
||||
unexpected := read_all(allocator, input_reader(&state), 4) catch |err| {
|
||||
expected ReadAllError :: if (failure == 0) .read_failed else .limit_exceeded
|
||||
try testing.expect_equal(expected, err)
|
||||
try testing.expect_equal(0, state.live)
|
||||
continue
|
||||
}
|
||||
arraylist.deinit(&unexpected)
|
||||
try testing.expect(false)
|
||||
}
|
||||
}
|
||||
|
||||
read_all_releases_storage_at_each_failed_growth test {
|
||||
source [9000]mut u8 := undefined
|
||||
mem.set(source[..], u8('x'))
|
||||
for (usize(0)..3) |remaining| {
|
||||
state := Input{ bytes = source[..], remaining = remaining }
|
||||
allocator :: mem.Allocator{ context = &state, vtable = &tracked_vtable }
|
||||
unexpected := read_all(allocator, input_reader(&state), source.len) catch |err| {
|
||||
try testing.expect(err == .out_of_memory)
|
||||
try testing.expect_equal(0, state.live)
|
||||
continue
|
||||
}
|
||||
arraylist.deinit(&unexpected)
|
||||
try testing.expect(false)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user