[libvirt] [PATCH] Introduce virStringStripIPv6Brackets

Helper function to strip the brackets from an IPv6 address. Tested by viruritest. --- src/libvirt_private.syms | 1 + src/util/virstring.c | 22 ++++++++++++++++++++++ src/util/virstring.h | 2 ++ src/util/viruri.c | 20 ++++---------------- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d6265ac..c5397dd 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2004,6 +2004,7 @@ virStringSortCompare; virStringSortRevCompare; virStringSplit; virStringSplitCount; +virStringStripIPv6Brackets; virStrncpy; virStrndup; virStrToDouble; diff --git a/src/util/virstring.c b/src/util/virstring.c index 54c0b6f..0cb7f3f 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -929,3 +929,25 @@ virStringReplace(const char *haystack, return virBufferContentAndReset(&buf); } + +/** + * virStringStripIPv6Brackets: + * @str: the string to strip + * + * Modify the string in-place to remove the leading and closing brackets + * from an IPv6 address. + */ +void +virStringStripIPv6Brackets(char *str) +{ + size_t len; + + if (!str) + return; + + len = strlen(str); + if (str[0] == '[' && str[len-1] == ']' && strchr(str, ':')) { + memmove(&str[0], &str[1], len - 2); + str[len - 2] = '\0'; + } +} diff --git a/src/util/virstring.h b/src/util/virstring.h index b82ef2a..40ebaeb 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -268,4 +268,6 @@ char *virStringReplace(const char *haystack, const char *newneedle) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); +void virStringStripIPv6Brackets(char *str); + #endif /* __VIR_STRING_H__ */ diff --git a/src/util/viruri.c b/src/util/viruri.c index 23d86c5..6166c37 100644 --- a/src/util/viruri.c +++ b/src/util/viruri.c @@ -182,22 +182,10 @@ virURIParse(const char *uri) if (VIR_STRDUP(ret->user, xmluri->user) < 0) goto error; - /* First check: does it even make sense to jump inside */ - if (ret->server != NULL && - ret->server[0] == '[') { - size_t length = strlen(ret->server); - - /* We want to modify the server string only if there are - * square brackets on both ends and inside there is IPv6 - * address. Otherwise we could make a mistake by modifying - * something other than an IPv6 address. */ - if (ret->server[length - 1] == ']' && strchr(ret->server, ':')) { - memmove(&ret->server[0], &ret->server[1], length - 2); - ret->server[length - 2] = '\0'; - } - /* Even after such modification, it is completely ok to free - * the uri with xmlFreeURI() */ - } + /* Strip square bracket from an IPv6 address. + * The function modifies the string in-place. Even after such + * modification, it is OK to free the URI with xmlFreeURI. */ + virStringStripIPv6Brackets(ret->server); if (virURIParseParams(ret) < 0) goto error; -- 2.0.4

-----Original Message----- From: libvir-list-bounces@redhat.com [mailto:libvir-list-bounces@redhat.com] On Behalf Of Ján Tomko Sent: Wednesday, October 08, 2014 4:47 PM To: libvir-list@redhat.com Subject: [libvirt] [PATCH] Introduce virStringStripIPv6Brackets
Helper function to strip the brackets from an IPv6 address. Tested by viruritest.
Reviewed-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>

On Wed, Oct 08, 2014 at 10:46:53AM +0200, Ján Tomko wrote:
Helper function to strip the brackets from an IPv6 address. Tested by viruritest. --- src/libvirt_private.syms | 1 + src/util/virstring.c | 22 ++++++++++++++++++++++ src/util/virstring.h | 2 ++ src/util/viruri.c | 20 ++++---------------- 4 files changed, 29 insertions(+), 16 deletions(-)
Can we have tests in viruritest.c and virstringtest.c too Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On 10/08/2014 11:10 AM, Daniel P. Berrange wrote:
On Wed, Oct 08, 2014 at 10:46:53AM +0200, Ján Tomko wrote:
Helper function to strip the brackets from an IPv6 address. Tested by viruritest. --- src/libvirt_private.syms | 1 + src/util/virstring.c | 22 ++++++++++++++++++++++ src/util/virstring.h | 2 ++ src/util/viruri.c | 20 ++++---------------- 4 files changed, 29 insertions(+), 16 deletions(-)
Can we have tests in viruritest.c and virstringtest.c too
We already test it on sane inputs in viruritest. I've added some corner cases to virstringtest in v2.
Regards, Daniel
participants (3)
-
Chen, Hanxiao
-
Daniel P. Berrange
-
Ján Tomko