Skip to content

Commit b9d1432

Browse files
authored
[flang-rt][device] Use snprintf result for length (#172239)
The buffer might not be null terminated on the device and result in 1 byte invalid read when trying to get the length.
1 parent adaca13 commit b9d1432

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

flang-rt/lib/runtime/external-unit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ bool ExternalFileUnit::OpenAnonymousUnit(common::optional<OpenStatus> status,
201201
// I/O to an unconnected unit reads/creates a local file, e.g. fort.7
202202
std::size_t pathMaxLen{32};
203203
auto path{SizedNew<char>{handler}(pathMaxLen)};
204-
std::snprintf(path.get(), pathMaxLen, "fort.%d", unitNumber_);
204+
int len = std::snprintf(path.get(), pathMaxLen, "fort.%d", unitNumber_);
205205
OpenUnit(status, action, position, std::move(path),
206-
runtime::strlen(path.get()), convert, handler);
206+
len >= 0 ? static_cast<std::size_t>(len) : 0, convert, handler);
207207
return IsConnected();
208208
}
209209

0 commit comments

Comments
 (0)