From nobody Fri Apr 29 23:12:18 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 0049E1ABC315; Fri, 29 Apr 2022 23:12:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KqpD31w0Sz3wD3; Fri, 29 Apr 2022 23:12:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651273939; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=9Ew/XZHWD4jOSw5qJDjXynh0KqyyqkKxd0Lrw34T07E=; b=IjsNTN3g8q5l9PWr4JqhrlToFtOu8azVVN8RuVRt9OEtZ9xoOI4NerePTf314gXkKBq02r 86WE1bpLgUVA4nppNc8A/InvKZeYNfr3jNdRIDUTSQu2y0b6+y7IAtzrQObD+UX7UcPPlx K2yjDZaqKA6ppyYtatKvwyeX6ZjtEPXjECYSonht06186ABl0Yd6JqZzMEsdebYkKmRsL9 2Q28F+WXybxZTNSa2sC7zqcF/PBPzziONMERAODrzJ9tRq/YAjCz3Pa87bOMfwDbJ2L6Hk C47B99Dog0wv1zBY9kpL73hyydab2uWwCM2hrErHwVG3VI67FRVccCcLAf4xIw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 408F8162BE; Fri, 29 Apr 2022 23:12:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 23TNCIQr044469; Fri, 29 Apr 2022 23:12:18 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 23TNCIX9044468; Fri, 29 Apr 2022 23:12:18 GMT (envelope-from git) Date: Fri, 29 Apr 2022 23:12:18 GMT Message-Id: <202204292312.23TNCIX9044468@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: f5a4ae253931 - stable/13 - libiscsiutil: Use open_memstream to build the outgoing block of keys. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: f5a4ae25393125057b0d287e48da1b6423db1c85 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651273939; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=9Ew/XZHWD4jOSw5qJDjXynh0KqyyqkKxd0Lrw34T07E=; b=SgL7wgs1l/ye7R72IEzf4iZRSEaNc1B4wMBNXB1ewsxMF3Hq0nMPLgPjItxqs13Trwm8Vw CR6u/TIO+Cr0eh4AwnkNjJIYVoQDGa+Cu5zmUUYeHqYh2eNydag4UU0dyzNdiS4TVY6/HA 68KiQBeW0sYO4384fmpZgnmZL3ZvJglwOawdCoPQLvFM1xbMTKHeFcR4IASsB/Q54gj2VS Qp5nKP3+9GHdVsoZemjcg+r6hn/JwIOck5qbeyktbFyGbE0R/wneuQcGCW+HsnN3Qfo2BF csQ42ey5i7n993rDkJ8e9tJvwY+IHqKpYyKLPyqVU6hXCWnS+/gJjAPZq1Gt/w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1651273939; a=rsa-sha256; cv=none; b=az4tTSTSl89y4JmT3u01ISHuRTkIg+D62tpehjUToG8jbbhS1uBQpyrLfcNRMxPf7Ipws2 qbfj1Z2fitCX7ibxi/ajcKcKB7ire61CrYb4+pAWPORLQ+6qeYmEieTiuhmTMWIUQ1umvL ebrw0kpp1EAxpmuRht23uxiJ3tCIxLVA/AW/v8TbA6/mpm3Wrntnn0aCd7OW0ilhYiE9ZD hrIHbEIo7cgLBiffQspfr58TM8444xEUB/As3BfwWusBI6ciaEckYUSNBq1o9IU/dFZWWG HR88boaU2DCO50he6jUWwqSo1Vl6se8p0hIT2BbY0VzXj1TTVeHh4ir+2xVPww== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=f5a4ae25393125057b0d287e48da1b6423db1c85 commit f5a4ae25393125057b0d287e48da1b6423db1c85 Author: John Baldwin AuthorDate: 2021-12-22 18:42:19 +0000 Commit: John Baldwin 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 *