[libvirt] [PATCH] rpc: remove trailing carriage return character in error string

If the error message string ends with '\r\n', it is not enough to remove '\n' only which leads to messed string. Example, after three time incorrect password input, virsh command virsh -c qemu://remoteserver/system will report error like: : Connection reset by peerey,gssapi-keyex,gssapi-with-mic,password). it should be: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). : Connection reset by peer The terminal interprets '\r' as "move the cursor back to the start of the current line", so we have to remove the character if it exists. --- src/rpc/virnetsocket.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-) diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 08dfbb0..8385d88 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -985,8 +985,14 @@ reread: virFileReadLimFD(sock->errfd, 1024, &errout) >= 0 && errout != NULL) { size_t elen = strlen(errout); - if (elen && errout[elen-1] == '\n') + if (elen && errout[elen-1] == '\n') { errout[elen-1] = '\0'; + + /* remove trailing '\r' if it exists */ + if (elen-1 && errout[elen-2] == '\r') { + errout[elen-2] = '\0'; + } + } } if (ret < 0) { -- 1.7.7.5

On 07/18/2012 04:15 AM, Guannan Ren wrote:
If the error message string ends with '\r\n', it is not enough to remove '\n' only which leads to messed string. Example, after three time incorrect password input, virsh command virsh -c qemu://remoteserver/system will report error like:
: Connection reset by peerey,gssapi-keyex,gssapi-with-mic,password).
it should be: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). : Connection reset by peer
The terminal interprets '\r' as "move the cursor back to the start of the current line", so we have to remove the character if it exists. --- src/rpc/virnetsocket.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)
Looks okay, but...
diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 08dfbb0..8385d88 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -985,8 +985,14 @@ reread: virFileReadLimFD(sock->errfd, 1024, &errout) >= 0 && errout != NULL) { size_t elen = strlen(errout); - if (elen && errout[elen-1] == '\n') + if (elen && errout[elen-1] == '\n') { errout[elen-1] = '\0'; + + /* remove trailing '\r' if it exists */ + if (elen-1 && errout[elen-2] == '\r') { + errout[elen-2] = '\0'; + } + }
Rather than special-casing just '\r\n' and '\n', should we instead: while (elen && c_isspace(errout[elen - 1])) errout[--elen] = '\0'; to trim ALL trailing whitespace? -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

Instead of only removing the ending newline character, it is better to remove all of standard whitespace character for the sake of log format. One example that we have to do this is: After three times incorrect password input, virsh command virsh -c qemu://remoteserver/system will report error like: : Connection reset by peerey,gssapi-keyex,gssapi-with-mic,password). But it should be: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). : Connection reset by peer The reason is that we dropped the newline, but have a '\r' left. The terminal interprets it as "move the cursor back to the start of the current line", so the error string is messed up. --- src/rpc/virnetsocket.c | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 08dfbb0..eeee309 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -35,6 +35,7 @@ # include <netinet/tcp.h> #endif +#include "c-ctype.h" #include "virnetsocket.h" #include "util.h" #include "memory.h" @@ -985,8 +986,9 @@ reread: virFileReadLimFD(sock->errfd, 1024, &errout) >= 0 && errout != NULL) { size_t elen = strlen(errout); - if (elen && errout[elen-1] == '\n') - errout[elen-1] = '\0'; + /* remove trailing whitespace */ + while (elen && c_isspace(errout[elen - 1])) + errout[--elen] = '\0'; } if (ret < 0) { -- 1.7.7.5

On 07/18/2012 09:23 AM, Guannan Ren wrote:
Instead of only removing the ending newline character, it is better to remove all of standard whitespace character for the sake of log format.
One example that we have to do this is: After three times incorrect password input, virsh command virsh -c qemu://remoteserver/system will report error like:
: Connection reset by peerey,gssapi-keyex,gssapi-with-mic,password).
But it should be: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). : Connection reset by peer
The reason is that we dropped the newline, but have a '\r' left. The terminal interprets it as "move the cursor back to the start of the current line", so the error string is messed up.
ACK. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 07/19/2012 12:16 AM, Eric Blake wrote:
On 07/18/2012 09:23 AM, Guannan Ren wrote:
Instead of only removing the ending newline character, it is better to remove all of standard whitespace character for the sake of log format.
One example that we have to do this is: After three times incorrect password input, virsh command virsh -c qemu://remoteserver/system will report error like:
: Connection reset by peerey,gssapi-keyex,gssapi-with-mic,password).
But it should be: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). : Connection reset by peer
The reason is that we dropped the newline, but have a '\r' left. The terminal interprets it as "move the cursor back to the start of the current line", so the error string is messed up. ACK.
Thanks and pushed.
participants (2)
-
Eric Blake
-
Guannan Ren