git: f5a4ae253931 - stable/13 - libiscsiutil: Use open_memstream to build the outgoing block of keys.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 29 Apr 2022 23:12:18 UTC
The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=f5a4ae25393125057b0d287e48da1b6423db1c85 commit f5a4ae25393125057b0d287e48da1b6423db1c85 Author: John Baldwin <jhb@FreeBSD.org> AuthorDate: 2021-12-22 18:42:19 +0000 Commit: John Baldwin <jhb@FreeBSD.org> CommitDate: 2022-04-29 21:14:37 +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 (cherry picked from commit 2ccb8fde5eec8a0a25449374fd5a71654460947f) --- 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 8011b0a25329..8c156877a689 100644 --- a/lib/libiscsiutil/keys.c +++ b/lib/libiscsiutil/keys.c @@ -102,41 +102,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 *