Skip to content

Commit

Permalink
ARROW-15626: [GLib] Fix a bug that GArrowGIOInputStream may not read …
Browse files Browse the repository at this point in the history
…enough data

If GIOInputStream is used with a file descriptor that can return short reads, it will too. This breaks the assumption by callers that the Read() will return all that it asked for.

Closes apache#12380 from rsaccoccio/short_read

Lead-authored-by: Rob Saccoccio <rob.saccoccio@sas.com>
Co-authored-by: rsaccoccio <38737870+rsaccoccio@users.noreply.github.com>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
3 people committed Apr 20, 2022
1 parent 4354ad7 commit 0650241
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions c_glib/arrow-glib/input-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,17 +802,19 @@ namespace garrow {
arrow::Result<int64_t> Read(int64_t n_bytes, void *out) override {
std::lock_guard<std::mutex> guard(lock_);
GError *error = NULL;
auto n_read_bytes = g_input_stream_read(input_stream_,
out,
n_bytes,
NULL,
&error);
if (n_read_bytes == -1) {
gsize n_read_bytes = 0;
auto success = g_input_stream_read_all(input_stream_,
out,
n_bytes,
&n_read_bytes,
NULL,
&error);
if (success) {
return n_read_bytes;
} else {
return garrow_error_to_status(error,
arrow::StatusCode::IOError,
"[gio-input-stream][read]");
} else {
return n_read_bytes;
}
}

Expand All @@ -833,20 +835,22 @@ namespace garrow {

std::lock_guard<std::mutex> guard(lock_);
GError *error = NULL;
auto n_read_bytes = g_input_stream_read(input_stream_,
buffer->mutable_data(),
n_bytes,
NULL,
&error);
if (n_read_bytes == -1) {
return garrow_error_to_status(error,
arrow::StatusCode::IOError,
"[gio-input-stream][read][buffer]");
} else {
if (n_read_bytes < n_bytes) {
gsize n_read_bytes = 0;
auto success = g_input_stream_read_all(input_stream_,
buffer->mutable_data(),
n_bytes,
&n_read_bytes,
NULL,
&error);
if (success) {
if (n_read_bytes < static_cast<gsize>(n_bytes)) {
RETURN_NOT_OK(buffer->Resize(n_read_bytes));
}
return std::move(buffer);
} else {
return garrow_error_to_status(error,
arrow::StatusCode::IOError,
"[gio-input-stream][read][buffer]");
}
}

Expand Down

0 comments on commit 0650241

Please sign in to comment.