aboutsummaryrefslogtreecommitdiff
path: root/lib/libiscsiutil
diff options
context:
space:
mode:
authorJohn Baldwin <jhb@FreeBSD.org>2021-12-22 18:42:19 +0000
committerJohn Baldwin <jhb@FreeBSD.org>2021-12-22 18:43:23 +0000
commit2ccb8fde5eec8a0a25449374fd5a71654460947f (patch)
treeaabb62b523b7b420c0b26b92a00ead47f1029fbd /lib/libiscsiutil
parentfd99905b4591a5e4df3dda32e4c67258aaf44517 (diff)
downloadsrc-2ccb8fde5eec8a0a25449374fd5a71654460947f.tar.gz
src-2ccb8fde5eec8a0a25449374fd5a71654460947f.zip
Diffstat (limited to 'lib/libiscsiutil')
-rw-r--r--lib/libiscsiutil/keys.c38
1 files changed, 15 insertions, 23 deletions
diff --git a/lib/libiscsiutil/keys.c b/lib/libiscsiutil/keys.c
index 185a179906b9..9b165df87557 100644
--- a/lib/libiscsiutil/keys.c
+++ b/lib/libiscsiutil/keys.c
@@ -106,41 +106,33 @@ keys_load(struct keys *keys, const struct pdu *pdu)
void
keys_save(struct keys *keys, struct pdu *pdu)
{
+ FILE *fp;
char *data;
size_t len;
int i;
- /*
- * XXX: Not particularly efficient.
- */
- len = 0;
+ fp = open_memstream(&data, &len);
+ if (fp == NULL)
+ log_err(1, "open_memstream");
for (i = 0; i < KEYS_MAX; i++) {
if (keys->keys_names[i] == NULL)
break;
- /*
- * +1 for '=', +1 for '\0'.
- */
- len += strlen(keys->keys_names[i]) +
- strlen(keys->keys_values[i]) + 2;
- }
- if (len == 0)
- return;
+ fprintf(fp, "%s=%s", keys->keys_names[i], keys->keys_values[i]);
- data = malloc(len);
- if (data == NULL)
- log_err(1, "malloc");
+ /* Append a '\0' after each key pair. */
+ fputc('\0', fp);
+ }
+ if (fclose(fp) != 0)
+ log_err(1, "fclose");
+
+ if (len == 0) {
+ free(data);
+ data = NULL;
+ }
pdu->pdu_data = data;
pdu->pdu_data_len = len;
-
- for (i = 0; i < KEYS_MAX; i++) {
- if (keys->keys_names[i] == NULL)
- break;
- data += sprintf(data, "%s=%s",
- keys->keys_names[i], keys->keys_values[i]);
- data += 1; /* for '\0'. */
- }
}
const char *