提交 6de994cd 编写于 作者: D Daniel P. Berrangé

util: make string functions abort on OOM

The functions are left returning an "int" to avoid an immediate
big-bang cleanup. They'll simply never return anything other
than 0.
Reviewed-by: NMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
Signed-off-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 252e9b38
...@@ -726,40 +726,27 @@ virDoubleToStr(char **strp, double number) ...@@ -726,40 +726,27 @@ virDoubleToStr(char **strp, double number)
int int
virVasprintfInternal(bool report, virVasprintfInternal(char **strp,
int domcode,
const char *filename,
const char *funcname,
size_t linenr,
char **strp,
const char *fmt, const char *fmt,
va_list list) va_list list)
{ {
int ret; int ret;
if ((ret = vasprintf(strp, fmt, list)) == -1) { if ((ret = vasprintf(strp, fmt, list)) == -1)
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
*strp = NULL;
}
return ret; return ret;
} }
int int
virAsprintfInternal(bool report, virAsprintfInternal(char **strp,
int domcode,
const char *filename,
const char *funcname,
size_t linenr,
char **strp,
const char *fmt, ...) const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret; int ret;
va_start(ap, fmt); va_start(ap, fmt);
ret = virVasprintfInternal(report, domcode, filename, ret = virVasprintfInternal(strp, fmt, ap);
funcname, linenr, strp, fmt, ap);
va_end(ap); va_end(ap);
return ret; return ret;
} }
...@@ -937,37 +924,20 @@ virStringIsEmpty(const char *str) ...@@ -937,37 +924,20 @@ virStringIsEmpty(const char *str)
* virStrdup: * virStrdup:
* @dest: where to store duplicated string * @dest: where to store duplicated string
* @src: the source string to duplicate * @src: the source string to duplicate
* @report: whether to report OOM error, if there is one *
* @domcode: error domain code * Wrapper over strdup, which aborts on OOM error.
* @filename: caller's filename *
* @funcname: caller's funcname * Returns: 0 for NULL src, 1 on successful copy, aborts on OOM
* @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.
*
* Returns: 0 for NULL src, 1 on successful copy, -1 otherwise.
*/ */
int int
virStrdup(char **dest, virStrdup(char **dest,
const char *src, const char *src)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
*dest = NULL; *dest = NULL;
if (!src) if (!src)
return 0; return 0;
if (!(*dest = strdup(src))) { if (!(*dest = strdup(src)))
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
}
return 1; return 1;
} }
...@@ -977,45 +947,28 @@ virStrdup(char **dest, ...@@ -977,45 +947,28 @@ virStrdup(char **dest,
* @dest: where to store duplicated string * @dest: where to store duplicated string
* @src: the source string to duplicate * @src: the source string to duplicate
* @n: how many bytes to copy * @n: how many bytes to copy
* @report: whether to report OOM error, if there is one *
* @domcode: error domain code * Wrapper over strndup, which aborts on OOM error.
* @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.
* *
* In case @n is smaller than zero, the whole @src string is * In case @n is smaller than zero, the whole @src string is
* copied. * copied.
* *
* Returns: 0 for NULL src, 1 on successful copy, -1 otherwise. * Returns: 0 for NULL src, 1 on successful copy, aborts on OOM
*/ */
int int
virStrndup(char **dest, virStrndup(char **dest,
const char *src, const char *src,
ssize_t n, ssize_t n)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
*dest = NULL; *dest = NULL;
if (!src) if (!src)
return 0; return 0;
if (n < 0) if (n < 0)
n = strlen(src); n = strlen(src);
if (!(*dest = strndup(src, n))) { if (!(*dest = strndup(src, n)))
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
}
return 1; return 1;
} }
...@@ -1483,10 +1436,8 @@ virStringEncodeBase64(const uint8_t *buf, size_t buflen) ...@@ -1483,10 +1436,8 @@ virStringEncodeBase64(const uint8_t *buf, size_t buflen)
char *ret; char *ret;
base64_encode_alloc((const char *) buf, buflen, &ret); base64_encode_alloc((const char *) buf, buflen, &ret);
if (!ret) { if (!ret)
virReportOOMError(); abort();
return NULL;
}
return ret; return ret;
} }
......
...@@ -128,22 +128,16 @@ int virStrcpy(char *dest, const char *src, size_t destbytes) ...@@ -128,22 +128,16 @@ int virStrcpy(char *dest, const char *src, size_t destbytes)
#define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest)) #define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest))
/* Don't call these directly - use the macros below */ /* Don't call these directly - use the macros below */
int virStrdup(char **dest, const char *src, bool report, int domcode, int virStrdup(char **dest, const char *src)
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, ssize_t n, bool report, int domcode, int virStrndup(char **dest, const char *src, ssize_t n)
const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virAsprintfInternal(bool report, int domcode, const char *filename, int virAsprintfInternal(char **strp, const char *fmt, ...)
const char *funcname, size_t linenr, char **strp, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3)
const char *fmt, ...)
ATTRIBUTE_NONNULL(6) ATTRIBUTE_NONNULL(7) ATTRIBUTE_FMT_PRINTF(7, 8)
ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_RETURN_CHECK;
int virVasprintfInternal(bool report, int domcode, const char *filename, int virVasprintfInternal(char **strp, const char *fmt, va_list list)
const char *funcname, size_t linenr, char **strp, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0)
const char *fmt, va_list list)
ATTRIBUTE_NONNULL(6) ATTRIBUTE_NONNULL(7) ATTRIBUTE_FMT_PRINTF(7, 0)
ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_RETURN_CHECK;
/** /**
...@@ -155,11 +149,9 @@ int virVasprintfInternal(bool report, int domcode, const char *filename, ...@@ -155,11 +149,9 @@ int virVasprintfInternal(bool report, int domcode, const char *filename,
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 if @src was NULL, * Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM
* 1 if @src was copied
*/ */
#define VIR_STRDUP(dst, src) virStrdup(&(dst), src, true, VIR_FROM_THIS, \ #define VIR_STRDUP(dst, src) virStrdup(&(dst), src)
__FILE__, __FUNCTION__, __LINE__)
/** /**
* VIR_STRDUP_QUIET: * VIR_STRDUP_QUIET:
...@@ -170,9 +162,9 @@ int virVasprintfInternal(bool report, int domcode, const char *filename, ...@@ -170,9 +162,9 @@ int virVasprintfInternal(bool report, int domcode, const char *filename,
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 if @src was NULL, 1 if @src was copied * Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM
*/ */
#define VIR_STRDUP_QUIET(dst, src) virStrdup(&(dst), src, false, 0, NULL, NULL, 0) #define VIR_STRDUP_QUIET(dst, src) VIR_STRDUP(dst, src)
/** /**
* VIR_STRNDUP: * VIR_STRNDUP:
...@@ -187,12 +179,9 @@ int virVasprintfInternal(bool report, int domcode, const char *filename, ...@@ -187,12 +179,9 @@ int virVasprintfInternal(bool report, int domcode, const char *filename,
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 if @src was NULL, * Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM
* 1 if @src was copied
*/ */
#define VIR_STRNDUP(dst, src, n) virStrndup(&(dst), src, n, true, \ #define VIR_STRNDUP(dst, src, n) virStrndup(&(dst), src, n)
VIR_FROM_THIS, __FILE__, \
__FUNCTION__, __LINE__)
/** /**
* VIR_STRNDUP_QUIET: * VIR_STRNDUP_QUIET:
...@@ -208,51 +197,41 @@ int virVasprintfInternal(bool report, int domcode, const char *filename, ...@@ -208,51 +197,41 @@ int virVasprintfInternal(bool report, int domcode, const char *filename,
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 if @src was NULL, 1 if @src was copied * Returns 0 if @src was NULL, 1 if @src was copied, aborts on OOM
*/ */
#define VIR_STRNDUP_QUIET(dst, src, n) virStrndup(&(dst), src, n, false, \ #define VIR_STRNDUP_QUIET(dst, src, n) virStrndup(&(dst), src, n)
0, NULL, NULL, 0)
size_t virStringListLength(const char * const *strings); size_t virStringListLength(const char * const *strings);
/** /**
* virVasprintf * virVasprintf
* *
* Like glibc's vasprintf but makes sure *strp == NULL on failure, in which * Like glibc's vasprintf but aborts on OOM
* case the OOM error is reported too.
* *
* Returns -1 on failure (with OOM error reported), number of bytes printed * Returns number of bytes printed on success, aborts on OOM
* on success.
*/ */
#define virVasprintf(strp, fmt, list) \ #define virVasprintf(strp, fmt, list) virVasprintfInternal(strp, fmt, list)
virVasprintfInternal(true, VIR_FROM_THIS, __FILE__, __FUNCTION__, \
__LINE__, strp, fmt, list)
/** /**
* virVasprintfQuiet * virVasprintfQuiet
* *
* Like glibc's vasprintf but makes sure *strp == NULL on failure. * Like glibc's vasprintf but aborts on OOM.
* *
* Returns -1 on failure, number of bytes printed on success. * Returns number of bytes printed on success, aborts on OOM
*/ */
#define virVasprintfQuiet(strp, fmt, list) \ #define virVasprintfQuiet(strp, fmt, list) virVasprintf(strp, fmt, list)
virVasprintfInternal(false, 0, NULL, NULL, 0, strp, fmt, list)
/** /**
* virAsprintf: * virAsprintf:
* @strp: variable to hold result (char **) * @strp: variable to hold result (char **)
* @fmt: printf format * @fmt: printf format
* *
* Like glibc's asprintf but makes sure *strp == NULL on failure, in which case * Like glibc's asprintf but aborts on OOM.
* the OOM error is reported too.
* *
* Returns -1 on failure (with OOM error reported), number of bytes printed * Returns number of bytes printed on success, aborts on OOM
* on success.
*/ */
#define virAsprintf(strp, ...) \ #define virAsprintf(strp, ...) virAsprintfInternal(strp, __VA_ARGS__)
virAsprintfInternal(true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__, \
strp, __VA_ARGS__)
/** /**
* virAsprintfQuiet: * virAsprintfQuiet:
...@@ -261,12 +240,10 @@ size_t virStringListLength(const char * const *strings); ...@@ -261,12 +240,10 @@ size_t virStringListLength(const char * const *strings);
* *
* Like glibc's asprintf but makes sure *strp == NULL on failure. * Like glibc's asprintf but makes sure *strp == NULL on failure.
* *
* Returns -1 on failure, number of bytes printed on success. * Returns number of bytes printed on success, aborts on OOM
*/ */
#define virAsprintfQuiet(strp, ...) \ #define virAsprintfQuiet(strp, ...) virAsprintf(strp, __VA_ARGS__)
virAsprintfInternal(false, 0, NULL, NULL, 0, \
strp, __VA_ARGS__)
int virStringSortCompare(const void *a, const void *b); int virStringSortCompare(const void *a, const void *b);
int virStringSortRevCompare(const void *a, const void *b); int virStringSortRevCompare(const void *a, const void *b);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册