Add a new helper that sanitizes error semantics of base64_encode_alloc.
#include "virfile.h"
#include "virhash.h"
#include "virlog.h"
+#include "virstring.h"
#include "base64.h"
#define VIR_FROM_THIS VIR_FROM_SECRET
if (!secret->value)
return 0;
- base64_encode_alloc((const char *)secret->value, secret->value_size,
- &base64);
- if (base64 == NULL) {
- virReportOOMError();
+ if (!(base64 = virStringEncodeBase64(secret->value, secret->value_size)))
goto cleanup;
- }
if (virFileRewrite(secret->base64File, S_IRUSR | S_IWUSR,
virSecretRewriteFile, base64) < 0)
virStrcpy;
virStrdup;
virStringArrayHasString;
+virStringEncodeBase64;
virStringFreeList;
virStringFreeListCount;
virStringGetFirstWithPrefix;
virJSONValuePtr reply = NULL;
char *password64 = NULL;
- base64_encode_alloc(password, strlen(password), &password64);
- if (!password64) {
- virReportOOMError();
+ if (!(password64 = virStringEncodeBase64((unsigned char *) password,
+ strlen(password))))
goto cleanup;
- }
if (!(cmd = qemuAgentMakeCommand("guest-set-user-password",
"b:crypted", crypted,
int r = 0;
virStorageAuthDefPtr authdef = source->auth;
unsigned char *secret_value = NULL;
- size_t secret_value_size;
+ size_t secret_value_size = 0;
char *rados_key = NULL;
virBuffer mon_host = VIR_BUFFER_INITIALIZER;
virSecretPtr secret = NULL;
goto cleanup;
}
- base64_encode_alloc((char *)secret_value,
- secret_value_size, &rados_key);
- memset(secret_value, 0, secret_value_size);
-
- if (rados_key == NULL) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("failed to decode the RADOS key"));
+ if (!(rados_key = virStringEncodeBase64(secret_value, secret_value_size)))
goto cleanup;
- }
VIR_DEBUG("Found cephx key: %s", rados_key);
if (rados_conf_set(ptr->cluster, "key", rados_key) < 0) {
goto cleanup;
}
- memset(rados_key, 0, strlen(rados_key));
-
if (rados_conf_set(ptr->cluster, "auth_supported", "cephx") < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("failed to set RADOS option: %s"),
ret = 0;
cleanup:
- VIR_FREE(secret_value);
- VIR_FREE(rados_key);
+ VIR_DISPOSE_N(secret_value, secret_value_size);
+ VIR_DISPOSE_STRING(rados_key);
virObjectUnref(secret);
#include <stdio.h>
#include <regex.h>
+#include "base64.h"
#include "c-ctype.h"
#include "virstring.h"
#include "viralloc.h"
return true;
}
+
+
+/**
+ * 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;
+}
bool virStringIsPrintable(const char *str);
+char *virStringEncodeBase64(const uint8_t *buf, size_t buflen);
+
#endif /* __VIR_STRING_H__ */
#include "viralloc.h"
#include "virfile.h"
#include "virutil.h"
+#include "virstring.h"
#include "conf/secret_conf.h"
static virSecretPtr
if (value == NULL)
goto cleanup;
- base64_encode_alloc((char *)value, value_size, &base64);
- memset(value, 0, value_size);
- VIR_FREE(value);
-
- if (base64 == NULL) {
- vshError(ctl, "%s", _("Failed to allocate memory"));
+ if (!(base64 = virStringEncodeBase64(value, value_size)))
goto cleanup;
- }
+
vshPrint(ctl, "%s", base64);
- memset(base64, 0, strlen(base64));
- VIR_FREE(base64);
ret = true;
cleanup:
+ VIR_DISPOSE_N(value, value_size);
+ VIR_DISPOSE_STRING(base64);
virSecretFree(secret);
return ret;
}