From nobody Wed Dec 27 06:08:11 2023 X-Original-To: dev-commits-src-main@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 4T0LmC5LWjz55Bcf; Wed, 27 Dec 2023 06:08:11 +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 4T0LmC3dnSz3JMw; Wed, 27 Dec 2023 06:08:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703657291; 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=TLCr25blznBxRX5G0DfYlN+vIE5DlhGBtMQEDSpUpIo=; b=w3S0m14q43wyTiPZVJGTdfv6McZs0DoLMErKOQleIv8e1XGK6V3XH8EEBddvtoVC+Xsiyc t2GI++wD9g+PrliEu82vh9MLLAQWqg05lR5qNTXFSL3c5mijYdiSTr5Nz9vkpr94DipNj3 Js2r+j/zj253RC5MKAj2ShI6/V8MPCGqD239Ph6B6vWkYeUWhHn69cvnBIbbUwTwls627N XJnYOXkPQRfheuUF4LXOZRUY68OOJiLus7FOYLY4HUR1kWFdnYVA92oSvQU4sZJePrpdhr 3DldZm8uYweK/WiTmDrVnUBYefviqe8Niaogt8bv9U8Zvjt5tozoHY2j60dn7A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703657291; a=rsa-sha256; cv=none; b=r3o3i8HKg6lyt0T/VWlNquo6MxmFHqFqCMzSMz65EOB76Xtu+xkEspjuJ69Cwsq73Bo4y2 7P5rsjodcesucYy96jt8l/2OhxYHrJNumzRYgAuU9ANWdLcGvEIjilCQnCdG81LlDJJwII taXsLRApbEteMHTE4OKoas98XSegqZzCPV7S9jjfcsOWGQduKeDyq+130c0hV8rVSE+oUi W0y2vFt86pwVcStP7vDkvJUudOgJM/CVRzAEJRn17iCZ+PH1a6rcKPWpyZSIhNzEuS1Sfp g0CkL+VohVt/mfwfzjc1fn1gNYEhHGHab6XyjVUa7jgEKL43GnboM/+vj3KP1w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703657291; 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=TLCr25blznBxRX5G0DfYlN+vIE5DlhGBtMQEDSpUpIo=; b=p246YOm4572lWop1rtjyKVAvD5s5J82iQ1KCNE70ONSoyreGytjxHJpSRYbf4laR0lSlvW cAe2jt+YBIE80o6vXsh8cYiftekda4eWYwYyWMj2/XtBGZPUgofohDwKKKLM7IzkGq22mp dipm8tY3Vf3T6r5FOdtyPflIJ8UvTbPrHFhmJttLob9MVSi+2cFfYDQFvTAVDdCeaQsk1t vEOTQEUzLx/Fd8Q1aQ8A+NVeMRj2wiAqHlxl4JGTIXNwtFCRCPYX2U9RYYuOQo0hRXYWye XQMbqhCV+dvKEuXLkDCNAwDeDCIVArYGtuEO8Xv14BI4UJAajq0/xtaS0hndLQ== 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 4T0LmC2jvhzCG0; Wed, 27 Dec 2023 06:08:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BR68B5Y024975; Wed, 27 Dec 2023 06:08:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BR68BED024972; Wed, 27 Dec 2023 06:08:11 GMT (envelope-from git) Date: Wed, 27 Dec 2023 06:08:11 GMT Message-Id: <202312270608.3BR68BED024972@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 5745a5841413 - main - daemon: replace memchr with memrchr List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 5745a5841413da5c2e002ba65c60d7b9d594bc52 Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=5745a5841413da5c2e002ba65c60d7b9d594bc52 commit 5745a5841413da5c2e002ba65c60d7b9d594bc52 Author: Ihor Antonov AuthorDate: 2023-12-27 06:07:26 +0000 Commit: Kyle Evans CommitDate: 2023-12-27 06:07:26 +0000 daemon: replace memchr with memrchr Looping over lines in the buffer is not needed. Same effect can be achieved by looking for the last new line. If found the buffer is guaranteed to have one or more complete lines. All complete lines are flushed at once with no looping. Reviewed by: cperciva, kevans --- usr.sbin/daemon/daemon.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index e9c4bf05504c..066240fc8302 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -587,23 +587,27 @@ static bool listen_child(struct daemon_state *state) { ssize_t rv; + unsigned char *cp; assert(state != NULL); assert(state->pos < LBUF_SIZE - 1); rv = read(state->pipe_fd[0], state->buf + state->pos, LBUF_SIZE - state->pos - 1); if (rv > 0) { - unsigned char *cp; - state->pos += rv; assert(state->pos <= LBUF_SIZE - 1); /* Always NUL-terminate just in case. */ state->buf[LBUF_SIZE - 1] = '\0'; + /* - * Chomp line by line until we run out of buffer. + * Find position of the last newline in the buffer. + * The buffer is guaranteed to have one or more complete lines + * if at least one newline was found when searching in reverse. + * All complete lines are flushed. * This does not take NUL characters into account. */ - while ((cp = memchr(state->buf, '\n', state->pos)) != NULL) { + cp = memrchr(state->buf, '\n', state->pos); + if (cp != NULL) { size_t bytes_line = cp - state->buf + 1; assert(bytes_line <= state->pos); do_output(state->buf, bytes_line, state);