From nobody Mon Feb 13 16:06:14 2023 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 4PFq2Z6ryGz3rMBs; Mon, 13 Feb 2023 16:06:14 +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 4PFq2Z617Tz3JTg; Mon, 13 Feb 2023 16:06:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1676304374; 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=cEZ5Jck38YH6SWP/Teml+AovQBhe+aL7v6XedqnxcUs=; b=W54RBHJursADxx5Ob5P4Veau1KOER9WENiT7FTb1JERz0jb3dQif1FQ6TEGL08dahHLdZ8 m3zrM402ppkA918thAQ8kajZ+xj7UL1YUarBD0wuPOGd7HU/ZkH+ltOnIzJkQN5u6yavhz JDVyP7cjol7VB1auMxrNTSwv4tz+S8pZquheXUAGQoXSEYJ3+1OZMRjBTTd0wFg9yUa4Ca WGSjyC/xrfuX/dmYmnlezj5qHJvIBJx+EH3V84sIzWXyE/OMR0q5ndtVeatLLezwxhHtGd NUPlHJItJ00dFPCJEv07RifNtOAFZg+LRgyz5ur7Ol8I6gSp0t/1Ftegs52Ndw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1676304374; 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=cEZ5Jck38YH6SWP/Teml+AovQBhe+aL7v6XedqnxcUs=; b=Ct2151wBqupsnni/ICZDZOwyQCRlOe9h7Kz+epVrBpVpGr7z359NBPslKelYCkGCna9oJe bAySWzLPVn/1zFptbYoSlcebiDgN2w+TPxUnJpaYVxhNJ4QGzNWXW8K+sA6T9x4LUXIqq4 EdZfrs2go1p367QHXQy254S6QI785Use1RuUqWZmpxi3Opi4eUHSvwH0a7vPU0oDmarP8n QKcLga9e2eGzDPy5jzTpIu8DUv/ex59KynuNPMmEavhbwr4lNivKpKk+gvXTE/Wzv2dFBw BefupD2hyAOxiyc3dEpDMbdAL1MMwpZJD7wYOuSBmYz4S96UJBKRWUHfuJcVKA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1676304374; a=rsa-sha256; cv=none; b=x6WgXWo9NTzgk4Ik3lFYuVbZ6CiQ+dKsaiul/RFhBL7GDaMVJqxMsrQDueeFWnkqy2CWh2 kROXbBC54QqLfxTss9NTlkRnF+q6oscV/iNtPHyzusoulhKd38I8XNuGpbIeCD+32j72Jp XptQKTvRXikldYUuYw8gMJk0dJ5HTv+c5w0ZV9L9G4Vba99fWQugNgz0E/ypxOK76XeS9R kLVllGTpgvM3cEucWkjHZvFl1aKMHp6rH1eYd6eBaaEf18jvx1cuSzFX6cen9fC5yzhz8M xqgZcXz7wa54qQkJtza3nFN7AQQcGlT9l4DrxFUJbvnTcpiJpVy5Q0ykqfUKpQ== 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 4PFq2Z53tDzPlt; Mon, 13 Feb 2023 16:06:14 +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 31DG6EwG025882; Mon, 13 Feb 2023 16:06:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 31DG6Ehu025881; Mon, 13 Feb 2023 16:06:14 GMT (envelope-from git) Date: Mon, 13 Feb 2023 16:06:14 GMT Message-Id: <202302131606.31DG6Ehu025881@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Alan Somers Subject: git: 4bd4e4b45e8b - main - sesutil: don't malloc in a tight loop 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: asomers X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4bd4e4b45e8bfbe855406098f393bdf4eda7104d Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by asomers: URL: https://cgit.FreeBSD.org/src/commit/?id=4bd4e4b45e8bfbe855406098f393bdf4eda7104d commit 4bd4e4b45e8bfbe855406098f393bdf4eda7104d Author: Alan Somers AuthorDate: 2023-02-09 22:23:51 +0000 Commit: Alan Somers CommitDate: 2023-02-13 16:04:05 +0000 sesutil: don't malloc in a tight loop Reported by: mav MFC after: 1 week Sponsored by: Axcient Reviwed by: mav Differential Revision: https://reviews.freebsd.org/D38473 --- usr.sbin/sesutil/sesutil.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/usr.sbin/sesutil/sesutil.c b/usr.sbin/sesutil/sesutil.c index 989216690edf..22bd0127a484 100644 --- a/usr.sbin/sesutil/sesutil.c +++ b/usr.sbin/sesutil/sesutil.c @@ -417,6 +417,17 @@ objmap(int argc, char **argv __unused) usage(stderr, "map"); } + memset(&e_desc, 0, sizeof(e_desc)); + /* SES4r02 allows element descriptors of up to 65536 characters */ + e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); + if (e_desc.elm_desc_str == NULL) + xo_err(EXIT_FAILURE, "calloc()"); + + e_devname.elm_devnames = calloc(128, sizeof(char)); + if (e_devname.elm_devnames == NULL) + xo_err(EXIT_FAILURE, "calloc()"); + e_devname.elm_names_size = 128; + /* Get the list of ses devices */ if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); @@ -481,28 +492,16 @@ objmap(int argc, char **argv __unused) xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); } /* Get the description of the element */ - memset(&e_desc, 0, sizeof(e_desc)); e_desc.elm_idx = e_ptr[j].elm_idx; e_desc.elm_desc_len = UINT16_MAX; - e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); - if (e_desc.elm_desc_str == NULL) { - close(fd); - xo_err(EXIT_FAILURE, "calloc()"); - } if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t) &e_desc) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC"); } + e_desc.elm_desc_str[e_desc.elm_desc_len] = '\0'; /* Get the device name(s) of the element */ - memset(&e_devname, 0, sizeof(e_devname)); e_devname.elm_idx = e_ptr[j].elm_idx; - e_devname.elm_names_size = 128; - e_devname.elm_devnames = calloc(128, sizeof(char)); - if (e_devname.elm_devnames == NULL) { - close(fd); - xo_err(EXIT_FAILURE, "calloc()"); - } if (ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t) &e_devname) <0) { /* Continue even if we can't look up devnames */ @@ -525,14 +524,14 @@ objmap(int argc, char **argv __unused) } print_extra_status(e_ptr[j].elm_type, e_status.cstat, PRINT_STYLE_DASHED); xo_close_instance("elements"); - free(e_desc.elm_desc_str); - free(e_devname.elm_devnames); } xo_close_list("elements"); free(e_ptr); close(fd); } globfree(&g); + free(e_devname.elm_devnames); + free(e_desc.elm_desc_str); xo_close_list("enclosures"); xo_close_container("sesutil"); xo_finish(); @@ -727,6 +726,10 @@ show(int argc, char **argv __unused) first_ses = true; + e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); + if (e_desc.elm_desc_str == NULL) + xo_err(EXIT_FAILURE, "calloc()"); + /* Get the list of ses devices */ if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); @@ -808,19 +811,14 @@ show(int argc, char **argv __unused) continue; /* Get the description of the element */ - memset(&e_desc, 0, sizeof(e_desc)); e_desc.elm_idx = e_ptr[j].elm_idx; e_desc.elm_desc_len = UINT16_MAX; - e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); - if (e_desc.elm_desc_str == NULL) { - close(fd); - xo_err(EXIT_FAILURE, "calloc()"); - } if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t) &e_desc) < 0) { close(fd); xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC"); } + e_desc.elm_desc_str[e_desc.elm_desc_len] = '\0'; switch (e_ptr[j].elm_type) { case ELMTYP_DEVICE: @@ -853,7 +851,6 @@ show(int argc, char **argv __unused) */ break; } - free(e_desc.elm_desc_str); } if (prev_type != (elm_type_t)-1 && prev_type != ELMTYP_DEVICE && prev_type != ELMTYP_ARRAY_DEV) @@ -863,6 +860,7 @@ show(int argc, char **argv __unused) close(fd); } globfree(&g); + free(e_desc.elm_desc_str); xo_close_list("enclosures"); xo_close_container("sesutil"); xo_finish();