|
604 | 604 | end
|
605 | 605 | # The fast path here usually checks there is already room, then does nothing.
|
606 | 606 | # When append is true, new data is added after io.size, not io.ptr
|
607 |
| - existing_space = min(lastindex(io.data), io.maxsize + get_offset(io)) - (io.append ? io.size : io.ptr - 1) |
| 607 | + start_offset = io.append ? io.size : io.ptr - 1 |
| 608 | + existing_space = min(lastindex(io.data) - start_offset, io.maxsize - (start_offset - get_offset(io))) |
608 | 609 | if existing_space < nshort % Int
|
609 | 610 | # Outline this function to make it more likely that ensureroom inlines itself
|
610 | 611 | return ensureroom_slowpath(io, nshort, existing_space)
|
@@ -898,12 +899,13 @@ function unsafe_write(to::GenericIOBuffer, p::Ptr{UInt8}, nb::UInt)
|
898 | 899 | append = to.append
|
899 | 900 | ptr = append ? size+1 : to.ptr
|
900 | 901 | data = to.data
|
901 |
| - to_write = min(nb, (min(Int(length(data))::Int, to.maxsize + get_offset(to)) - ptr + 1) % UInt) % Int |
| 902 | + start_offset = ptr - 1 |
| 903 | + to_write = max(0, min(nb, (min(Int(length(data))::Int - start_offset, to.maxsize - (start_offset - get_offset(to)))) % UInt) % Int) |
902 | 904 | # Dispatch based on the type of data, to possibly allow using memcpy
|
903 | 905 | _unsafe_write(data, p, ptr, to_write % UInt)
|
904 | 906 | # Update to.size only if the ptr has advanced to higher than
|
905 | 907 | # the previous size. Otherwise, we just overwrote existing data
|
906 |
| - to.size = max(size, ptr + to_write - 1) |
| 908 | + to.size = max(size, start_offset + to_write) |
907 | 909 | # If to.append, we only update size, not ptr.
|
908 | 910 | if !append
|
909 | 911 | to.ptr = ptr + to_write
|
|
941 | 943 | ptr = (to.append ? to.size+1 : to.ptr)
|
942 | 944 | # We have just ensured there is room for 1 byte, EXCEPT if we were to exceed
|
943 | 945 | # maxsize. So, we just need to check that here.
|
944 |
| - if ptr > to.maxsize + get_offset(to) |
| 946 | + if ptr - get_offset(to) > to.maxsize |
945 | 947 | return 0
|
946 | 948 | else
|
947 | 949 | to.data[ptr] = a
|
|
0 commit comments