git: 2ccb8fde5eec - main - libiscsiutil: Use open_memstream to build the outgoing block of keys.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 22 Dec 2021 18:43:37 UTC
The branch main has been updated by jhb:
URL: https://cgit.FreeBSD.org/src/commit/?id=2ccb8fde5eec8a0a25449374fd5a71654460947f
commit 2ccb8fde5eec8a0a25449374fd5a71654460947f
Author: John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2021-12-22 18:42:19 +0000
Commit: John Baldwin <jhb@FreeBSD.org>
CommitDate: 2021-12-22 18:43:23 +0000
libiscsiutil: Use open_memstream to build the outgoing block of keys.
Reviewed by: mav
Sponsored by: Chelsio Communications
Differential Revision: https://reviews.freebsd.org/D33546
---
lib/libiscsiutil/keys.c | 38 +++++++++++++++-----------------------
1 file 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 *