nmtui: handle write() errors correctly in nmt_newt_edit_string

It might happen that write() returns -1, but the errno is not EINTR.
In that case, the length would be incremented by 1, and the data pointer
to the data being written would be moved back by 1 byte on every error.

Make it so that the function exits with an error if it indicates an error.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1971

Fixes: 3bda3fb60c ('nmtui: initial import of nmtui')
This commit is contained in:
Jan Vaclav 2024-06-17 11:24:42 +02:00 committed by Íñigo Huguet
parent 4b66f9fe71
commit 13317bd536

View file

@ -416,9 +416,18 @@ nmt_newt_edit_string(const char *data)
len = data ? strlen(data) : 0;
while (len) {
do
nwrote = write(fd, data, len);
while (nwrote == -1 && errno == EINTR);
nwrote = write(fd, data, len);
if (nwrote == -1) {
if (errno == EINTR) {
continue;
}
nmt_newt_message_dialog(_("Could not write to temporary file: %s"),
nm_strerror_native(errno));
nm_close(fd);
goto done;
}
len -= nwrote;
data += nwrote;