From f1888900bdf97e91a00ea2c28dff8611540ccc84 Mon Sep 17 00:00:00 2001 From: Jan Vaclav Date: Mon, 17 Jun 2024 11:24:42 +0200 Subject: [PATCH] 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: 3bda3fb60c10 ('nmtui: initial import of nmtui') (cherry picked from commit 13317bd53656dad69f17f493bf085db9d07c8407) --- src/libnmt-newt/nmt-newt-utils.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/libnmt-newt/nmt-newt-utils.c b/src/libnmt-newt/nmt-newt-utils.c index 8eeee2026b..36a47b2b61 100644 --- a/src/libnmt-newt/nmt-newt-utils.c +++ b/src/libnmt-newt/nmt-newt-utils.c @@ -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;