From patchwork Tue Jun 23 17:55:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11621289 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9CC9114F6 for ; Tue, 23 Jun 2020 17:55:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8191820781 for ; Tue, 23 Jun 2020 17:55:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="se5GqFXU" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733133AbgFWRzu (ORCPT ); Tue, 23 Jun 2020 13:55:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732973AbgFWRzt (ORCPT ); Tue, 23 Jun 2020 13:55:49 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6341FC061755 for ; Tue, 23 Jun 2020 10:55:49 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id e12so5187593qtr.9 for ; Tue, 23 Jun 2020 10:55:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8JbklcbvWcdiJ1x9Npd1bM0uwrCcvK+2aGR5z5h8pE8=; b=se5GqFXUJqLB6CWbVZoPMu1q+OnmhdADVpelwBReZF1DAUalHwlb2TkS2xcPiXG68m O7HpMu/wlEdwRJHKgkYMHKY57LKH+pwBVZD/Y2R68Y8eMmWwSOWHqVP9TxPuEP67QnV2 QBSfUdnWWv2m1Wst6uqRFTdvdWD08xuO/HwrEoL+Hgc5WbIadCuv82exNSEL2mz90vxa ql1wnHqicOVciDK3JddQMCw1VCFcZEWq590rxMLVEsDidCGHW+6QH8lOzb6yWDyvZ+Mk vCQrIsXziqdE5OEXHq5k/DWiG4oBrqhnOdnAKq1OeYwjlCjzaeLmGSma0rGegbiG1nlB 9xog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8JbklcbvWcdiJ1x9Npd1bM0uwrCcvK+2aGR5z5h8pE8=; b=Btid8YmBR7BCSgRsAof1XSHLF+sWtR9wtbtsgigWGK4oH1pyPC5xDklepilwHEgAAo ByRXuS8zQWU+OXx1dxYiOuxkZwg2TYQOZ4C0aYLYQQWp9yW9Pt58kWvyMgaVXCPx487b 4VrXoypoHJ3+bj8pgh5hZzsVKlHvj/rU5PdusfiwiOvsja42pd9bWex6aIL4bl8IXmQ3 mlFOvIvujEgVitILHqCwuEuTXnsR7VTxnZp34T1ABEJ/vz3hkOeSMcG8m7llvO5bXBpZ UgOHhx+Lmu2wELme/34NmDUPlmZpt9vnTes2kwhxrWQq/yFIJ+sAOg3NJRxHXEpKycSM IXUg== X-Gm-Message-State: AOAM532/IkfZHv3mJRYV49skOAPnfe6tlm6BWVgl1NA9CIiYLqOGz2J2 uUHn0DEm2sY3lHTBjPRB9jz6PkWiF3w= X-Google-Smtp-Source: ABdhPJx8loQNNPoiPoBQhjEdInvAnycVkyDJeeylHWzMqAkVTHWh2SjcdUUxpcdm3BT1PhEDYcQxxg== X-Received: by 2002:ac8:33f8:: with SMTP id d53mr21688828qtb.80.1592934948494; Tue, 23 Jun 2020 10:55:48 -0700 (PDT) Received: from archbookpro.phub.net.cable.rogers.com (CPEc05627352ede-CM185933998587.cpe.net.cable.rogers.com. [174.112.146.193]) by smtp.gmail.com with ESMTPSA id l2sm1380920qtc.80.2020.06.23.10.55.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jun 2020 10:55:47 -0700 (PDT) From: Denton Liu To: Git Mailing List Cc: Jeff King , Chris Torek , =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= , Junio C Hamano Subject: [PATCH v3 1/3] remote-curl: use strlen() instead of magic numbers Date: Tue, 23 Jun 2020 13:55:32 -0400 Message-Id: X-Mailer: git-send-email 2.27.0.307.g7979e895e7 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When we are dealing with a packet-line length header, we use the magic literal `4`, representing the length of "0000" and "0001", the packet line length headers. Use `strlen("000x")` so that we do not have to use the magic literal. Signed-off-by: Denton Liu --- remote-curl.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/remote-curl.c b/remote-curl.c index 75532a8bae..a2cbef546b 100644 --- a/remote-curl.c +++ b/remote-curl.c @@ -596,10 +596,10 @@ static int rpc_read_from_out(struct rpc_state *rpc, int options, set_packet_header(buf - 4, *appended); break; case PACKET_READ_DELIM: - memcpy(buf - 4, "0001", 4); + memcpy(buf - strlen("0001"), "0001", strlen("0001")); break; case PACKET_READ_FLUSH: - memcpy(buf - 4, "0000", 4); + memcpy(buf - strlen("0000"), "0000", strlen("0000")); break; case PACKET_READ_RESPONSE_END: die(_("remote server sent stateless separator")); @@ -804,7 +804,7 @@ static int probe_rpc(struct rpc_state *rpc, struct slot_results *results) curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL); curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000"); - curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4); + curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, strlen("0000")); curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf); From patchwork Tue Jun 23 17:55:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11621291 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AA96960D for ; Tue, 23 Jun 2020 17:55:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8ACEF20780 for ; Tue, 23 Jun 2020 17:55:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="tfGm+RfZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733153AbgFWRzw (ORCPT ); Tue, 23 Jun 2020 13:55:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733082AbgFWRzv (ORCPT ); Tue, 23 Jun 2020 13:55:51 -0400 Received: from mail-qt1-x841.google.com (mail-qt1-x841.google.com [IPv6:2607:f8b0:4864:20::841]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 499E9C061573 for ; Tue, 23 Jun 2020 10:55:51 -0700 (PDT) Received: by mail-qt1-x841.google.com with SMTP id j10so7482400qtq.11 for ; Tue, 23 Jun 2020 10:55:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=M6uWLlokDu6jnAAFUesEpZHX0r0JBcEkr1D2A2GQ7yk=; b=tfGm+RfZgoDrecn2+RfX6XuO/VSCSLPpCuNI7jvX53nIle5RStj9ROfusJWQ8vbX0k 2knAs6NYyeCguO44nSJzKQKnwySgwQpe/+PeVcjFuCvqokGJYXRlSTZwSDPf/dfP1LLF rQrNHdGC1RJtn5b+dVCVKIkXzUVN3gNS5OlRXxlwXbwipzX0wb40/lYPA10CqxjUYJIY MK4zlJ+LU0ui8270Hew6H51VVPij7tJy+0TqvGdy20QuU62y/k5QXopmdGwJPzdupvau sLoesGVgaRKC5Vc8cPW6JcWFx4PGaCRmZtFcBeLMxj/q7KQtWv6rBVn4kfS+rJlQYIlB i+3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=M6uWLlokDu6jnAAFUesEpZHX0r0JBcEkr1D2A2GQ7yk=; b=AS30dJ+IDIHdfxj1dXig2jl17felaoKvGRoC+QXoSc/cJZcEtlggXeQK0lg+Ky5YCa cS68FPOkLmtdw4Fkns4ELPr6rxdXJmL6FcPyJXzpOKAzv07vB+vLcNjnBkW7FUKpCn2g 0PDq+o3Dy5yDc7vm18y2bX8GOOMdeIDmbIqRicqiAWzB7axq6yUlHFZkO6MPEWFz1nLJ 5iJ/lyIpXnH6TcGwNBtZk2rM1dDKtSe5oX//WybwbyHteJirGyRr3/px4fubti1aCeUa bM9zaAXu/Jp+8tO3rfXyVIKcKx68whfQvFBDwvyYc4x7cK2iORT/7sVibzMb/HiF1BxW ZoWA== X-Gm-Message-State: AOAM5338m4okJkgg4h5PtDz4ylRatj7Ctj8ncoJt0BjFPEQJ8qXW/OkR fmHlp73V/vkhBirPxwg/4RTYlI0ey6E= X-Google-Smtp-Source: ABdhPJzGlL9VQDp9gZivopI/EDumhOTw4WwcFZBAlmjctdpC6bSjIvu66mbob5C9RLKBOvNPZH5RTw== X-Received: by 2002:ac8:4342:: with SMTP id a2mr22597625qtn.88.1592934949660; Tue, 23 Jun 2020 10:55:49 -0700 (PDT) Received: from archbookpro.phub.net.cable.rogers.com (CPEc05627352ede-CM185933998587.cpe.net.cable.rogers.com. [174.112.146.193]) by smtp.gmail.com with ESMTPSA id l2sm1380920qtc.80.2020.06.23.10.55.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jun 2020 10:55:49 -0700 (PDT) From: Denton Liu To: Git Mailing List Cc: Jeff King , Chris Torek , =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= , Junio C Hamano Subject: [PATCH v3 2/3] pkt-line: use string versions of functions Date: Tue, 23 Jun 2020 13:55:33 -0400 Message-Id: X-Mailer: git-send-email 2.27.0.307.g7979e895e7 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We have many cases where we are writing a control packet as a string constant out and we need to specify the length of the string. Currently, the length is specified as a magical `4` literal. Change these instances to use a function that calls strlen() to determine the length of the string removing the need to specify the length at all. Since these functions are inline, the strlen()s should be replaced with constants at compile-time so this should not result in any performance penalty. Signed-off-by: Denton Liu --- Notes: Junio, you mentioned in an earlier email[0] that write_str_in_full() and strbuf_addstr() each count the string length at runtime. However, I don't think that's true since they're both inline functions and strbuf_addstr() has the following comment: /** * Add a NUL-terminated string to the buffer. * * NOTE: This function will *always* be implemented as an inline or a macro * using strlen, meaning that this is efficient to write things like: * * strbuf_addstr(sb, "immediate string"); * */ so I believe that the lengths should be determined at compile-time. [0]: https://lore.kernel.org/git/xmqqeeqhxred.fsf@gitster.c.googlers.com/ pkt-line.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/pkt-line.c b/pkt-line.c index 8f9bc68ee2..e29f427b19 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -81,49 +81,61 @@ static void packet_trace(const char *buf, unsigned int len, int write) strbuf_release(&out); } +static inline void packet_trace_str(const char *buf, int write) +{ + packet_trace(buf, strlen(buf), write); +} + +#define control_packet_write(fd, s, errstr) \ + do { \ + (void)s"is a string constant"; \ + packet_trace_str(s, 1); \ + if (write_str_in_full((fd), s) < 0) \ + die_errno((errstr)); \ + } while (0) + /* * If we buffered things up above (we don't, but we should), * we'd flush it here */ void packet_flush(int fd) { - packet_trace("0000", 4, 1); - if (write_in_full(fd, "0000", 4) < 0) - die_errno(_("unable to write flush packet")); + control_packet_write(fd, "0000", _("unable to write flush packet")); } void packet_delim(int fd) { - packet_trace("0001", 4, 1); - if (write_in_full(fd, "0001", 4) < 0) - die_errno(_("unable to write delim packet")); + control_packet_write(fd, "0001", _("unable to write delim packet")); } void packet_response_end(int fd) { - packet_trace("0002", 4, 1); - if (write_in_full(fd, "0002", 4) < 0) - die_errno(_("unable to write stateless separator packet")); + control_packet_write(fd, "0002", _("unable to write stateless separator packet")); } int packet_flush_gently(int fd) { - packet_trace("0000", 4, 1); - if (write_in_full(fd, "0000", 4) < 0) + packet_trace_str("0000", 1); + if (write_str_in_full(fd, "0000") < 0) return error(_("flush packet write failed")); return 0; } +#define control_packet_buf_write(buf, s) \ + do { \ + (void)s"is a string constant"; \ + packet_trace_str(s, 1); \ + strbuf_addstr((buf), s); \ + } while (0) + void packet_buf_flush(struct strbuf *buf) { - packet_trace("0000", 4, 1); - strbuf_add(buf, "0000", 4); + control_packet_buf_write(buf, "0000"); } void packet_buf_delim(struct strbuf *buf) { - packet_trace("0001", 4, 1); - strbuf_add(buf, "0001", 4); + control_packet_buf_write(buf, "0001"); } void set_packet_header(char *buf, int size) @@ -337,15 +349,15 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer, if (len < 0) { die(_("protocol error: bad line length character: %.4s"), linelen); } else if (!len) { - packet_trace("0000", 4, 0); + packet_trace_str("0000", 0); *pktlen = 0; return PACKET_READ_FLUSH; } else if (len == 1) { - packet_trace("0001", 4, 0); + packet_trace_str("0001", 0); *pktlen = 0; return PACKET_READ_DELIM; } else if (len == 2) { - packet_trace("0002", 4, 0); + packet_trace_str("0002", 0); *pktlen = 0; return PACKET_READ_RESPONSE_END; } else if (len < 4) { From patchwork Tue Jun 23 17:55:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 11621295 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CBABF60D for ; Tue, 23 Jun 2020 17:55:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A394820781 for ; Tue, 23 Jun 2020 17:55:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DxWMRJ+O" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733156AbgFWRzx (ORCPT ); Tue, 23 Jun 2020 13:55:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733082AbgFWRzw (ORCPT ); Tue, 23 Jun 2020 13:55:52 -0400 Received: from mail-qk1-x741.google.com (mail-qk1-x741.google.com [IPv6:2607:f8b0:4864:20::741]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC8B8C061573 for ; Tue, 23 Jun 2020 10:55:52 -0700 (PDT) Received: by mail-qk1-x741.google.com with SMTP id j80so8341144qke.0 for ; Tue, 23 Jun 2020 10:55:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5sg2Q2g5qcpXMcpyJ/IaET/JU1rW991c5WwWSmLAIVM=; b=DxWMRJ+OLJRrnjkZvY48wZaKDx0K184ztkDOjEnFybUBhwaLXrtcgr8DpSdg799Q2Y tgHzezAM2q8fUhzHNEjbSK9PUwYou0/x6FrlUWc7oJ63bObnoi/z4uGP9RdEz/PuArFP sgHiHtyHIa1AfunX9Cm+B5pJsjBnmhbjEuZzC72UPkrsX1cS45jOlaaVoBQ5HSbhmYy8 U6qu2T4JaGxvrzaW4qM8GVYC3U7+Ea8vVdvPTKD8U/2PSVG6eCCTZL6bkvwYgjnPv0cu yiRfQbMeifB4Uuz+uiMLlbMa+331LSSNZq61SzVcLuqFAzgXPCWUg8fENMKXLHOfhdeA vqlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5sg2Q2g5qcpXMcpyJ/IaET/JU1rW991c5WwWSmLAIVM=; b=o86Q29V7VhZdQSWuh3Ld+WwhKNgQ75xLDyx0/hGIm9UXG9wsn9ldlgpU1eWSHmf50U c+OPmn5ZfyFpYJ+2ymDbJF8cOeh0HaDZVqSmriyj6lx5WAOb+4Hdb22Y2Swfmtw7a2Kv 82xw/LBlf//gNCSA8ZPqS84lbl3ZNlfIQMWMidgdwIvuUhYYhsmu7wXaKMzuzX95veIG PYea79Q6fqbkDcYJh5otuvXQV2zQ5U1pXThurPOsp753PkbWCIxSFSQjXeIZ+S2ra7M/ F5bmaVjtJEs5WUH3SICqTLQqNEoasJ11+AvRgLGu28X+seAgdo/c0CmUzvyJDNnG4Ymq gZCA== X-Gm-Message-State: AOAM532mIAW3mIj1UuRI2C+Q0sqXL/3zJAYNrMXMVszaYpGjcTqr5Ux4 exIt3atnW3XywoH91zsaUR6qGBZhxUs= X-Google-Smtp-Source: ABdhPJzTp4MKfY7buaO/ncfMBcAAN4cmuwH/zCRKS2UmTxplIQNIcA9kuO4igxmj1uPh/PsutC1zbg== X-Received: by 2002:a05:620a:102c:: with SMTP id a12mr21283847qkk.226.1592934951519; Tue, 23 Jun 2020 10:55:51 -0700 (PDT) Received: from archbookpro.phub.net.cable.rogers.com (CPEc05627352ede-CM185933998587.cpe.net.cable.rogers.com. [174.112.146.193]) by smtp.gmail.com with ESMTPSA id l2sm1380920qtc.80.2020.06.23.10.55.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jun 2020 10:55:50 -0700 (PDT) From: Denton Liu To: Git Mailing List Cc: Jeff King , Chris Torek , =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= , Junio C Hamano Subject: [PATCH v3 3/3] pkt-line: extract out PACKET_HEADER_SIZE Date: Tue, 23 Jun 2020 13:55:34 -0400 Message-Id: <00c19983fd99d7c335238725011e32785cb2b2a5.1592934880.git.liu.denton@gmail.com> X-Mailer: git-send-email 2.27.0.307.g7979e895e7 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In pkt-line and remote-curl, we have many instances of magic `4` literals floating around which represent the number of bytes in the packet line length header. Instead of using these magic numbers, replace them with constant expressions. In most cases, replace the `4` with `PACKET_HEADER_SIZE`. However, in the case where there's a `char array[PACKET_HEADER_SIZE]` and we are reading data into it, replace the `4` with a `sizeof(array)` so that it's clear that the logic has something to do with that array. Signed-off-by: Denton Liu --- pkt-line.c | 20 ++++++++++---------- pkt-line.h | 6 ++++-- remote-curl.c | 29 +++++++++++++++-------------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/pkt-line.c b/pkt-line.c index e29f427b19..d451f0b67c 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -165,7 +165,7 @@ static void format_packet(struct strbuf *out, const char *prefix, die(_("protocol error: impossibly long line")); set_packet_header(&out->buf[orig_len], n); - packet_trace(out->buf + orig_len + 4, n - 4, 1); + packet_trace(out->buf + orig_len + PACKET_HEADER_SIZE, n - PACKET_HEADER_SIZE, 1); } static int packet_write_fmt_1(int fd, int gently, const char *prefix, @@ -211,13 +211,13 @@ static int packet_write_gently(const int fd_out, const char *buf, size_t size) static char packet_write_buffer[LARGE_PACKET_MAX]; size_t packet_size; - if (size > sizeof(packet_write_buffer) - 4) + if (size > sizeof(packet_write_buffer) - PACKET_HEADER_SIZE) return error(_("packet write failed - data exceeds max packet size")); packet_trace(buf, size, 1); - packet_size = size + 4; + packet_size = size + PACKET_HEADER_SIZE; set_packet_header(packet_write_buffer, packet_size); - memcpy(packet_write_buffer + 4, buf, size); + memcpy(packet_write_buffer + PACKET_HEADER_SIZE, buf, size); if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0) return error(_("packet write failed")); return 0; @@ -325,7 +325,7 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size, return ret; } -int packet_length(const char lenbuf_hex[4]) +int packet_length(const char lenbuf_hex[PACKET_HEADER_SIZE]) { int val = hex2chr(lenbuf_hex); return (val < 0) ? val : (val << 8) | hex2chr(lenbuf_hex + 2); @@ -337,9 +337,9 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer, int options) { int len; - char linelen[4]; + char linelen[PACKET_HEADER_SIZE]; - if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) { + if (get_packet_data(fd, src_buffer, src_len, linelen, sizeof(linelen), options) < 0) { *pktlen = -1; return PACKET_READ_EOF; } @@ -360,11 +360,11 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer, packet_trace_str("0002", 0); *pktlen = 0; return PACKET_READ_RESPONSE_END; - } else if (len < 4) { + } else if (len < PACKET_HEADER_SIZE) { die(_("protocol error: bad line length %d"), len); } - len -= 4; + len -= sizeof(linelen); if ((unsigned)len >= size) die(_("protocol error: bad line length %d"), len); @@ -382,7 +382,7 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer, if ((options & PACKET_READ_DIE_ON_ERR_PACKET) && starts_with(buffer, "ERR ")) - die(_("remote error: %s"), buffer + 4); + die(_("remote error: %s"), buffer + PACKET_HEADER_SIZE); *pktlen = len; return PACKET_READ_NORMAL; diff --git a/pkt-line.h b/pkt-line.h index 5b373fe4cd..d6121b8044 100644 --- a/pkt-line.h +++ b/pkt-line.h @@ -5,6 +5,8 @@ #include "strbuf.h" #include "sideband.h" +#define PACKET_HEADER_SIZE 4 + /* * Write a packetized stream, where each line is preceded by * its length (including the header) as a 4-byte hex number. @@ -82,7 +84,7 @@ int packet_read(int fd, char **src_buffer, size_t *src_len, char * If lenbuf_hex contains non-hex characters, return -1. Otherwise, return the * numeric value of the length header. */ -int packet_length(const char lenbuf_hex[4]); +int packet_length(const char lenbuf_hex[PACKET_HEADER_SIZE]); /* * Read a packetized line into a buffer like the 'packet_read()' function but @@ -211,7 +213,7 @@ enum packet_read_status packet_reader_peek(struct packet_reader *reader); #define DEFAULT_PACKET_MAX 1000 #define LARGE_PACKET_MAX 65520 -#define LARGE_PACKET_DATA_MAX (LARGE_PACKET_MAX - 4) +#define LARGE_PACKET_DATA_MAX (LARGE_PACKET_MAX - PACKET_HEADER_SIZE) extern char packet_buffer[LARGE_PACKET_MAX]; struct packet_writer { diff --git a/remote-curl.c b/remote-curl.c index a2cbef546b..c99a2a514a 100644 --- a/remote-curl.c +++ b/remote-curl.c @@ -536,8 +536,8 @@ struct rpc_state { unsigned initial_buffer : 1; /* - * Whenever a pkt-line is read into buf, append the 4 characters - * denoting its length before appending the payload. + * Whenever a pkt-line is read into buf, append the PACKET_HEADER_SIZE + * characters denoting its length before appending the payload. */ unsigned write_line_lengths : 1; @@ -556,8 +556,9 @@ struct rpc_state { * rpc->buf and rpc->len if there is enough space. Returns 1 if there was * enough space, 0 otherwise. * - * If rpc->write_line_lengths is true, appends the line length as a 4-byte - * hexadecimal string before appending the result described above. + * If rpc->write_line_lengths is true, appends the line length as a + * PACKET_HEADER_SIZE-byte hexadecimal string before appending the result + * described above. * * Writes the total number of bytes appended into appended. */ @@ -569,8 +570,8 @@ static int rpc_read_from_out(struct rpc_state *rpc, int options, int pktlen_raw; if (rpc->write_line_lengths) { - left = rpc->alloc - rpc->len - 4; - buf = rpc->buf + rpc->len + 4; + left = rpc->alloc - rpc->len - PACKET_HEADER_SIZE; + buf = rpc->buf + rpc->len + PACKET_HEADER_SIZE; } else { left = rpc->alloc - rpc->len; buf = rpc->buf + rpc->len; @@ -582,7 +583,7 @@ static int rpc_read_from_out(struct rpc_state *rpc, int options, *status = packet_read_with_status(rpc->out, NULL, NULL, buf, left, &pktlen_raw, options); if (*status != PACKET_READ_EOF) { - *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0); + *appended = pktlen_raw + (rpc->write_line_lengths ? PACKET_HEADER_SIZE : 0); rpc->len += *appended; } @@ -593,7 +594,7 @@ static int rpc_read_from_out(struct rpc_state *rpc, int options, die(_("shouldn't have EOF when not gentle on EOF")); break; case PACKET_READ_NORMAL: - set_packet_header(buf - 4, *appended); + set_packet_header(buf - PACKET_HEADER_SIZE, *appended); break; case PACKET_READ_DELIM: memcpy(buf - strlen("0001"), "0001", strlen("0001")); @@ -682,7 +683,7 @@ static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp) #endif struct check_pktline_state { - char len_buf[4]; + char len_buf[PACKET_HEADER_SIZE]; int len_filled; int remaining; }; @@ -691,7 +692,7 @@ static void check_pktline(struct check_pktline_state *state, const char *ptr, si { while (size) { if (!state->remaining) { - int digits_remaining = 4 - state->len_filled; + int digits_remaining = sizeof(state->len_buf) - state->len_filled; if (digits_remaining > size) digits_remaining = size; memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining); @@ -699,16 +700,16 @@ static void check_pktline(struct check_pktline_state *state, const char *ptr, si ptr += digits_remaining; size -= digits_remaining; - if (state->len_filled == 4) { + if (state->len_filled == sizeof(state->len_buf)) { state->remaining = packet_length(state->len_buf); if (state->remaining < 0) { die(_("remote-curl: bad line length character: %.4s"), state->len_buf); } else if (state->remaining == 2) { die(_("remote-curl: unexpected response end packet")); - } else if (state->remaining < 4) { + } else if (state->remaining < sizeof(state->len_buf)) { state->remaining = 0; } else { - state->remaining -= 4; + state->remaining -= sizeof(state->len_buf); } state->len_filled = 0; } @@ -1469,7 +1470,7 @@ int cmd_main(int argc, const char **argv) parse_fetch(&buf); } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) { - int for_push = !!strstr(buf.buf + 4, "for-push"); + int for_push = !!strstr(buf.buf + PACKET_HEADER_SIZE, "for-push"); output_refs(get_refs(for_push)); } else if (starts_with(buf.buf, "push ")) {