diff --git a/runtime/hsa-runtime/libamdhsacode/amd_elf_image.cpp b/runtime/hsa-runtime/libamdhsacode/amd_elf_image.cpp index 2fdaab0b2d..87d3407a16 100644 --- a/runtime/hsa-runtime/libamdhsacode/amd_elf_image.cpp +++ b/runtime/hsa-runtime/libamdhsacode/amd_elf_image.cpp @@ -213,10 +213,19 @@ namespace elf { #else // _WIN32 int in = _open(filename.c_str(), O_RDONLY); if (in < 0) { return perror("open failed"); } - if (_lseek(in, 0L, SEEK_END) < 0) { return perror("lseek failed"); } + if (_lseek(in, 0L, SEEK_END) < 0) { + _close(in); + return perror("lseek failed"); + } off_t size; - if ((size = _lseek(in, 0L, SEEK_CUR)) < 0) { return perror("lseek(2) failed"); } - if (_lseek(in, 0L, SEEK_SET) < 0) { return perror("lseek(3) failed"); } + if ((size = _lseek(in, 0L, SEEK_CUR)) < 0) { + _close(in); + return perror("lseek(2) failed"); + } + if (_lseek(in, 0L, SEEK_SET) < 0) { + _close(in); + return perror("lseek(3) failed"); + } if (_lseek(d, 0L, SEEK_SET) < 0) { return perror("lseek(3) failed"); } ssize_t written; do {