mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-07-11 04:15:49 +00:00
virStrndup: Accept negative values as string length
It may shorten the code a bit as the following pattern: VIR_STRNDUP(dst, src, cond ? n : strlen(src)) is used on several places among our code. However, we can move the strlen into virStrndup and thus write just: VIR_STRNDUP(dst, src, cond ? n : -1)
This commit is contained in:
parent
267915ee69
commit
c93571968b
@ -568,12 +568,15 @@ virStrdup(char **dest,
|
|||||||
* caller's body where virStrndup is called from. Consider
|
* caller's body where virStrndup is called from. Consider
|
||||||
* using VIR_STRNDUP which sets these automatically.
|
* using VIR_STRNDUP which sets these automatically.
|
||||||
*
|
*
|
||||||
|
* In case @n is smaller than zero, the whole @src string is
|
||||||
|
* copied.
|
||||||
|
*
|
||||||
* Returns: 0 for NULL src, 1 on successful copy, -1 otherwise.
|
* Returns: 0 for NULL src, 1 on successful copy, -1 otherwise.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
virStrndup(char **dest,
|
virStrndup(char **dest,
|
||||||
const char *src,
|
const char *src,
|
||||||
size_t n,
|
ssize_t n,
|
||||||
bool report,
|
bool report,
|
||||||
int domcode,
|
int domcode,
|
||||||
const char *filename,
|
const char *filename,
|
||||||
@ -582,6 +585,8 @@ virStrndup(char **dest,
|
|||||||
{
|
{
|
||||||
if (!src)
|
if (!src)
|
||||||
return 0;
|
return 0;
|
||||||
|
if (n < 0)
|
||||||
|
n = strlen(src);
|
||||||
if (!(*dest = strndup(src, n))) {
|
if (!(*dest = strndup(src, n))) {
|
||||||
if (report)
|
if (report)
|
||||||
virReportOOMErrorFull(domcode, filename, funcname, linenr);
|
virReportOOMErrorFull(domcode, filename, funcname, linenr);
|
||||||
|
@ -93,7 +93,7 @@ int virStrdup(char **dest, const char *src, bool report, int domcode,
|
|||||||
const char *filename, const char *funcname, size_t linenr)
|
const char *filename, const char *funcname, size_t linenr)
|
||||||
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
|
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
|
||||||
|
|
||||||
int virStrndup(char **dest, const char *src, size_t n, bool report, int domcode,
|
int virStrndup(char **dest, const char *src, ssize_t n, bool report, int domcode,
|
||||||
const char *filename, const char *funcname, size_t linenr)
|
const char *filename, const char *funcname, size_t linenr)
|
||||||
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
|
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
|
||||||
|
|
||||||
@ -132,7 +132,9 @@ int virStrndup(char **dest, const char *src, size_t n, bool report, int domcode,
|
|||||||
* @n: the maximum number of bytes to copy
|
* @n: the maximum number of bytes to copy
|
||||||
*
|
*
|
||||||
* Duplicate @src string and store it into @dst. If @src is longer than @n,
|
* Duplicate @src string and store it into @dst. If @src is longer than @n,
|
||||||
* only @n bytes are copied and terminating null byte '\0' is added.
|
* only @n bytes are copied and terminating null byte '\0' is added. If @n
|
||||||
|
* is a negative number, then the whole @src string is copied. That is,
|
||||||
|
* VIR_STRDUP(dst, src) and VIR_STRNDUP(dst, src, -1) are equal.
|
||||||
*
|
*
|
||||||
* This macro is safe to use on arguments with side effects.
|
* This macro is safe to use on arguments with side effects.
|
||||||
*
|
*
|
||||||
@ -150,7 +152,10 @@ int virStrndup(char **dest, const char *src, size_t n, bool report, int domcode,
|
|||||||
* @n: the maximum number of bytes to copy
|
* @n: the maximum number of bytes to copy
|
||||||
*
|
*
|
||||||
* Duplicate @src string and store it into @dst. If @src is longer than @n,
|
* Duplicate @src string and store it into @dst. If @src is longer than @n,
|
||||||
* only @n bytes are copied and terminating null byte '\0' is added.
|
* only @n bytes are copied and terminating null byte '\0' is added. If @n
|
||||||
|
* is a negative number, then the whole @src string is copied. That is,
|
||||||
|
* VIR_STRDUP_QUIET(dst, src) and VIR_STRNDUP_QUIET(dst, src, -1) are
|
||||||
|
* equal.
|
||||||
*
|
*
|
||||||
* This macro is safe to use on arguments with side effects.
|
* This macro is safe to use on arguments with side effects.
|
||||||
*
|
*
|
||||||
|
@ -196,6 +196,40 @@ cleanup:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
testStrndupNegative(const void *opaque ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
char *dst;
|
||||||
|
const char *src = "Hello world";
|
||||||
|
int value;
|
||||||
|
|
||||||
|
if ((value = VIR_STRNDUP(dst, src, 5)) != 1) {
|
||||||
|
fprintf(stderr, "unexpected virStrndup result %d, expected 1\n", value);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (STRNEQ_NULLABLE(dst, "Hello")) {
|
||||||
|
fprintf(stderr, "unexpected content '%s'", dst);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
VIR_FREE(dst);
|
||||||
|
if ((value = VIR_STRNDUP(dst, src, -1)) != 1) {
|
||||||
|
fprintf(stderr, "unexpected virStrndup result %d, expected 1\n", value);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (STRNEQ_NULLABLE(dst, src)) {
|
||||||
|
fprintf(stderr, "unexpected content '%s'", dst);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(dst);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
mymain(void)
|
mymain(void)
|
||||||
@ -245,6 +279,9 @@ mymain(void)
|
|||||||
if (virtTestRun("strdup", 1, testStrdup, NULL) < 0)
|
if (virtTestRun("strdup", 1, testStrdup, NULL) < 0)
|
||||||
ret = -1;
|
ret = -1;
|
||||||
|
|
||||||
|
if (virtTestRun("strdup", 1, testStrndupNegative, NULL) < 0)
|
||||||
|
ret = -1;
|
||||||
|
|
||||||
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user