2012-11-30 15:21:02 +00:00
|
|
|
/*
|
2015-04-23 14:13:53 +00:00
|
|
|
* Copyright (C) 2012-2015 Red Hat, Inc.
|
2012-11-30 15:21:02 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-04-03 10:36:23 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2014-01-23 09:28:29 +00:00
|
|
|
#include <regex.h>
|
2017-06-22 11:34:38 +00:00
|
|
|
#include <locale.h>
|
2013-04-03 10:36:23 +00:00
|
|
|
|
2016-05-13 11:15:15 +00:00
|
|
|
#include "base64.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "c-ctype.h"
|
2012-11-30 15:21:02 +00:00
|
|
|
#include "virstring.h"
|
2017-06-21 17:08:28 +00:00
|
|
|
#include "virthread.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-04 12:04:07 +00:00
|
|
|
#include "virbuffer.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2014-01-23 09:28:29 +00:00
|
|
|
#include "virlog.h"
|
2012-11-30 15:21:02 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.string");
|
|
|
|
|
2012-11-30 15:21:02 +00:00
|
|
|
/*
|
2016-11-25 08:18:35 +00:00
|
|
|
* The following virStringSplit & virStringListJoin methods
|
2012-11-30 15:21:02 +00:00
|
|
|
* are derived from g_strsplit / g_strjoin in glib2,
|
|
|
|
* also available under the LGPLv2+ license terms
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-05-12 07:46:37 +00:00
|
|
|
* virStringSplitCount:
|
2012-11-30 15:21:02 +00:00
|
|
|
* @string: a string to split
|
|
|
|
* @delim: a string which specifies the places at which to split
|
|
|
|
* the string. The delimiter is not included in any of the resulting
|
|
|
|
* strings, unless @max_tokens is reached.
|
|
|
|
* @max_tokens: the maximum number of pieces to split @string into.
|
|
|
|
* If this is 0, the string is split completely.
|
2014-05-12 07:46:37 +00:00
|
|
|
* @tokcount: If provided, the value is set to the count of pieces the string
|
|
|
|
* was split to excluding the terminating NULL element.
|
2012-11-30 15:21:02 +00:00
|
|
|
*
|
|
|
|
* Splits a string into a maximum of @max_tokens pieces, using the given
|
|
|
|
* @delim. If @max_tokens is reached, the remainder of @string is
|
|
|
|
* appended to the last token.
|
|
|
|
*
|
|
|
|
* As a special case, the result of splitting the empty string "" is an empty
|
|
|
|
* vector, not a vector containing a single string. The reason for this
|
2016-12-01 20:50:08 +00:00
|
|
|
* special case is that being able to represent an empty vector is typically
|
2012-11-30 15:21:02 +00:00
|
|
|
* more useful than consistent handling of empty elements. If you do need
|
|
|
|
* to represent empty elements, you'll need to check for the empty string
|
|
|
|
* before calling virStringSplit().
|
|
|
|
*
|
|
|
|
* Return value: a newly-allocated NULL-terminated array of strings. Use
|
2016-11-25 08:18:35 +00:00
|
|
|
* virStringListFree() to free it.
|
2012-11-30 15:21:02 +00:00
|
|
|
*/
|
2014-05-12 07:46:37 +00:00
|
|
|
char **
|
|
|
|
virStringSplitCount(const char *string,
|
|
|
|
const char *delim,
|
|
|
|
size_t max_tokens,
|
|
|
|
size_t *tokcount)
|
2012-11-30 15:21:02 +00:00
|
|
|
{
|
|
|
|
char **tokens = NULL;
|
|
|
|
size_t ntokens = 0;
|
|
|
|
size_t maxtokens = 0;
|
|
|
|
const char *remainder = string;
|
|
|
|
char *tmp;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (max_tokens == 0)
|
|
|
|
max_tokens = INT_MAX;
|
|
|
|
|
|
|
|
tmp = strstr(remainder, delim);
|
|
|
|
if (tmp) {
|
|
|
|
size_t delimlen = strlen(delim);
|
|
|
|
|
|
|
|
while (--max_tokens && tmp) {
|
|
|
|
size_t len = tmp - remainder;
|
|
|
|
|
|
|
|
if (VIR_RESIZE_N(tokens, maxtokens, ntokens, 1) < 0)
|
2013-07-04 10:17:18 +00:00
|
|
|
goto error;
|
2012-11-30 15:21:02 +00:00
|
|
|
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRNDUP(tokens[ntokens], remainder, len) < 0)
|
|
|
|
goto error;
|
2012-11-30 15:21:02 +00:00
|
|
|
ntokens++;
|
|
|
|
remainder = tmp + delimlen;
|
|
|
|
tmp = strstr(remainder, delim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*string) {
|
|
|
|
if (VIR_RESIZE_N(tokens, maxtokens, ntokens, 1) < 0)
|
2013-07-04 10:17:18 +00:00
|
|
|
goto error;
|
2012-11-30 15:21:02 +00:00
|
|
|
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(tokens[ntokens], remainder) < 0)
|
|
|
|
goto error;
|
2012-11-30 15:21:02 +00:00
|
|
|
ntokens++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_RESIZE_N(tokens, maxtokens, ntokens, 1) < 0)
|
2013-07-04 10:17:18 +00:00
|
|
|
goto error;
|
2012-11-30 15:21:02 +00:00
|
|
|
tokens[ntokens++] = NULL;
|
|
|
|
|
2014-05-12 07:46:37 +00:00
|
|
|
if (tokcount)
|
|
|
|
*tokcount = ntokens - 1;
|
|
|
|
|
2012-11-30 15:21:02 +00:00
|
|
|
return tokens;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2013-05-21 07:58:16 +00:00
|
|
|
for (i = 0; i < ntokens; i++)
|
2012-11-30 15:21:02 +00:00
|
|
|
VIR_FREE(tokens[i]);
|
|
|
|
VIR_FREE(tokens);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-12 07:46:37 +00:00
|
|
|
char **
|
|
|
|
virStringSplit(const char *string,
|
|
|
|
const char *delim,
|
|
|
|
size_t max_tokens)
|
|
|
|
{
|
|
|
|
return virStringSplitCount(string, delim, max_tokens, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-30 15:21:02 +00:00
|
|
|
/**
|
2016-11-25 08:18:35 +00:00
|
|
|
* virStringListJoin:
|
2012-11-30 15:21:02 +00:00
|
|
|
* @strings: a NULL-terminated array of strings to join
|
|
|
|
* @delim: a string to insert between each of the strings
|
|
|
|
*
|
|
|
|
* Joins a number of strings together to form one long string, with the
|
|
|
|
* @delim inserted between each of them. The returned string
|
|
|
|
* should be freed with VIR_FREE().
|
|
|
|
*
|
|
|
|
* Returns: a newly-allocated string containing all of the strings joined
|
|
|
|
* together, with @delim between them
|
|
|
|
*/
|
2016-11-25 08:18:35 +00:00
|
|
|
char *virStringListJoin(const char **strings,
|
|
|
|
const char *delim)
|
2012-11-30 15:21:02 +00:00
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
while (*strings) {
|
|
|
|
virBufferAdd(&buf, *strings, -1);
|
|
|
|
if (*(strings+1))
|
|
|
|
virBufferAdd(&buf, delim, -1);
|
|
|
|
strings++;
|
|
|
|
}
|
2014-06-27 08:40:15 +00:00
|
|
|
if (virBufferCheckError(&buf) < 0)
|
2012-11-30 15:21:02 +00:00
|
|
|
return NULL;
|
|
|
|
ret = virBufferContentAndReset(&buf);
|
2013-05-24 07:19:51 +00:00
|
|
|
if (!ret)
|
|
|
|
ignore_value(VIR_STRDUP(ret, ""));
|
2012-11-30 15:21:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-25 14:43:53 +00:00
|
|
|
/**
|
|
|
|
* virStringListAdd:
|
|
|
|
* @strings: a NULL-terminated array of strings
|
|
|
|
* @item: string to add
|
|
|
|
*
|
2018-07-26 14:10:10 +00:00
|
|
|
* Appends @item into string list @strings. If *@strings is not
|
|
|
|
* allocated yet new string list is created.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success,
|
|
|
|
* -1 otherwise
|
2016-11-25 14:43:53 +00:00
|
|
|
*/
|
2018-07-26 14:10:10 +00:00
|
|
|
int
|
|
|
|
virStringListAdd(char ***strings,
|
2016-11-25 14:43:53 +00:00
|
|
|
const char *item)
|
|
|
|
{
|
2018-07-26 14:10:10 +00:00
|
|
|
size_t i = virStringListLength((const char **) *strings);
|
2016-11-25 14:43:53 +00:00
|
|
|
|
2018-07-26 14:10:10 +00:00
|
|
|
if (VIR_EXPAND_N(*strings, i, 2) < 0 ||
|
|
|
|
VIR_STRDUP((*strings)[i - 2], item) < 0)
|
|
|
|
return -1;
|
2016-11-25 14:43:53 +00:00
|
|
|
|
2018-07-26 14:10:10 +00:00
|
|
|
return 0;
|
2016-11-25 14:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-28 13:38:58 +00:00
|
|
|
/**
|
|
|
|
* virStringListRemove:
|
|
|
|
* @strings: a NULL-terminated array of strings
|
|
|
|
* @item: string to remove
|
|
|
|
*
|
|
|
|
* Remove every occurrence of @item in list of @strings.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virStringListRemove(char ***strings,
|
|
|
|
const char *item)
|
|
|
|
{
|
|
|
|
size_t r, w = 0;
|
|
|
|
|
|
|
|
if (!strings || !*strings)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (r = 0; (*strings)[r]; r++) {
|
|
|
|
if (STREQ((*strings)[r], item)) {
|
|
|
|
VIR_FREE((*strings)[r]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r != w)
|
|
|
|
(*strings)[w] = (*strings)[r];
|
|
|
|
w++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w == 0) {
|
|
|
|
VIR_FREE(*strings);
|
|
|
|
} else {
|
|
|
|
(*strings)[w] = NULL;
|
|
|
|
ignore_value(VIR_REALLOC_N(*strings, w + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-25 08:29:54 +00:00
|
|
|
/**
|
|
|
|
* virStringListMerge:
|
|
|
|
* @dst: a NULL-terminated array of strings to expand
|
|
|
|
* @src: a NULL-terminated array of strings
|
|
|
|
*
|
|
|
|
* Merges @src into @dst. Upon successful return from this
|
|
|
|
* function, @dst is resized to $(dst + src) elements and @src is
|
|
|
|
* freed.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virStringListMerge(char ***dst,
|
|
|
|
char ***src)
|
|
|
|
{
|
|
|
|
size_t dst_len, src_len, i;
|
|
|
|
|
|
|
|
if (!src || !*src)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dst_len = virStringListLength((const char **) *dst);
|
|
|
|
src_len = virStringListLength((const char **) *src);
|
|
|
|
|
|
|
|
if (VIR_REALLOC_N(*dst, dst_len + src_len + 1) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i <= src_len; i++)
|
|
|
|
(*dst)[i + dst_len] = (*src)[i];
|
|
|
|
|
|
|
|
/* Don't call virStringListFree() as it would free strings in
|
|
|
|
* @src. */
|
|
|
|
VIR_FREE(*src);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-20 08:41:35 +00:00
|
|
|
/**
|
|
|
|
* virStringListCopy:
|
|
|
|
* @dst: where to store the copy of @strings
|
|
|
|
* @src: a NULL-terminated array of strings
|
|
|
|
*
|
|
|
|
* Makes a deep copy of the @src string list and stores it in @dst. Callers
|
|
|
|
* are responsible for freeing @dst.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virStringListCopy(char ***dst,
|
|
|
|
const char **src)
|
|
|
|
{
|
|
|
|
char **copy = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
*dst = NULL;
|
|
|
|
|
|
|
|
if (!src)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(copy, virStringListLength(src) + 1) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
for (i = 0; src[i]; i++) {
|
|
|
|
if (VIR_STRDUP(copy[i], src[i]) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = copy;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virStringListFree(copy);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-30 15:21:02 +00:00
|
|
|
/**
|
2016-11-25 08:18:35 +00:00
|
|
|
* virStringListFree:
|
2018-01-03 09:56:35 +00:00
|
|
|
* @strings: a NULL-terminated array of strings to free
|
2012-11-30 15:21:02 +00:00
|
|
|
*
|
|
|
|
* Frees a NULL-terminated array of strings, and the array itself.
|
2016-11-25 08:18:35 +00:00
|
|
|
* If called on a NULL value, virStringListFree() simply returns.
|
2012-11-30 15:21:02 +00:00
|
|
|
*/
|
2016-11-25 08:18:35 +00:00
|
|
|
void virStringListFree(char **strings)
|
2012-11-30 15:21:02 +00:00
|
|
|
{
|
|
|
|
char **tmp = strings;
|
|
|
|
while (tmp && *tmp) {
|
|
|
|
VIR_FREE(*tmp);
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
VIR_FREE(strings);
|
|
|
|
}
|
2013-04-12 20:55:45 +00:00
|
|
|
|
|
|
|
|
2014-05-19 12:20:09 +00:00
|
|
|
/**
|
2016-11-25 08:18:35 +00:00
|
|
|
* virStringListFreeCount:
|
2014-05-19 12:20:09 +00:00
|
|
|
* @strings: array of strings to free
|
|
|
|
* @count: number of elements in the array
|
|
|
|
*
|
|
|
|
* Frees a string array of @count length.
|
|
|
|
*/
|
|
|
|
void
|
2016-11-25 08:18:35 +00:00
|
|
|
virStringListFreeCount(char **strings,
|
2014-05-19 12:20:09 +00:00
|
|
|
size_t count)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2014-09-04 20:37:11 +00:00
|
|
|
if (!strings)
|
|
|
|
return;
|
|
|
|
|
2014-05-19 12:20:09 +00:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
VIR_FREE(strings[i]);
|
|
|
|
|
|
|
|
VIR_FREE(strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-12 20:55:45 +00:00
|
|
|
bool
|
2016-11-25 08:18:35 +00:00
|
|
|
virStringListHasString(const char **strings,
|
|
|
|
const char *needle)
|
2013-04-12 20:55:45 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
2013-07-19 12:55:41 +00:00
|
|
|
if (!strings)
|
|
|
|
return false;
|
|
|
|
|
2013-04-12 20:55:45 +00:00
|
|
|
while (strings[i]) {
|
|
|
|
if (STREQ(strings[i++], needle))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-03 10:36:23 +00:00
|
|
|
|
2015-08-03 12:03:23 +00:00
|
|
|
char *
|
2016-11-25 08:18:35 +00:00
|
|
|
virStringListGetFirstWithPrefix(char **strings,
|
|
|
|
const char *prefix)
|
2015-08-03 12:03:23 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if (!strings)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (strings[i]) {
|
|
|
|
if (STRPREFIX(strings[i], prefix))
|
|
|
|
return strings[i] + strlen(prefix);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-03 10:36:23 +00:00
|
|
|
/* Like strtol, but produce an "int" result, and check more carefully.
|
|
|
|
Return 0 upon success; return -1 to indicate failure.
|
|
|
|
When END_PTR is NULL, the byte after the final valid digit must be NUL.
|
|
|
|
Otherwise, it's like strtol and lets the caller check any suffix for
|
|
|
|
validity. This function is careful to return -1 when the string S
|
|
|
|
represents a number that is not representable as an "int". */
|
|
|
|
int
|
|
|
|
virStrToLong_i(char const *s, char **end_ptr, int base, int *result)
|
|
|
|
{
|
|
|
|
long int val;
|
|
|
|
char *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(s, &p, base); /* exempt from syntax-check */
|
|
|
|
err = (errno || (!end_ptr && *p) || p == s || (int) val != val);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-01 02:11:09 +00:00
|
|
|
/* Just like virStrToLong_i, above, but produce an "unsigned int"
|
|
|
|
* value. This version allows twos-complement wraparound of negative
|
|
|
|
* numbers. */
|
2013-04-03 10:36:23 +00:00
|
|
|
int
|
|
|
|
virStrToLong_ui(char const *s, char **end_ptr, int base, unsigned int *result)
|
|
|
|
{
|
|
|
|
unsigned long int val;
|
|
|
|
char *p;
|
util: fix uint parsing on 64-bit platforms
Commit f22b7899 called to light a long-standing latent bug: the
behavior of virStrToLong_ui was different on 32-bit platforms
than on 64-bit platforms. Curse you, C type promotion and
narrowing rules, and strtoul specification. POSIX says that for
a 32-bit long, strtol handles only 2^32 values [LONG_MIN to
LONG_MAX] while strtoul handles 2^33 - 1 values [-ULONG_MAX to
ULONG_MAX] with twos-complement wraparound for negatives. Thus,
parsing -1 as unsigned long produces ULONG_MAX, rather than a
range error. We WANT[1] this same shortcut for turning -1 into
UINT_MAX when parsing to int; and get it for free with 32-bit
long. But with 64-bit long, ULONG_MAX is outside the range
of int and we were rejecting it as invalid; meanwhile, we were
silently treating -18446744073709551615 as 1 even though it
textually exceeds INT_MIN. Too bad there's not a strtoui() in
libc that does guaranteed parsing to int, regardless of the size
of long.
The bug has been latent since 2007, introduced by Jim Meyering
in commit 5d25419 in the attempt to eradicate unsafe use of
strto[u]l when parsing ints and longs. How embarrassing that we
are only discovering it now - so I'm adding a testsuite to ensure
that it covers all the corner cases we care about.
[1] Ideally, we really want the caller to be able to choose whether
to allow negative numbers to wrap around to their 2s-complement
counterpart, as in strtoul, or to force a stricter input range
of [0 to UINT_MAX] by rejecting negative signs; this will be added
in a later patch for all three int types.
This patch is tested on both 32- and 64-bit; the enhanced
virstringtest passes on both platforms, while virstoragetest now
reliably fails on both platforms instead of just 32-bit platforms.
That test will be fixed later.
* src/util/virstring.c (virStrToLong_ui): Ensure same behavior
regardless of platform long size.
* tests/virstringtest.c (testStringToLong): New function.
(mymain): Comprehensively test string to long parsing.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-04-30 20:46:18 +00:00
|
|
|
bool err = false;
|
2013-04-03 10:36:23 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoul(s, &p, base); /* exempt from syntax-check */
|
util: fix uint parsing on 64-bit platforms
Commit f22b7899 called to light a long-standing latent bug: the
behavior of virStrToLong_ui was different on 32-bit platforms
than on 64-bit platforms. Curse you, C type promotion and
narrowing rules, and strtoul specification. POSIX says that for
a 32-bit long, strtol handles only 2^32 values [LONG_MIN to
LONG_MAX] while strtoul handles 2^33 - 1 values [-ULONG_MAX to
ULONG_MAX] with twos-complement wraparound for negatives. Thus,
parsing -1 as unsigned long produces ULONG_MAX, rather than a
range error. We WANT[1] this same shortcut for turning -1 into
UINT_MAX when parsing to int; and get it for free with 32-bit
long. But with 64-bit long, ULONG_MAX is outside the range
of int and we were rejecting it as invalid; meanwhile, we were
silently treating -18446744073709551615 as 1 even though it
textually exceeds INT_MIN. Too bad there's not a strtoui() in
libc that does guaranteed parsing to int, regardless of the size
of long.
The bug has been latent since 2007, introduced by Jim Meyering
in commit 5d25419 in the attempt to eradicate unsafe use of
strto[u]l when parsing ints and longs. How embarrassing that we
are only discovering it now - so I'm adding a testsuite to ensure
that it covers all the corner cases we care about.
[1] Ideally, we really want the caller to be able to choose whether
to allow negative numbers to wrap around to their 2s-complement
counterpart, as in strtoul, or to force a stricter input range
of [0 to UINT_MAX] by rejecting negative signs; this will be added
in a later patch for all three int types.
This patch is tested on both 32- and 64-bit; the enhanced
virstringtest passes on both platforms, while virstoragetest now
reliably fails on both platforms instead of just 32-bit platforms.
That test will be fixed later.
* src/util/virstring.c (virStrToLong_ui): Ensure same behavior
regardless of platform long size.
* tests/virstringtest.c (testStringToLong): New function.
(mymain): Comprehensively test string to long parsing.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-04-30 20:46:18 +00:00
|
|
|
|
|
|
|
/* This one's tricky. We _want_ to allow "-1" as shorthand for
|
|
|
|
* UINT_MAX regardless of whether long is 32-bit or 64-bit. But
|
|
|
|
* strtoul treats "-1" as ULONG_MAX, and going from ulong back
|
|
|
|
* to uint differs depending on the size of long. */
|
|
|
|
if (sizeof(long) > sizeof(int) && memchr(s, '-', p - s)) {
|
|
|
|
if (-val > UINT_MAX)
|
|
|
|
err = true;
|
|
|
|
else
|
|
|
|
val &= 0xffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
err |= (errno || (!end_ptr && *p) || p == s || (unsigned int) val != val);
|
2013-04-03 10:36:23 +00:00
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-01 02:11:09 +00:00
|
|
|
/* Just like virStrToLong_i, above, but produce an "unsigned int"
|
|
|
|
* value. This version rejects any negative signs. */
|
|
|
|
int
|
|
|
|
virStrToLong_uip(char const *s, char **end_ptr, int base, unsigned int *result)
|
|
|
|
{
|
|
|
|
unsigned long int val;
|
|
|
|
char *p;
|
|
|
|
bool err = false;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoul(s, &p, base); /* exempt from syntax-check */
|
|
|
|
err = (memchr(s, '-', p - s) ||
|
|
|
|
errno || (!end_ptr && *p) || p == s || (unsigned int) val != val);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-03 10:36:23 +00:00
|
|
|
/* Just like virStrToLong_i, above, but produce a "long" value. */
|
|
|
|
int
|
|
|
|
virStrToLong_l(char const *s, char **end_ptr, int base, long *result)
|
|
|
|
{
|
|
|
|
long int val;
|
|
|
|
char *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(s, &p, base); /* exempt from syntax-check */
|
|
|
|
err = (errno || (!end_ptr && *p) || p == s);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-01 02:11:09 +00:00
|
|
|
/* Just like virStrToLong_i, above, but produce an "unsigned long"
|
|
|
|
* value. This version allows twos-complement wraparound of negative
|
|
|
|
* numbers. */
|
2013-04-03 10:36:23 +00:00
|
|
|
int
|
|
|
|
virStrToLong_ul(char const *s, char **end_ptr, int base, unsigned long *result)
|
|
|
|
{
|
|
|
|
unsigned long int val;
|
|
|
|
char *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoul(s, &p, base); /* exempt from syntax-check */
|
|
|
|
err = (errno || (!end_ptr && *p) || p == s);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-01 02:11:09 +00:00
|
|
|
/* Just like virStrToLong_i, above, but produce an "unsigned long"
|
|
|
|
* value. This version rejects any negative signs. */
|
|
|
|
int
|
|
|
|
virStrToLong_ulp(char const *s, char **end_ptr, int base,
|
|
|
|
unsigned long *result)
|
|
|
|
{
|
|
|
|
unsigned long int val;
|
|
|
|
char *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoul(s, &p, base); /* exempt from syntax-check */
|
|
|
|
err = (memchr(s, '-', p - s) ||
|
|
|
|
errno || (!end_ptr && *p) || p == s);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-03 10:36:23 +00:00
|
|
|
/* Just like virStrToLong_i, above, but produce a "long long" value. */
|
|
|
|
int
|
|
|
|
virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result)
|
|
|
|
{
|
|
|
|
long long val;
|
|
|
|
char *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoll(s, &p, base); /* exempt from syntax-check */
|
|
|
|
err = (errno || (!end_ptr && *p) || p == s);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-01 02:11:09 +00:00
|
|
|
/* Just like virStrToLong_i, above, but produce an "unsigned long
|
|
|
|
* long" value. This version allows twos-complement wraparound of
|
|
|
|
* negative numbers. */
|
2013-04-03 10:36:23 +00:00
|
|
|
int
|
2014-05-01 02:11:09 +00:00
|
|
|
virStrToLong_ull(char const *s, char **end_ptr, int base,
|
|
|
|
unsigned long long *result)
|
2013-04-03 10:36:23 +00:00
|
|
|
{
|
|
|
|
unsigned long long val;
|
|
|
|
char *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoull(s, &p, base); /* exempt from syntax-check */
|
|
|
|
err = (errno || (!end_ptr && *p) || p == s);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-01 02:11:09 +00:00
|
|
|
/* Just like virStrToLong_i, above, but produce an "unsigned long
|
|
|
|
* long" value. This version rejects any negative signs. */
|
|
|
|
int
|
|
|
|
virStrToLong_ullp(char const *s, char **end_ptr, int base,
|
|
|
|
unsigned long long *result)
|
|
|
|
{
|
|
|
|
unsigned long long val;
|
|
|
|
char *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoull(s, &p, base); /* exempt from syntax-check */
|
|
|
|
err = (memchr(s, '-', p - s) ||
|
|
|
|
errno || (!end_ptr && *p) || p == s);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:08:28 +00:00
|
|
|
/* In case thread-safe locales are available */
|
|
|
|
#if HAVE_NEWLOCALE
|
|
|
|
|
2017-06-22 12:36:53 +00:00
|
|
|
typedef locale_t virLocale;
|
|
|
|
static virLocale virLocaleRaw;
|
2017-06-21 17:08:28 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virLocaleOnceInit(void)
|
|
|
|
{
|
2017-06-22 12:36:53 +00:00
|
|
|
virLocaleRaw = newlocale(LC_ALL_MASK, "C", (locale_t)0);
|
|
|
|
if (!virLocaleRaw)
|
2017-06-21 17:08:28 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(virLocale);
|
2017-06-22 12:36:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virLocaleSetRaw:
|
|
|
|
*
|
|
|
|
* @oldlocale: set to old locale pointer
|
|
|
|
*
|
|
|
|
* Sets the locale to 'C' to allow operating on non-localized objects.
|
|
|
|
* Returns 0 on success -1 on error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virLocaleSetRaw(virLocale *oldlocale)
|
|
|
|
{
|
|
|
|
if (virLocaleInitialize() < 0)
|
|
|
|
return -1;
|
|
|
|
*oldlocale = uselocale(virLocaleRaw);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLocaleRevert(virLocale *oldlocale)
|
|
|
|
{
|
|
|
|
uselocale(*oldlocale);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLocaleFixupRadix(char **strp ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !HAVE_NEWLOCALE */
|
|
|
|
|
|
|
|
typedef int virLocale;
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLocaleSetRaw(virLocale *oldlocale ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLocaleRevert(virLocale *oldlocale ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLocaleFixupRadix(char **strp)
|
|
|
|
{
|
|
|
|
char *radix, *tmp;
|
|
|
|
struct lconv *lc;
|
|
|
|
|
|
|
|
lc = localeconv();
|
|
|
|
radix = lc->decimal_point;
|
|
|
|
tmp = strstr(*strp, radix);
|
|
|
|
if (tmp) {
|
|
|
|
*tmp = '.';
|
|
|
|
if (strlen(radix) > 1)
|
|
|
|
memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *strp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !HAVE_NEWLOCALE */
|
|
|
|
|
2017-06-21 17:08:28 +00:00
|
|
|
|
2017-06-21 17:08:29 +00:00
|
|
|
/**
|
|
|
|
* virStrToDouble
|
|
|
|
*
|
|
|
|
* converts string with C locale (thread-safe) to double.
|
|
|
|
*
|
|
|
|
* Returns -1 on error or returns 0 on success.
|
|
|
|
*/
|
2013-04-03 10:36:23 +00:00
|
|
|
int
|
|
|
|
virStrToDouble(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
double *result)
|
|
|
|
{
|
2017-06-22 12:36:53 +00:00
|
|
|
virLocale oldlocale;
|
2013-04-03 10:36:23 +00:00
|
|
|
double val;
|
|
|
|
char *p;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
2017-06-22 12:36:53 +00:00
|
|
|
if (virLocaleSetRaw(&oldlocale) < 0)
|
2017-06-21 17:08:29 +00:00
|
|
|
return -1;
|
2013-04-03 10:36:23 +00:00
|
|
|
val = strtod(s, &p); /* exempt from syntax-check */
|
2017-06-22 12:36:53 +00:00
|
|
|
virLocaleRevert(&oldlocale);
|
|
|
|
|
2013-04-03 10:36:23 +00:00
|
|
|
err = (errno || (!end_ptr && *p) || p == s);
|
|
|
|
if (end_ptr)
|
|
|
|
*end_ptr = p;
|
|
|
|
if (err)
|
|
|
|
return -1;
|
|
|
|
*result = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:08:28 +00:00
|
|
|
/**
|
|
|
|
* virDoubleToStr
|
|
|
|
*
|
|
|
|
* converts double to string with C locale (thread-safe).
|
|
|
|
*
|
|
|
|
* Returns -1 on error, size of the string otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virDoubleToStr(char **strp, double number)
|
|
|
|
{
|
2017-06-22 12:36:53 +00:00
|
|
|
virLocale oldlocale;
|
2017-06-21 17:08:28 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2017-06-22 12:36:53 +00:00
|
|
|
if (virLocaleSetRaw(&oldlocale) < 0)
|
|
|
|
return -1;
|
2017-06-21 17:08:28 +00:00
|
|
|
|
|
|
|
ret = virAsprintf(strp, "%lf", number);
|
|
|
|
|
2017-06-22 12:36:53 +00:00
|
|
|
virLocaleRevert(&oldlocale);
|
|
|
|
virLocaleFixupRadix(strp);
|
2017-06-21 17:08:28 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-03 10:36:23 +00:00
|
|
|
int
|
2013-06-07 15:10:28 +00:00
|
|
|
virVasprintfInternal(bool report,
|
|
|
|
int domcode,
|
|
|
|
const char *filename,
|
|
|
|
const char *funcname,
|
|
|
|
size_t linenr,
|
|
|
|
char **strp,
|
|
|
|
const char *fmt,
|
|
|
|
va_list list)
|
2013-04-03 10:36:23 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-06-07 15:10:28 +00:00
|
|
|
if ((ret = vasprintf(strp, fmt, list)) == -1) {
|
|
|
|
if (report)
|
|
|
|
virReportOOMErrorFull(domcode, filename, funcname, linenr);
|
2013-04-03 10:36:23 +00:00
|
|
|
*strp = NULL;
|
2013-06-07 15:10:28 +00:00
|
|
|
}
|
2013-04-03 10:36:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-06-07 15:10:28 +00:00
|
|
|
virAsprintfInternal(bool report,
|
|
|
|
int domcode,
|
|
|
|
const char *filename,
|
|
|
|
const char *funcname,
|
|
|
|
size_t linenr,
|
|
|
|
char **strp,
|
|
|
|
const char *fmt, ...)
|
2013-04-03 10:36:23 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2013-06-07 15:10:28 +00:00
|
|
|
ret = virVasprintfInternal(report, domcode, filename,
|
|
|
|
funcname, linenr, strp, fmt, ap);
|
2013-04-03 10:36:23 +00:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-20 11:00:44 +00:00
|
|
|
* virStrncpy:
|
|
|
|
*
|
|
|
|
* @dest: destination buffer
|
|
|
|
* @src: source buffer
|
|
|
|
* @n: number of bytes to copy
|
|
|
|
* @destbytes: number of bytes the destination can accomodate
|
|
|
|
*
|
|
|
|
* Copies the first @n bytes of @src to @dest.
|
|
|
|
*
|
|
|
|
* @src must be NULL-terminated; if successful, @dest is guaranteed to
|
|
|
|
* be NULL-terminated as well.
|
|
|
|
*
|
|
|
|
* @n must be a reasonable value, that is, it must not exceed either
|
|
|
|
* the length of @src or the size of @dest. For the latter constraint,
|
|
|
|
* the fact that @dest needs to accomodate a NULL byte in addition to
|
|
|
|
* the bytes copied from @src must be taken into account.
|
|
|
|
*
|
|
|
|
* If you want to copy *all* of @src to @dest, use virStrcpy() or
|
|
|
|
* virStrcpyStatic() instead.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, <0 on failure.
|
2013-04-03 10:36:23 +00:00
|
|
|
*/
|
2018-07-20 07:50:37 +00:00
|
|
|
int
|
2013-04-03 10:36:23 +00:00
|
|
|
virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
|
|
|
|
{
|
2018-07-20 11:00:44 +00:00
|
|
|
size_t src_len = strlen(src);
|
|
|
|
|
|
|
|
/* As a special case, -1 means "copy the entire string".
|
|
|
|
*
|
|
|
|
* This is to avoid calling strlen() twice, once in the virStrcpy()
|
|
|
|
* wrapper and once here for bound checking purposes. */
|
|
|
|
if (n == -1)
|
|
|
|
n = src_len;
|
|
|
|
|
|
|
|
if (n <= 0 || n > src_len || n > (destbytes - 1))
|
2018-07-20 07:50:37 +00:00
|
|
|
return -1;
|
2013-04-03 10:36:23 +00:00
|
|
|
|
2018-07-20 11:00:44 +00:00
|
|
|
memcpy(dest, src, n);
|
2013-04-03 10:36:23 +00:00
|
|
|
dest[n] = '\0';
|
|
|
|
|
2018-07-20 07:50:37 +00:00
|
|
|
return 0;
|
2013-04-03 10:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-20 11:00:44 +00:00
|
|
|
* virStrcpy:
|
|
|
|
*
|
|
|
|
* @dest: destination buffer
|
|
|
|
* @src: source buffer
|
|
|
|
* @destbytes: number of bytes the destination can accomodate
|
|
|
|
*
|
|
|
|
* Copies @src to @dest.
|
|
|
|
*
|
|
|
|
* See virStrncpy() for more information.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success, <0 on failure.
|
2013-04-03 10:36:23 +00:00
|
|
|
*/
|
2018-07-20 07:50:37 +00:00
|
|
|
int
|
2013-04-03 10:36:23 +00:00
|
|
|
virStrcpy(char *dest, const char *src, size_t destbytes)
|
|
|
|
{
|
2018-07-20 11:00:44 +00:00
|
|
|
return virStrncpy(dest, src, -1, destbytes);
|
2013-04-03 10:36:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSkipSpaces:
|
|
|
|
* @str: pointer to the char pointer used
|
|
|
|
*
|
|
|
|
* Skip potential blanks, this includes space tabs, line feed,
|
|
|
|
* carriage returns.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virSkipSpaces(const char **str)
|
|
|
|
{
|
|
|
|
const char *cur = *str;
|
|
|
|
|
|
|
|
while (c_isspace(*cur))
|
|
|
|
cur++;
|
|
|
|
*str = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSkipSpacesAndBackslash:
|
|
|
|
* @str: pointer to the char pointer used
|
|
|
|
*
|
|
|
|
* Like virSkipSpaces, but also skip backslashes erroneously emitted
|
|
|
|
* by xend
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virSkipSpacesAndBackslash(const char **str)
|
|
|
|
{
|
|
|
|
const char *cur = *str;
|
|
|
|
|
|
|
|
while (c_isspace(*cur) || *cur == '\\')
|
|
|
|
cur++;
|
|
|
|
*str = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virTrimSpaces:
|
|
|
|
* @str: string to modify to remove all trailing spaces
|
|
|
|
* @endp: track the end of the string
|
|
|
|
*
|
|
|
|
* If @endp is NULL on entry, then all spaces prior to the trailing
|
|
|
|
* NUL in @str are removed, by writing NUL into the appropriate
|
|
|
|
* location. If @endp is non-NULL but points to a NULL pointer,
|
|
|
|
* then all spaces prior to the trailing NUL in @str are removed,
|
|
|
|
* NUL is written to the new string end, and endp is set to the
|
|
|
|
* location of the (new) string end. If @endp is non-NULL and
|
|
|
|
* points to a non-NULL pointer, then that pointer is used as
|
|
|
|
* the end of the string, endp is set to the (new) location, but
|
|
|
|
* no NUL pointer is written into the string.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virTrimSpaces(char *str, char **endp)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
if (!endp || !*endp)
|
|
|
|
end = str + strlen(str);
|
|
|
|
else
|
|
|
|
end = *endp;
|
|
|
|
while (end > str && c_isspace(end[-1]))
|
|
|
|
end--;
|
|
|
|
if (endp) {
|
|
|
|
if (!*endp)
|
|
|
|
*end = '\0';
|
|
|
|
*endp = end;
|
|
|
|
} else {
|
|
|
|
*end = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virSkipSpacesBackwards:
|
|
|
|
* @str: start of string
|
|
|
|
* @endp: on entry, *endp must be NULL or a location within @str, on exit,
|
|
|
|
* will be adjusted to skip trailing spaces, or to NULL if @str had nothing
|
|
|
|
* but spaces.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virSkipSpacesBackwards(const char *str, char **endp)
|
|
|
|
{
|
|
|
|
/* Casting away const is safe, since virTrimSpaces does not
|
|
|
|
* modify string with this particular usage. */
|
|
|
|
char *s = (char*) str;
|
|
|
|
|
|
|
|
if (!*endp)
|
|
|
|
*endp = s + strlen(s);
|
|
|
|
virTrimSpaces(s, endp);
|
|
|
|
if (s == *endp)
|
|
|
|
*endp = NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-22 08:26:42 +00:00
|
|
|
/**
|
|
|
|
* virStringIsEmpty:
|
|
|
|
* @str: string to check
|
|
|
|
*
|
|
|
|
* Returns true if string is empty (may contain only whitespace) or NULL.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
virStringIsEmpty(const char *str)
|
|
|
|
{
|
|
|
|
if (!str)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
virSkipSpaces(&str);
|
|
|
|
return str[0] == '\0';
|
|
|
|
}
|
|
|
|
|
2013-04-03 12:51:20 +00:00
|
|
|
/**
|
|
|
|
* virStrdup:
|
|
|
|
* @dest: where to store duplicated string
|
|
|
|
* @src: the source string to duplicate
|
|
|
|
* @report: whether to report OOM error, if there is one
|
|
|
|
* @domcode: error domain code
|
|
|
|
* @filename: caller's filename
|
|
|
|
* @funcname: caller's funcname
|
|
|
|
* @linenr: caller's line number
|
|
|
|
*
|
|
|
|
* Wrapper over strdup, which reports OOM error if told so,
|
|
|
|
* in which case callers wants to pass @domcode, @filename,
|
|
|
|
* @funcname and @linenr which should represent location in
|
|
|
|
* caller's body where virStrdup is called from. Consider
|
|
|
|
* using VIR_STRDUP which sets these automatically.
|
|
|
|
*
|
2013-05-07 02:42:44 +00:00
|
|
|
* Returns: 0 for NULL src, 1 on successful copy, -1 otherwise.
|
2013-04-03 12:51:20 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virStrdup(char **dest,
|
|
|
|
const char *src,
|
|
|
|
bool report,
|
|
|
|
int domcode,
|
|
|
|
const char *filename,
|
|
|
|
const char *funcname,
|
|
|
|
size_t linenr)
|
|
|
|
{
|
2013-05-28 07:13:17 +00:00
|
|
|
*dest = NULL;
|
2013-05-07 02:42:44 +00:00
|
|
|
if (!src)
|
|
|
|
return 0;
|
2013-04-03 12:51:20 +00:00
|
|
|
if (!(*dest = strdup(src))) {
|
|
|
|
if (report)
|
|
|
|
virReportOOMErrorFull(domcode, filename, funcname, linenr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-07 02:42:44 +00:00
|
|
|
return 1;
|
2013-04-03 12:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStrndup:
|
|
|
|
* @dest: where to store duplicated string
|
|
|
|
* @src: the source string to duplicate
|
|
|
|
* @n: how many bytes to copy
|
|
|
|
* @report: whether to report OOM error, if there is one
|
|
|
|
* @domcode: error domain code
|
|
|
|
* @filename: caller's filename
|
|
|
|
* @funcname: caller's funcname
|
|
|
|
* @linenr: caller's line number
|
|
|
|
*
|
|
|
|
* Wrapper over strndup, which reports OOM error if told so,
|
|
|
|
* in which case callers wants to pass @domcode, @filename,
|
|
|
|
* @funcname and @linenr which should represent location in
|
|
|
|
* caller's body where virStrndup is called from. Consider
|
|
|
|
* using VIR_STRNDUP which sets these automatically.
|
|
|
|
*
|
2013-05-24 08:45:57 +00:00
|
|
|
* In case @n is smaller than zero, the whole @src string is
|
|
|
|
* copied.
|
|
|
|
*
|
2013-05-07 02:42:44 +00:00
|
|
|
* Returns: 0 for NULL src, 1 on successful copy, -1 otherwise.
|
2013-04-03 12:51:20 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virStrndup(char **dest,
|
|
|
|
const char *src,
|
2013-05-24 08:45:57 +00:00
|
|
|
ssize_t n,
|
2013-04-03 12:51:20 +00:00
|
|
|
bool report,
|
|
|
|
int domcode,
|
|
|
|
const char *filename,
|
|
|
|
const char *funcname,
|
|
|
|
size_t linenr)
|
|
|
|
{
|
2013-05-28 07:13:17 +00:00
|
|
|
*dest = NULL;
|
2013-05-07 02:42:44 +00:00
|
|
|
if (!src)
|
|
|
|
return 0;
|
2013-05-24 08:45:57 +00:00
|
|
|
if (n < 0)
|
|
|
|
n = strlen(src);
|
2013-04-03 12:51:20 +00:00
|
|
|
if (!(*dest = strndup(src, n))) {
|
|
|
|
if (report)
|
|
|
|
virReportOOMErrorFull(domcode, filename, funcname, linenr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-07 02:42:44 +00:00
|
|
|
return 1;
|
2013-04-03 12:51:20 +00:00
|
|
|
}
|
2013-04-25 16:05:00 +00:00
|
|
|
|
|
|
|
|
2016-02-09 17:18:49 +00:00
|
|
|
size_t virStringListLength(const char * const *strings)
|
2013-04-25 16:05:00 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
while (strings && strings[i])
|
|
|
|
i++;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
2013-11-28 11:14:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStringSortCompare:
|
|
|
|
*
|
|
|
|
* A comparator function for sorting strings in
|
|
|
|
* normal order with qsort().
|
|
|
|
*/
|
|
|
|
int virStringSortCompare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const char **sa = (const char**)a;
|
|
|
|
const char **sb = (const char**)b;
|
|
|
|
|
|
|
|
return strcmp(*sa, *sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStringSortRevCompare:
|
|
|
|
*
|
|
|
|
* A comparator function for sorting strings in
|
|
|
|
* reverse order with qsort().
|
|
|
|
*/
|
|
|
|
int virStringSortRevCompare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const char **sa = (const char**)a;
|
|
|
|
const char **sb = (const char**)b;
|
|
|
|
|
|
|
|
return strcmp(*sb, *sa);
|
|
|
|
}
|
2014-01-23 09:28:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virStringSearch:
|
|
|
|
* @str: string to search
|
|
|
|
* @regexp: POSIX Extended regular expression pattern used for matching
|
|
|
|
* @max_matches: maximum number of substrings to return
|
2017-12-23 09:49:08 +00:00
|
|
|
* @matches: pointer to an array to be filled with NULL terminated list of matches
|
2014-01-23 09:28:29 +00:00
|
|
|
*
|
|
|
|
* Performs a POSIX extended regex search against a string and return all matching substrings.
|
2018-01-03 09:56:35 +00:00
|
|
|
* The @matches value should be freed with virStringListFree() when no longer
|
2014-01-23 09:28:29 +00:00
|
|
|
* required.
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
* char *source = "6853a496-1c10-472e-867a-8244937bd6f0
|
|
|
|
* 773ab075-4cd7-4fc2-8b6e-21c84e9cb391
|
|
|
|
* bbb3c75c-d60f-43b0-b802-fd56b84a4222
|
|
|
|
* 60c04aa1-0375-4654-8d9f-e149d9885273
|
|
|
|
* 4548d465-9891-4c34-a184-3b1c34a26aa8";
|
|
|
|
* char **matches = NULL;
|
|
|
|
* virStringSearch(source,
|
|
|
|
* "([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})",
|
|
|
|
* 3,
|
|
|
|
* &matches);
|
|
|
|
*
|
|
|
|
* // matches[0] == "6853a496-1c10-472e-867a-8244937bd6f0";
|
|
|
|
* // matches[1] == "773ab075-4cd7-4fc2-8b6e-21c84e9cb391";
|
|
|
|
* // matches[2] == "bbb3c75c-d60f-43b0-b802-fd56b84a4222"
|
|
|
|
* // matches[3] == NULL;
|
|
|
|
*
|
2016-11-25 08:18:35 +00:00
|
|
|
* virStringListFree(matches);
|
2014-01-23 09:28:29 +00:00
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* Returns: -1 on error, or number of matches
|
|
|
|
*/
|
|
|
|
ssize_t
|
|
|
|
virStringSearch(const char *str,
|
|
|
|
const char *regexp,
|
|
|
|
size_t max_matches,
|
|
|
|
char ***matches)
|
|
|
|
{
|
|
|
|
regex_t re;
|
|
|
|
regmatch_t rem;
|
|
|
|
size_t nmatches = 0;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
int rv = -1;
|
|
|
|
|
|
|
|
*matches = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("search '%s' for '%s'", str, regexp);
|
|
|
|
|
|
|
|
if ((rv = regcomp(&re, regexp, REG_EXTENDED)) != 0) {
|
|
|
|
char error[100];
|
|
|
|
regerror(rv, &re, error, sizeof(error));
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Error while compiling regular expression '%s': %s"),
|
|
|
|
regexp, error);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (re.re_nsub != 1) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Regular expression '%s' must have exactly 1 match group, not %zu"),
|
|
|
|
regexp, re.re_nsub);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* '*matches' must always be NULL terminated in every iteration
|
|
|
|
* of the loop, so start by allocating 1 element
|
|
|
|
*/
|
|
|
|
if (VIR_EXPAND_N(*matches, nmatches, 1) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
while ((nmatches - 1) < max_matches) {
|
|
|
|
char *match;
|
|
|
|
|
|
|
|
if (regexec(&re, str, 1, &rem, 0) != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (VIR_EXPAND_N(*matches, nmatches, 1) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (VIR_STRNDUP(match, str + rem.rm_so,
|
|
|
|
rem.rm_eo - rem.rm_so) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
VIR_DEBUG("Got '%s'", match);
|
|
|
|
|
|
|
|
(*matches)[nmatches-2] = match;
|
|
|
|
|
|
|
|
str = str + rem.rm_eo;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = nmatches - 1; /* don't count the trailing null */
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2014-01-23 09:28:29 +00:00
|
|
|
regfree(&re);
|
|
|
|
if (ret < 0) {
|
2016-11-25 08:18:35 +00:00
|
|
|
virStringListFree(*matches);
|
2014-01-23 09:28:29 +00:00
|
|
|
*matches = NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2014-02-19 20:30:46 +00:00
|
|
|
|
2017-05-11 15:16:13 +00:00
|
|
|
/**
|
|
|
|
* virStringMatch:
|
|
|
|
* @str: string to match
|
|
|
|
* @regexp: POSIX Extended regular expression pattern used for matching
|
|
|
|
*
|
|
|
|
* Performs a POSIX extended regex match against a string.
|
|
|
|
* Returns true on match, false on error or no match.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
virStringMatch(const char *str,
|
|
|
|
const char *regexp)
|
|
|
|
{
|
|
|
|
regex_t re;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
VIR_DEBUG("match '%s' for '%s'", str, regexp);
|
|
|
|
|
|
|
|
if ((rv = regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB)) != 0) {
|
|
|
|
char error[100];
|
|
|
|
regerror(rv, &re, error, sizeof(error));
|
|
|
|
VIR_WARN("error while compiling regular expression '%s': %s",
|
|
|
|
regexp, error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = regexec(&re, str, 0, NULL, 0);
|
|
|
|
|
|
|
|
regfree(&re);
|
|
|
|
|
|
|
|
return rv == 0;
|
|
|
|
}
|
|
|
|
|
2014-02-19 20:30:46 +00:00
|
|
|
/**
|
|
|
|
* virStringReplace:
|
|
|
|
* @haystack: the source string to process
|
|
|
|
* @oldneedle: the substring to locate
|
|
|
|
* @newneedle: the substring to insert
|
|
|
|
*
|
2014-04-20 20:07:46 +00:00
|
|
|
* Search @haystack and replace all occurrences of @oldneedle with @newneedle.
|
2014-02-19 20:30:46 +00:00
|
|
|
*
|
|
|
|
* Returns: a new string with all the replacements, or NULL on error
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virStringReplace(const char *haystack,
|
|
|
|
const char *oldneedle,
|
|
|
|
const char *newneedle)
|
|
|
|
{
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
const char *tmp1, *tmp2;
|
|
|
|
size_t oldneedlelen = strlen(oldneedle);
|
|
|
|
size_t newneedlelen = strlen(newneedle);
|
|
|
|
|
|
|
|
tmp1 = haystack;
|
|
|
|
tmp2 = NULL;
|
|
|
|
|
|
|
|
while (tmp1) {
|
|
|
|
tmp2 = strstr(tmp1, oldneedle);
|
|
|
|
|
|
|
|
if (tmp2) {
|
|
|
|
virBufferAdd(&buf, tmp1, (tmp2 - tmp1));
|
|
|
|
virBufferAdd(&buf, newneedle, newneedlelen);
|
|
|
|
tmp2 += oldneedlelen;
|
|
|
|
} else {
|
|
|
|
virBufferAdd(&buf, tmp1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp1 = tmp2;
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:40:15 +00:00
|
|
|
if (virBufferCheckError(&buf) < 0)
|
2014-02-19 20:30:46 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virBufferContentAndReset(&buf);
|
|
|
|
}
|
2014-10-07 15:27:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 10:30:16 +00:00
|
|
|
|
|
|
|
|
2017-04-26 14:26:53 +00:00
|
|
|
/**
|
|
|
|
* virStringHasChars:
|
|
|
|
* @str: string to look for chars in
|
|
|
|
* @chars: chars to find in string @str
|
|
|
|
*
|
|
|
|
* Returns true if @str contains any of the chars in @chars.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
virStringHasChars(const char *str,
|
|
|
|
const char *chars)
|
|
|
|
{
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return str[strcspn(str, chars)] != '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-14 10:30:16 +00:00
|
|
|
static const char control_chars[] =
|
|
|
|
"\x01\x02\x03\x04\x05\x06\x07"
|
|
|
|
"\x08" /* \t \n */ "\x0B\x0C" /* \r */ "\x0E\x0F"
|
|
|
|
"\x10\x11\x12\x13\x14\x15\x16\x17"
|
|
|
|
"\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
|
|
|
|
|
|
|
|
bool
|
|
|
|
virStringHasControlChars(const char *str)
|
|
|
|
{
|
2017-04-26 14:26:53 +00:00
|
|
|
return virStringHasChars(str, control_chars);
|
2015-04-14 10:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-10 16:21:13 +00:00
|
|
|
VIR_WARNINGS_NO_WLOGICALOP_STRCHR
|
2015-04-24 10:57:10 +00:00
|
|
|
|
|
|
|
|
2015-04-14 10:30:16 +00:00
|
|
|
/**
|
|
|
|
* virStringStripControlChars:
|
|
|
|
* @str: the string to strip
|
|
|
|
*
|
|
|
|
* Modify the string in-place to remove the control characters
|
|
|
|
* in the interval: [0x01, 0x20)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virStringStripControlChars(char *str)
|
|
|
|
{
|
|
|
|
size_t len, i, j;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
for (i = 0, j = 0; i < len; i++) {
|
2015-04-23 14:13:53 +00:00
|
|
|
if (strchr(control_chars, str[i]))
|
2015-04-14 10:30:16 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
str[j++] = str[i];
|
|
|
|
}
|
|
|
|
str[j] = '\0';
|
|
|
|
}
|
2015-06-15 16:53:58 +00:00
|
|
|
|
2017-12-18 14:46:53 +00:00
|
|
|
/**
|
|
|
|
* virStringFilterChars:
|
|
|
|
* @str: the string to strip
|
|
|
|
* @valid: the valid characters for the string
|
|
|
|
*
|
|
|
|
* Modify the string in-place to remove the characters that aren't
|
|
|
|
* in the list of valid ones.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virStringFilterChars(char *str, const char *valid)
|
|
|
|
{
|
|
|
|
size_t len, i, j;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
for (i = 0, j = 0; i < len; i++) {
|
|
|
|
if (strchr(valid, str[i]))
|
|
|
|
str[j++] = str[i];
|
|
|
|
}
|
|
|
|
str[j] = '\0';
|
|
|
|
}
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
/**
|
|
|
|
* virStringToUpper:
|
2018-01-03 09:56:35 +00:00
|
|
|
* @src string to capitalize
|
2015-06-15 16:53:58 +00:00
|
|
|
* @dst: where to store the new capitalized string
|
|
|
|
*
|
|
|
|
* Capitalize the string with replacement of all '-' characters for '_'
|
|
|
|
* characters. Caller frees the result.
|
|
|
|
*
|
2016-11-15 14:00:08 +00:00
|
|
|
* Returns 0 if src is NULL, 1 if capitalization was successful, -1 on failure.
|
2015-06-15 16:53:58 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virStringToUpper(char **dst, const char *src)
|
|
|
|
{
|
|
|
|
char *cap = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!src)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(cap, strlen(src) + 1) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; src[i]; i++) {
|
|
|
|
cap[i] = c_toupper(src[i]);
|
|
|
|
if (cap[i] == '-')
|
|
|
|
cap[i] = '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = cap;
|
|
|
|
return 1;
|
|
|
|
}
|
2016-04-11 11:35:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStringIsPrintable:
|
|
|
|
*
|
|
|
|
* Returns true @str contains only printable characters.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
virStringIsPrintable(const char *str)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; str[i]; i++)
|
|
|
|
if (!c_isprint(str[i]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-13 11:15:15 +00:00
|
|
|
|
|
|
|
|
2016-05-12 15:43:39 +00:00
|
|
|
/**
|
|
|
|
* virBufferIsPrintable:
|
|
|
|
*
|
|
|
|
* Returns true if @buf of @buflen contains only printable characters
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
virStringBufferIsPrintable(const uint8_t *buf,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < buflen; i++)
|
|
|
|
if (!c_isprint(buf[i]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-13 11:15:15 +00:00
|
|
|
/**
|
|
|
|
* virStringEncodeBase64:
|
|
|
|
* @buf: buffer of bytes to encode
|
|
|
|
* @buflen: number of bytes to encode
|
|
|
|
*
|
|
|
|
* Encodes @buf to base 64 and returns the resulting string. The caller is
|
|
|
|
* responsible for freeing the result.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virStringEncodeBase64(const uint8_t *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
base64_encode_alloc((const char *) buf, buflen, &ret);
|
|
|
|
if (!ret) {
|
|
|
|
virReportOOMError();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-05-10 08:16:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virStringTrimOptionalNewline:
|
|
|
|
* @str: the string to modify in-place
|
|
|
|
*
|
|
|
|
* Modify @str to remove a single '\n' character
|
|
|
|
* from its end, if one exists.
|
|
|
|
*/
|
|
|
|
void virStringTrimOptionalNewline(char *str)
|
|
|
|
{
|
2017-11-22 20:52:17 +00:00
|
|
|
size_t len = strlen(str);
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (str[len - 1] == '\n')
|
|
|
|
str[len - 1] = '\0';
|
2017-05-10 08:16:11 +00:00
|
|
|
}
|
2017-07-13 13:31:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStringParsePort:
|
|
|
|
* @str: port number to parse
|
|
|
|
* @port: pointer to parse port into
|
|
|
|
*
|
|
|
|
* Parses a string representation of a network port and validates it. Returns
|
|
|
|
* 0 on success and -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virStringParsePort(const char *str,
|
2017-07-20 10:42:53 +00:00
|
|
|
unsigned int *port)
|
2017-07-13 13:31:50 +00:00
|
|
|
{
|
|
|
|
unsigned int p = 0;
|
|
|
|
|
|
|
|
*port = 0;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (virStrToLong_uip(str, NULL, 10, &p) < 0) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("failed to parse port number '%s'"), str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p > UINT16_MAX) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("port '%s' out of range"), str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*port = p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|