Skip to content

Commit e0b94b7

Browse files
authored
chd: Handle short writes (#15902)
Check the number of bytes written by CHD file write operations and report an I/O error when a write completes without writing all requested data Also, prevent the alignment padding loop from getting stuck if a write succeeds without making any progress Used ChatGPT (GPT-5.6 Sol) to review the code, identify the short-write handling issues, and help prepare the fix.
1 parent c690a9e commit e0b94b7

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/lib/util/chd.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ inline std::error_condition chd_file::file_write(uint64_t offset, const void *so
194194
err = m_file->seek(offset, SEEK_SET);
195195
if (UNEXPECTED(err))
196196
return err;
197-
return write(*m_file, source, length).first;
197+
size_t count;
198+
std::tie(err, count) = write(*m_file, source, length);
199+
if (UNEXPECTED(!err && (count != length)))
200+
return std::error_condition(std::errc::io_error);
201+
return err;
198202
}
199203

200204

@@ -235,6 +239,8 @@ inline uint64_t chd_file::file_append(const void *source, uint32_t length, uint3
235239
std::tie(err, count) = write(*m_file, buffer, bytes_to_write);
236240
if (UNEXPECTED(err))
237241
throw err;
242+
if (UNEXPECTED(!count))
243+
throw std::error_condition(std::errc::io_error);
238244
delta -= count;
239245
}
240246
}
@@ -245,9 +251,12 @@ inline uint64_t chd_file::file_append(const void *source, uint32_t length, uint3
245251
err = m_file->tell(offset);
246252
if (UNEXPECTED(err))
247253
throw err;
248-
std::tie(err, std::ignore) = write(*m_file, source, length);
254+
size_t count;
255+
std::tie(err, count) = write(*m_file, source, length);
249256
if (UNEXPECTED(err))
250257
throw err;
258+
if (UNEXPECTED(count != length))
259+
throw std::error_condition(std::errc::io_error);
251260
return offset;
252261
}
253262

0 commit comments

Comments
 (0)