From patchwork Tue Dec 7 17:45:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662355 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26E31C433EF for ; Tue, 7 Dec 2021 17:45:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240151AbhLGRs7 (ORCPT ); Tue, 7 Dec 2021 12:48:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235371AbhLGRs7 (ORCPT ); Tue, 7 Dec 2021 12:48:59 -0500 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56BD6C061746 for ; Tue, 7 Dec 2021 09:45:28 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id j3so31179471wrp.1 for ; Tue, 07 Dec 2021 09:45:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=4caTpC3+u83vTS7zkUHAtzr+4yoHPBYES+caC91RmF4=; b=a2dxhXN6orm9sJI4kDhmpwxrEQQVoBlS1kp5gKf3u5AiPzDxzhguwj+AGwB8v9gsvL zJYCl7rOAWoZYy0sLno81oG3sZ4NXLtURI4PBbOXaNWmYCQMfdAhqM9JfF+IilAVIBn8 5aF0blvLSyOUr2yfnhJ+0WOispKWuk7mS/sAolKggp4g9nQoENfa14ju06pFIzFDJaPv J/xWyKZ/4JoKTaBE7z2AzeCJwFOoPyqst2PP9nzOOkLBRCeqUk3d6BZjsGpFCmUeaHrX J/HFaEwIoYGxYmgt7wder72tKQNFWb8F5sx2wwNQcxFz3/1ylQj8J7IqVr1TxOyP5tFS /2Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=4caTpC3+u83vTS7zkUHAtzr+4yoHPBYES+caC91RmF4=; b=zjILVqXkukrNcD1CeYdJb7O+8HLV/oqfOPGDvqQcZFu1nwpL7yBxwOtTaSEgr/clkr 4qhw9y0U/xcF+MVklloqG/I0PWr2GljZUIax+0dTb1j6/aC9jmuKpynxtTkVMpQyKa6Y UXLE6w3/N94n0JAoJS4q5Ais7JYLx71IxJhkM6gBYY2+ZPuqHpfn77FrOMAYgxRa+eUi UHr609tD/PX8n7TBumzHh0MowSA/yi0vjYkU6lvBe3KH493cfXCB+wA9llXsh981RCJI /IHd5Sfupoj57d1vyPdfRc4z/m4Bl8aepOy8JpeKfPrl35f4G5iOuqrhUM1mqxol8zkY AAtQ== X-Gm-Message-State: AOAM531HqcLUfq0ADPN5Kv1rsq1CkbuavcpRIkCjrMRupSliyLXpjxqD U25VwRkRFCIRm/hoQjLiTxBL9foPdTk= X-Google-Smtp-Source: ABdhPJxiQ4dQLG2XLno3+sxH4r9gWVNzLCEVqlRy/zDyHoHV+/5QJvMURoonioyBSoi+hbqnMEqXYQ== X-Received: by 2002:a05:6000:1c2:: with SMTP id t2mr51075456wrx.378.1638899126806; Tue, 07 Dec 2021 09:45:26 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o12sm344033wmq.12.2021.12.07.09.45.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:26 -0800 (PST) Message-Id: <7c033815183cc8ad7a76bc2b9456b17041107c2c.1638899124.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:15 +0000 Subject: [PATCH 01/10] reftable: fix OOB stack write in print functions Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Signed-off-by: Han-Wen Nienhuys --- reftable/record.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 6a5dac32dc6..8536bd03aa9 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -254,7 +254,7 @@ static void hex_format(char *dest, uint8_t *src, int hash_size) void reftable_ref_record_print(struct reftable_ref_record *ref, uint32_t hash_id) { - char hex[2 * GIT_SHA256_RAWSZ + 1] = { 0 }; /* BUG */ + char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */ printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index); switch (ref->value_type) { case REFTABLE_REF_SYMREF: @@ -586,7 +586,7 @@ static struct reftable_record_vtable reftable_obj_record_vtable = { void reftable_log_record_print(struct reftable_log_record *log, uint32_t hash_id) { - char hex[GIT_SHA256_RAWSZ + 1] = { 0 }; + char hex[GIT_MAX_HEXSZ + 1] = { 0 }; switch (log->value_type) { case REFTABLE_LOG_DELETION: From patchwork Tue Dec 7 17:45:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662357 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F57BC433FE for ; Tue, 7 Dec 2021 17:45:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240155AbhLGRtA (ORCPT ); Tue, 7 Dec 2021 12:49:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240150AbhLGRs7 (ORCPT ); Tue, 7 Dec 2021 12:48:59 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B6F6C061574 for ; Tue, 7 Dec 2021 09:45:29 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id t9so31088712wrx.7 for ; Tue, 07 Dec 2021 09:45:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=2mX0ewo9yheSDENuy2H8jhMj0liBRlVQnqnpNtjTveM=; b=dG8yyqYk7p/QXxaPu858F0ph1tKSp14hAdLeMa/yKB2a3m2bv2ftlMOOogPxSr/NVh 7z43oLGmlt00DHPd59igi7OdYTMK7QM0yZ1yDF4zo8c9fiRQMpst/1XxH+vAVwAS1M4s JF2mb5gWFHDNDNzSUU1Xg04l0V6H71/im2Q2sW+whWqROM5LS8Q3Wf7GZbWoifp0xpHV 1rbck1yWiEWOpvFlnG2DASlWklQZcBCwVTE8kUL4vsR0wxI/zotLZn1uuHjQelusP/CA EHYv+c6rLG2e7VKpP/eHBIAo0oPHuekUIZbpK2so8O54RmCnEi5NpN7w7NTUtPqvlVAW spoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=2mX0ewo9yheSDENuy2H8jhMj0liBRlVQnqnpNtjTveM=; b=k4L+isMCodX4JqQrLZlnujlaOYaTBrcSQqKtOZZK0BMK7EEPHnF4MDXfnqsWp/2OtC xzcOl3F7Af9ONvM9O0bV7pjJVfv/oujtm1d68osDTpzC5f594Ycl5ZrOYc4X5HeXQcab nQbTe3x17l6RFy2c2mjQ0SgDJLt2QpHEjnD4+yUtuEsQDV5szvQbIDv2sU7m9RQ+Zuoi aZUpL9zwTMWtwxYdzmvmM2WIfo7Znk+s7LwQBwGfKsjxZjlh34N2MWnIYIc0dYDvaYnK nLmo/hrBXJTkVdQM8ThTbV4xhVwTD1db4g6pfxyQENvkg9JKIqkJBRzrKbZP+i9EibWQ N9jg== X-Gm-Message-State: AOAM530gK6WkNryocVJ5l1878XUsXvxS2/I+jfrAWmyJFpLijA8POPFO HsZxKh/ZGvI2tlxxaeRVMFsAoEhP0KU= X-Google-Smtp-Source: ABdhPJwnYzhR07GR3Z9SkhWj85goIBQu/gI23uqbepHt5xWtl6TvrJFxffF6mpZVy0vbXGMdsCxXjA== X-Received: by 2002:a05:6000:2c8:: with SMTP id o8mr6027381wry.266.1638899127535; Tue, 07 Dec 2021 09:45:27 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j8sm354651wrh.16.2021.12.07.09.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:27 -0800 (PST) Message-Id: <603bd1d4f6e4ceda02a5165eedd3ae5b92b8929e.1638899124.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:16 +0000 Subject: [PATCH 02/10] reftable: fix resource leak in error path Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This would be triggered by corrupt files, so it doesn't have test coverage. This was discovered by a Coverity scan. Signed-off-by: Han-Wen Nienhuys --- reftable/block.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 855e3f5c947..d7347bb3152 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -188,13 +188,16 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, uint32_t full_block_size = table_block_size; uint8_t typ = block->data[header_off]; uint32_t sz = get_be24(block->data + header_off + 1); - + int err = 0; uint16_t restart_count = 0; uint32_t restart_start = 0; uint8_t *restart_bytes = NULL; + uint8_t *uncompressed = NULL; - if (!reftable_is_block_type(typ)) - return REFTABLE_FORMAT_ERROR; + if (!reftable_is_block_type(typ)) { + err = REFTABLE_FORMAT_ERROR; + goto done; + } if (typ == BLOCK_TYPE_LOG) { int block_header_skip = 4 + header_off; @@ -213,15 +216,19 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, uncompress2(uncompressed + block_header_skip, &dst_len, block->data + block_header_skip, &src_len)) { reftable_free(uncompressed); - return REFTABLE_ZLIB_ERROR; + err = REFTABLE_ZLIB_ERROR; + goto done; } - if (dst_len + block_header_skip != sz) - return REFTABLE_FORMAT_ERROR; + if (dst_len + block_header_skip != sz) { + err = REFTABLE_FORMAT_ERROR; + goto done; + } /* We're done with the input data. */ reftable_block_done(block); block->data = uncompressed; + uncompressed = NULL; block->len = sz; block->source = malloc_block_source(); full_block_size = src_len + block_header_skip; @@ -251,7 +258,11 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, br->restart_count = restart_count; br->restart_bytes = restart_bytes; - return 0; +done: + if (uncompressed) { + reftable_free(uncompressed); + } + return err; } static uint32_t block_reader_restart_offset(struct block_reader *br, int i) From patchwork Tue Dec 7 17:45:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662359 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D1FCC433EF for ; Tue, 7 Dec 2021 17:45:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240174AbhLGRtB (ORCPT ); Tue, 7 Dec 2021 12:49:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235371AbhLGRtA (ORCPT ); Tue, 7 Dec 2021 12:49:00 -0500 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC029C061746 for ; Tue, 7 Dec 2021 09:45:29 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id d24so31274917wra.0 for ; Tue, 07 Dec 2021 09:45:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=zeOacPG8foepzoe93AsPL3Dhk+VXAN8S9blbgW2/nvM=; b=NnjPRbF6qQrn29yw9QZ2+8Dm2ZkZ8LohFKHCms32wj3lrqmyb0yc3j61Av7Lf8BfN2 MaAB4AKVRRr9Zanfmw9Yy73xOoOHzzfWgLechGYUFrryr0bveKRt/n9rbjeVMhSSUBvf uTepn0klt2jXfRFXQL/IeMnCa4fMBRYEYYvUtkTTH5dfbsFFACW/9wYQpK6+trpaTv+d M/oRVeWiGPO7NdZjOEK2Sr0ChtgLa/5YxfLxLPelUtjS3lr5iAguuV03JzT1EetbBcfB tLuA+Q3oN6vJXczmehXIdN3gxJlU9fTglaRy2EFLjREKmLCXIHM8ZMm65f7IcM5IiXyc Bm1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=zeOacPG8foepzoe93AsPL3Dhk+VXAN8S9blbgW2/nvM=; b=aDEzDo9iOHcf2rlUHhQ04N/ak6BPdeaMIip6tSeUtQZOpd8Qf93DnoeWv39xDvkVJx 7H74jFVAJLtX2M+bsf0rIehiJQc4J8lLFfGkm9NsJd2I/ZydB6ph03k0a91BBoO2t46U S8DpOHULyvcLCuY2xFNuqY29f/ghdMNuY6Uo1xGUSO7/L3Dy+zlxwF15iEG9LP7pAL7u nifa5hubMBnfCI653zoFfvPfzhZTsBbiHLytXQYefqZsDF3WOAyYNujgzMaQuo+jPdMz T1pECXc5scxi+WT3Y77iKoG+5qrNQsHsCPo1Irvm8awobYTJNIh6Gwx8MYbL0BIITF+u 8YKA== X-Gm-Message-State: AOAM531nqzeaycc9gWAzBr/GyiEvv+P7LObFakFXkJ3Q6bwSVSpbxrwl RIT5tt5mxMVOzpA7kglmB59D6Ge+PJE= X-Google-Smtp-Source: ABdhPJzOTiRaX75qcA6b68L9KX1OyURB3V+Mwm7B+YMdcU5s1cAL10LPlDamx8dS3C+oZ+msUrlz4g== X-Received: by 2002:adf:b355:: with SMTP id k21mr50977586wrd.451.1638899128138; Tue, 07 Dec 2021 09:45:28 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t8sm299176wmq.32.2021.12.07.09.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:27 -0800 (PST) Message-Id: <97279040dded376c110299cc8f2a6a9f7fc4950a.1638899124.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:17 +0000 Subject: [PATCH 03/10] reftable: fix resource leak blocksource.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This would be triggered in the unlikely event of fstat() failing on an opened file. Signed-off-by: Han-Wen Nienhuys --- reftable/blocksource.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/reftable/blocksource.c b/reftable/blocksource.c index 0044eecd9aa..2605371c28d 100644 --- a/reftable/blocksource.c +++ b/reftable/blocksource.c @@ -134,8 +134,10 @@ int reftable_block_source_from_file(struct reftable_block_source *bs, } err = fstat(fd, &st); - if (err < 0) - return -1; + if (err < 0) { + close(fd); + return REFTABLE_IO_ERROR; + } p = reftable_calloc(sizeof(struct file_block_source)); p->size = st.st_size; From patchwork Tue Dec 7 17:45:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662361 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62B5EC433F5 for ; Tue, 7 Dec 2021 17:45:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240156AbhLGRtD (ORCPT ); Tue, 7 Dec 2021 12:49:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240150AbhLGRtB (ORCPT ); Tue, 7 Dec 2021 12:49:01 -0500 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60F8FC061574 for ; Tue, 7 Dec 2021 09:45:30 -0800 (PST) Received: by mail-wr1-x42b.google.com with SMTP id t9so31088823wrx.7 for ; Tue, 07 Dec 2021 09:45:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=6v0+JuvgL+rkV6+ZUY0aCiH0eSlBA7tjh6rJWXdXHEw=; b=DP5x+9ZJT8qMmvm2TnbH+JxFgHH9ct5RJc3P7Vzz8vvuQ6vV26pIaAJXqG8RsypIOw dPHBmltazakLXmHb8keCv2Cx4wgYhD7zoIcVBSv9UxacBgkzfMk1t7rGipBOReFvq9t2 kFOQv796gjF3xqurZYPeeK6w3eKRXFrg1ltRTO2LgevkEF8KDn/6OzKosx0UYHgCknVt 41vRNZgDyjlCmMmS0ShZxk1cEi3TshdiEwADuuzlGM1XxNvjQ4QSb7UgDDZ6uNPnnfoD HWoLP+zEwtI6lurVm6/8QXeiYBFce5pvJ3xAr6MgXQx8xKWiazsPfj6xIJhwD1G6oOxs yeZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=6v0+JuvgL+rkV6+ZUY0aCiH0eSlBA7tjh6rJWXdXHEw=; b=GulLa/9tcng6N3PPt++xetPJCHGwsVVYs5l+xyCv1fZ2veZrTm3m/nFtYiWohW8/Px lHPFOb2mwaFiLO3l+5IarbK2cgvtBl3rfmobjo2R4CKPFttOSaJj5UgBr8kwilvjPpA/ qoARLzCFW7gT4UEZKb9qfOudGplbbnaaQE+VU5uQz8L0ij/9yaMXl+wvz8JgkV48WjG8 WK9aHNIgQpKo151OGz4zofzdnbgs9TWl6yepdZPXm1pu4ps0YDUGUcXETh9LP/4mytFo EQLvCRU2VRTg9qjMwUp6WLCP2fOCmjqEpskXt4Mo9QnN+Xm31K7nfFUYSOeup1XTfDWW WpLA== X-Gm-Message-State: AOAM530Hm7Ec00jqbxmHCVSOexIJoh+HMLeoft0lxilmkjwBZ8ITqi1n gfFip/cwHY/gGi939OKge3wzEGNP5P8= X-Google-Smtp-Source: ABdhPJz+kmtvwerSRBp2ToAABsjZIgrXja2fPflSA4qQSu/2VnP4ebkeDQIt/gSvcavjGbDon6ICTQ== X-Received: by 2002:adf:f486:: with SMTP id l6mr54059735wro.596.1638899128887; Tue, 07 Dec 2021 09:45:28 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l1sm366455wrn.15.2021.12.07.09.45.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:28 -0800 (PST) Message-Id: <9288289ebcb7f7db0bfeef80f3150e1205dc1d62.1638899124.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:18 +0000 Subject: [PATCH 04/10] reftable: check reftable_stack_auto_compact() return value Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Fixes a problem detected by Coverity. Signed-off-by: Han-Wen Nienhuys --- reftable/stack_test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/reftable/stack_test.c b/reftable/stack_test.c index eb0b7228b0c..d628420e63a 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -814,6 +814,7 @@ static void test_reftable_stack_auto_compaction(void) EXPECT_ERR(err); err = reftable_stack_auto_compact(st); + EXPECT_ERR(err); EXPECT(i < 3 || st->merged->stack_len < 2 * fastlog2(i)); } From patchwork Tue Dec 7 17:45:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662363 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 264C9C433FE for ; Tue, 7 Dec 2021 17:45:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240180AbhLGRtE (ORCPT ); Tue, 7 Dec 2021 12:49:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240170AbhLGRtB (ORCPT ); Tue, 7 Dec 2021 12:49:01 -0500 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BBE0C061746 for ; Tue, 7 Dec 2021 09:45:31 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id c4so31064229wrd.9 for ; Tue, 07 Dec 2021 09:45:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=J9jVqwMPaNdDSNjujdVEAD8serufDJ5UG7qKldS2NTo=; b=a7EgfAD1GeruV1R0AKwhqy5BFYsoslO+w/oz3dSC8GEvC5yjsSXUjWpGE4XxLIOLtJ IY0CfPn/mxTqjYCWDIygbppwgRjCIeTW+31W+XqMJpr5JLEaPkR8b3KmQnHxLUHCcgQ/ cllDr/Ld2a8DO95GqnDs2pKBritrw1u9eRzJtP6wDFTZJT6j4dPtVO2Qzi9Szl2GnieR L5v6UxffpbruAqvt/NYgfA+ap+yrldT/mgz24XOsbXo6OcPmstWWsRTqCTG7BEnOi6VX U9/VyBrmxQ9QZ+/abYH2K3QXdg1EqpRSISMDA1LBG/7McHSZPFP8xGCFVufWYSatld/e Svnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=J9jVqwMPaNdDSNjujdVEAD8serufDJ5UG7qKldS2NTo=; b=ebrRxVqBTAKKiIpJOzSblAnIfavbycUiALcWEzC6mWN7AEV++jlf6r3iO2ZvCqK61V N9hojFttJxo0PSUJABlvVvXB/wKspstswf8jfoIL0GhaPJXXvRTPNuIePHEUotL+nfSc 1IRMRMAo8sGd78OSAeeP1ln9hYRhF/os4GgQEbNGte4bhSc2gZTmBMZ2nr6I+TOVcxvW AwcRXO487oAsKt9ztHeHCz05AokaIsjuFJdJgW+1igCgwIzDcx8oKmnU0JjuUKXG88SJ Dxm/96t5tb5KuC8yUpIdnwA0mRudZgyZNwinXHUv4XPlJ4zW2i7bWBLV0tBVfhCaSh8B 9USg== X-Gm-Message-State: AOAM532PBBmVlLJf0kLUSl8kyjuJZbZ0FJVhetvLJZIN/PYZy7c8hUnn uzz7gmHokQIQHBNPCh+eZLIIakkQ9lw= X-Google-Smtp-Source: ABdhPJwl9uVFKu/68YOXGJn4ZyZrtXwQlE+CzKbS89WKcEGD0DiJUDg7roA+uggSTz2vhImJ9OaIgg== X-Received: by 2002:adf:cc91:: with SMTP id p17mr53163373wrj.589.1638899129503; Tue, 07 Dec 2021 09:45:29 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id k37sm273699wms.21.2021.12.07.09.45.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:29 -0800 (PST) Message-Id: <9269977a1c3c2654afbc6ae18e9bb442fbc7968a.1638899124.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:19 +0000 Subject: [PATCH 05/10] reftable: ignore remove() return value in stack_test.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys If the cleanup fails, there is nothing we can do. Signed-off-by: Han-Wen Nienhuys --- reftable/stack_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reftable/stack_test.c b/reftable/stack_test.c index d628420e63a..4b7292945c3 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -89,7 +89,7 @@ static void test_read_file(void) EXPECT(0 == strcmp(want[i], names[i])); } free_names(names); - remove(fn); + (void) remove(fn); } static void test_parse_names(void) From patchwork Tue Dec 7 17:45:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662365 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7AA7C4332F for ; Tue, 7 Dec 2021 17:45:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240187AbhLGRtF (ORCPT ); Tue, 7 Dec 2021 12:49:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240176AbhLGRtC (ORCPT ); Tue, 7 Dec 2021 12:49:02 -0500 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E07A0C061756 for ; Tue, 7 Dec 2021 09:45:31 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id o19-20020a1c7513000000b0033a93202467so36378wmc.2 for ; Tue, 07 Dec 2021 09:45:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=HOTpe9FNENotMYcACPCrU+33ZDetlyOQNtsGAWihsVI=; b=cYtGNmqb3bs3omc8lLO/fQe6rGR1BrzOqAdARb2HYxVXBVUXTztvbyM/QoFedjcdSI 9Cu/N66SQ2LnFjG9k5edRH2BFSZLAyB8gflaOmjN7Kq4U/XdTr56uR6Xv0oaK+SIVRL5 yWbBf6uyklnOSZBIxY+dBVbTBtZ33TU2WOvyy2AYFdOYDJdWJYZMPOhywYj/ldHU+Ulz p47OVQN5roGIMR0OL5XRBvxQeNEi+KvOzpCqMCoTiU6g7fQnPrfyi5I/sif963C8E3y4 rmF8pP4P2Z7pHj6J64yKsdYxS1Mol1BBwlUdmjVbaIgct/SQs1e1slz27SRg8dxBcGGh WhWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=HOTpe9FNENotMYcACPCrU+33ZDetlyOQNtsGAWihsVI=; b=RuKD9k5l1ec+TlnmUlnXNNB78mVkqpv3q6zAMotr56QBtwPYeHeobSxbL3msNI+Ahe uHcbs4F1rWj/PVPgzEooCKtO8BgCTzvmwIhBd595Tm3TUHlM0DTlwT/AWw9MSHZZmHc1 wobriszRMocgAcstkded3mheIBjPvCStGmuGfubFjXj480kNd3kPWjfmbG9BS+iLiOGJ MdCWFmMDBmgnqcLMvn++9vtsJwYKJNotqGj3MslVhBZaGWl1/5NbN5DAgM6Tc0As+ebW v3AXmXf0AnR/WkyShPAn7FllxlxDZrWw7pZdCIPYB0PoU2OczxZFUgrvyZVbY/3xQ6ke 6FZg== X-Gm-Message-State: AOAM531VtQDzDej/eCUMm4LsCgji6o/s3yOVYBrAm0GVqU/WXNjg4+uP j96OyVncbJQnHbVnfv8TxGTViH+Mh2Y= X-Google-Smtp-Source: ABdhPJzCEjrDOY5fd3GAOrv2a52UZWRdQS7wIjeWqygICqZ3owkHfEJfZ1olo3i/vy7x/3Hh0yIemg== X-Received: by 2002:a1c:4d3:: with SMTP id 202mr8750534wme.174.1638899130252; Tue, 07 Dec 2021 09:45:30 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id x13sm364720wrr.47.2021.12.07.09.45.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:29 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:20 +0000 Subject: [PATCH 06/10] reftable: fix resource warning Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This would trigger in the unlikely event that we are compacting, and the next available file handle is 0. Signed-off-by: Han-Wen Nienhuys --- reftable/stack.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index df5021ebf08..10dfd370e8e 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -877,7 +877,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, struct strbuf new_table_path = STRBUF_INIT; int err = 0; int have_lock = 0; - int lock_file_fd = 0; + int lock_file_fd = -1; int compact_count = last - first + 1; char **listp = NULL; char **delete_on_success = @@ -911,7 +911,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, } /* Don't want to write to the lock for now. */ close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; have_lock = 1; err = stack_uptodate(st); @@ -932,7 +932,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, sublock_file_fd = open(subtab_lock.buf, O_EXCL | O_CREAT | O_WRONLY, 0644); - if (sublock_file_fd > 0) { + if (sublock_file_fd >= 0) { close(sublock_file_fd); } else if (sublock_file_fd < 0) { if (errno == EEXIST) { @@ -1013,7 +1013,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, goto done; } err = close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; if (err < 0) { err = REFTABLE_IO_ERROR; unlink(new_table_path.buf); @@ -1050,9 +1050,9 @@ done: listp++; } free_names(subtable_locks); - if (lock_file_fd > 0) { + if (lock_file_fd >= 0) { close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; } if (have_lock) { unlink(lock_file_name.buf); From patchwork Tue Dec 7 17:45:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662367 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9266FC43217 for ; Tue, 7 Dec 2021 17:45:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240195AbhLGRtG (ORCPT ); Tue, 7 Dec 2021 12:49:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240175AbhLGRtD (ORCPT ); Tue, 7 Dec 2021 12:49:03 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 909CAC061748 for ; Tue, 7 Dec 2021 09:45:32 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id u1so31049543wru.13 for ; Tue, 07 Dec 2021 09:45:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=rJ3GDWKJ0GCH/2sQjyhi9Wp9eandivSrWcGSEkqQkNE=; b=lBlDuLugrAMpyMH0oRDVn82Kaz+ObDJxNnCVFH2cyAHirsGfWwjJwwBPimNPZG2B23 /Wp4tILzoasQ3mtzWSws0IoEgoq4qnNu3Nx/zoGWkMlifeGDRpIPsHVzkN1yoe8Q7jK1 yzSQo+U4mpXR+AUHlwLOSL63cF1Vx6ZkHRakOwJkPWc/rjnkEttAgja/meq5I8YwnUH+ TrquB2rwECiqJo2ItkCunz3pIiXyQba0+LXgY9c0KLROc333vPqlH4C5INHGhdoKWXue DqtWS5eA6jJhi3kNn3jEAuPU5cBbtMvRGSNrN24EjEt1MfNoov84qmQD4DbupqzUvXpr 2YWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=rJ3GDWKJ0GCH/2sQjyhi9Wp9eandivSrWcGSEkqQkNE=; b=05LRqGs3WvRIRqvY9HL0DJIlIqjfNWxr+0aDnZOmSAytc4oTlxImFNuRAK+nA+H3Yg x+wyv2QeJBuW8cpk4wPZug804H6KCH0Ehs+nXMCUy+K/TGDkBr4gqLwz59MmoIfXrM0u /rQhpta9nH/7sdXBizw4ph6PIXAE7HK9d0AS3F1h0Z4SeEyqxlwJzIcn8r5OEEHF3b9Y BSbGUsT+Cilz/CZTD7jmmmb2vqv3wXk7R+geqhiTozU7X8+9Jj6l0V+0SBRz73+qDlUj S1YlsROP35AsjSVeQut7nXQgwJWzuO11BdOtMTT3M0iqc9ZhiBL7Ro+rsYd+VBYAvCrn gt5Q== X-Gm-Message-State: AOAM533iICZOuIKKK4dKOzcXlC+nfXBsg0ikkdTtSJMS23jWp+y1VEv3 gLE2WUVb7Ym1N37Ilj/DLQBMeftA/VE= X-Google-Smtp-Source: ABdhPJwAnAzxOGrXnJJ77v/nbX7YJhYyTBaTs3AGE3LjRDO8i2P1sr5brGQrFjabhPywmDvENagJ3A== X-Received: by 2002:a5d:4575:: with SMTP id a21mr54398410wrc.193.1638899130996; Tue, 07 Dec 2021 09:45:30 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id e18sm369293wrs.48.2021.12.07.09.45.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:30 -0800 (PST) Message-Id: <47fef87eb4a614fb53d703152079cdd64b623814.1638899124.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:21 +0000 Subject: [PATCH 07/10] reftable: fix NULL derefs in error paths Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Spotted by Coverity. Signed-off-by: Han-Wen Nienhuys --- reftable/reader.c | 3 ++- reftable/stack.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/reftable/reader.c b/reftable/reader.c index 006709a645a..8d308d858f8 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -796,6 +796,7 @@ int reftable_reader_print_file(const char *tablename) reftable_table_from_reader(&tab, r); err = reftable_table_print(&tab); done: - reftable_reader_free(r); + if (r) + reftable_reader_free(r); return err; } diff --git a/reftable/stack.c b/reftable/stack.c index 10dfd370e8e..eb03b6c74f6 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -707,7 +707,8 @@ done: strbuf_release(&temp_tab_file_name); strbuf_release(&tab_file_name); strbuf_release(&next_name); - reftable_writer_free(wr); + if (wr) + reftable_writer_free(wr); return err; } From patchwork Tue Dec 7 17:45:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662369 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE094C433F5 for ; Tue, 7 Dec 2021 17:45:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240197AbhLGRtG (ORCPT ); Tue, 7 Dec 2021 12:49:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240178AbhLGRtD (ORCPT ); Tue, 7 Dec 2021 12:49:03 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B3B4C061574 for ; Tue, 7 Dec 2021 09:45:33 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id o13so31031698wrs.12 for ; Tue, 07 Dec 2021 09:45:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=MXMomjRC/fb24dxRgX+qEsh4KebnnV6VgDOcUfwXPBo=; b=DmRDM8/jnDMOmgmzG34dNlvlXujeJOtifaEQaQceWfHEKgu6KMxHsiCIp3axHva9MZ XxqLWpmM1bJLnDeaXhFU8HSkWH1e1saYELkLgfeBg8Fv+9ScLiK25oYW26Eb2tVFEd4q lKlZSWwlaw3DeWPGiJ6BvPRvTiHQ74qPd3CR7kiyD1QEceabkHN1Z3YtjzTqDfMEWTy4 xbvljZxxDAphl+RDDdlaYQWDwLjUdJI8BoHkaYtlSzyfqXJ91c4av0s7DkSVRhGlXNv7 OgJAhon/X5OYfwtcexUYqQSsWQCvVw6B6giPshHV/wCpLoJgdEHb4KAXi5k7tCCS3mRP C7HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=MXMomjRC/fb24dxRgX+qEsh4KebnnV6VgDOcUfwXPBo=; b=M08Teodig2mNchaw5CTZ4hn3qVYzTlfmRp8kGMa0Q4WUe7DDAcIf4eqmG45m3wco3Z QAAgZ0sFjIUwqPjdp5G14q9iipd5oMGgNb6ArtA0PSkyX2s0HTxxezQqqBNTk0AndcyQ DPL8pRLjDvm0xWr0QIFZ5slgAAm0DyTpwGHKb4zRuF9zpO6pRzoGaigNKxpeOrbH4dZ3 EWwc0qMaqyP2mz6Oj4Jv1m2t/z4j0ObCE8JiPyTQnCe3S20wkTGlLw8e3RoXXZ741m9J jsIlEB5T3dKHh3vzo1iipb6OwvNWUEPmSeQCgxk0ISG9luI5A8NYj7LxXIQF3Zk75xWi rf8g== X-Gm-Message-State: AOAM533IABI9WmxI2AQMbyqWsKK2vYP/vQ921JZNHA9Z6vrr1c+mufTJ ByHUk0SIxtBqHpJLwVsJRa5hjeIhe+U= X-Google-Smtp-Source: ABdhPJwJmPTNiVL5jyoO8h8d6tBKyzekzys9Qj16fPVifIycwIR/KMqV3tCAfVBw+3W17tlOpxhwDg== X-Received: by 2002:adf:f602:: with SMTP id t2mr53641418wrp.539.1638899131750; Tue, 07 Dec 2021 09:45:31 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w4sm324747wrs.88.2021.12.07.09.45.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:31 -0800 (PST) Message-Id: <458adf0fa9b90dae831da7aa8cdb67fa9a064ab5.1638899124.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:22 +0000 Subject: [PATCH 08/10] reftable: order unittests by complexity Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This is a more pratical ordering when working on refactorings of the reftable code. Signed-off-by: Han-Wen Nienhuys --- t/helper/test-reftable.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/t/helper/test-reftable.c b/t/helper/test-reftable.c index 26b03d7b789..f08d66df949 100644 --- a/t/helper/test-reftable.c +++ b/t/helper/test-reftable.c @@ -3,15 +3,16 @@ int cmd__reftable(int argc, const char **argv) { + // test from simple to complex. basics_test_main(argc, argv); + record_test_main(argc, argv); block_test_main(argc, argv); - merged_test_main(argc, argv); + tree_test_main(argc, argv); pq_test_main(argc, argv); - record_test_main(argc, argv); - refname_test_main(argc, argv); readwrite_test_main(argc, argv); + merged_test_main(argc, argv); stack_test_main(argc, argv); - tree_test_main(argc, argv); + refname_test_main(argc, argv); return 0; } From patchwork Tue Dec 7 17:45:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662371 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 400C9C433F5 for ; Tue, 7 Dec 2021 17:45:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235433AbhLGRtK (ORCPT ); Tue, 7 Dec 2021 12:49:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240170AbhLGRtE (ORCPT ); Tue, 7 Dec 2021 12:49:04 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CE6AC061746 for ; Tue, 7 Dec 2021 09:45:34 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id 137so11452170wma.1 for ; Tue, 07 Dec 2021 09:45:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=GWnJo8DsA3QyYjaZ609A0ySo06L+a0P9CK9sPytuqy8=; b=nTP8+UhchTo3QQtxFsXRohtiap383Wa4RgZLBSrOuPPIX1UIlRdBTwsA7K/ENLTGuQ +DP9FXh7GmCOah0+SH2pwgPO/3F/mQPQFUZsp+PilmLAv3FXT+10rd7HrLVw4790uAJD TnATSJqORZNlnLvOIvT4mfKaglh9ANJTFfxp+cAcRwLwXWChSEuHxXZtpLeFsDDt5lZC ffNS1DdRDWpGBlyBTiNMFzU9WGgWhgPvF4ydlf3UPzbJcITtqhMFInwGbAA4RYplkBJa SC3iod/bl5R7fNbb5Ov6n2Cm2r1Ko3X2uN0YeSQ7yZSm8yRqGWbbEeRlzqQxnMad2ddv fysg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=GWnJo8DsA3QyYjaZ609A0ySo06L+a0P9CK9sPytuqy8=; b=kEsw0UDPTMcmrODZqyf3czvE9Jwq/L5q3IaA2YzsgLMun98cRQHf30Y/D78dmXmtRs 0+ZOQ0Yv6KSatUUF8pZ31V/14mfCLE8PhiJCthgp1RqZH0oh3G91B9QTo4ygOuZxjO5M T/uAMQyuhVMxx0xvxlKtEEeLZoAGKwIVHs3eFbNxS+DXuh9fcRvuwINHqbmv/rNsk+OD 0ZIGXz0G+V1liCbiJ4zH5Nnrfb7GBTW5el/jxcvYQY0QFJLTmccpFPN5TTiXlkiaCxf0 6zKEp3PUAKpc/ZJ7JOiglBkHzxHGTwPyAF7FqlFA2mPOCWHdKhOQAGJj5u7QBodV12DB 6W+A== X-Gm-Message-State: AOAM5330eOJWSYJOWGHo+01pIRzfa4GhOODrv//vgxAPzXtbTcX8G1+2 d2JCUIU5wFXUQ/2HIRtjGOzRdeY467A= X-Google-Smtp-Source: ABdhPJybLwx2hb/faAddv4BA7DUDBHvu6gRNKGYb59iXGDu2gFTMBmZVnYJpRWtRQtN8VLe+rUNNZg== X-Received: by 2002:a1c:8002:: with SMTP id b2mr8965938wmd.2.1638899132371; Tue, 07 Dec 2021 09:45:32 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j134sm297032wmj.3.2021.12.07.09.45.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:32 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:23 +0000 Subject: [PATCH 09/10] reftable: drop stray printf in readwrite_test Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Signed-off-by: Han-Wen Nienhuys --- reftable/readwrite_test.c | 1 - 1 file changed, 1 deletion(-) diff --git a/reftable/readwrite_test.c b/reftable/readwrite_test.c index 5f6bcc2f775..7c40b9b77f3 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -597,7 +597,6 @@ static void test_write_key_order(void) err = reftable_writer_add_ref(w, &refs[0]); EXPECT_ERR(err); err = reftable_writer_add_ref(w, &refs[1]); - printf("%d\n", err); EXPECT(err == REFTABLE_API_ERROR); reftable_writer_close(w); reftable_writer_free(w); From patchwork Tue Dec 7 17:45:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12662373 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1859CC433FE for ; Tue, 7 Dec 2021 17:45:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240176AbhLGRtL (ORCPT ); Tue, 7 Dec 2021 12:49:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240200AbhLGRtK (ORCPT ); Tue, 7 Dec 2021 12:49:10 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7D49C061A32 for ; Tue, 7 Dec 2021 09:45:35 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id q3so31094903wru.5 for ; Tue, 07 Dec 2021 09:45:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=7it9M3NU5wjN3wI2TV3S3R8n4yhq3FJi98HUkUWUZAY=; b=W6HUOO8LtZDTA/vQ52bsNs3gEERKMfyoJ0wzn93nE+iu8+ZGYUgrD2rLyzJLNeUBXr ANjrhDemM5MmUReZj7Y4OKhiul6HPlJWHPzfNlL6JRAbQ33YMS8/j+tqRDLSl9z7CQnW 9ZHQksrMCtyMz9AT5fNwOn1QeWWdxh8qE5v8DkFRmETslbO6NNDCPajmTmntGhJmmLaq dxjg3xS3UgSN7sOfYCiHycknPX2DLXEFN4DnzdNeuvEHe9ux4SJphKk6u2KLCgMeHgkQ QFX9E12MF6L3a++MXoO7wKSuaJByHwZ+jJBlTW/x63vJ/W4r4eDtH1LbhnidPp1h1jiy GU2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=7it9M3NU5wjN3wI2TV3S3R8n4yhq3FJi98HUkUWUZAY=; b=mk+/WaTwD0iNDlzu3bkZ8DPpk494ujj4VtBzsEwNd6rC5hltadw1mo1QSKYXxTdC+R QCXJmAmMXll8HeSeDQamdZEwyNF4Jek5sFrX5jjqdc/hV4DUngaozE0XaWwE6tsz0YfV krH7IMK6IviBlBtb3ax0nD8tTlrgKtz2dMyBeuycvNN5LnrlmWq/7cCbkG0c4hMeeijr Y43jCy+exK7s01WWSOHEbVrr7Sgzpych6R9kDAkarot2+ZEIv76WpYhSF/OuL/ND87PY 5lhByJAXJjloWGlyBOnvfOOSdsoXzaokBMRhZ/rjMWisKJhzr6sjdzN2SglJ2qOc601c /E2A== X-Gm-Message-State: AOAM532fHZovfHEz9mwH5B/pTw7qMxn+v/+TV9ycLDOSroi4pTi6PckB yWSuBs2jWOvGdkrui++x4m6ThA6XrRM= X-Google-Smtp-Source: ABdhPJz36dKyVHy8xh2Perb/E4Cnuz1Dt3ZpSUdsYxSAbg89SPiR1P/QBeXSo1FXzZ+l2f0fDpR3nQ== X-Received: by 2002:adf:f749:: with SMTP id z9mr54046483wrp.379.1638899133438; Tue, 07 Dec 2021 09:45:33 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y6sm376367wrh.18.2021.12.07.09.45.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Dec 2021 09:45:33 -0800 (PST) Message-Id: <8deccc3a1dff7e4f7d613fa63d2781fd1f11f841.1638899124.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 07 Dec 2021 17:45:24 +0000 Subject: [PATCH 10/10] reftable: make reftable_record a tagged union Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This reduces the amount of glue code, because we don't need a void pointer or vtable within the structure. The only snag is that reftable_index_record contain a strbuf, so it cannot be zero-initialized. To address this, introduce reftable_record_for() to create a fresh instance, given a record type. Thanks to Peff for the suggestion. Signed-off-by: Han-Wen Nienhuys --- reftable/block.c | 4 +- reftable/block_test.c | 22 +++--- reftable/generic.c | 35 ++++---- reftable/iter.c | 4 +- reftable/merged.c | 37 ++++----- reftable/pq.c | 3 +- reftable/pq_test.c | 31 ++++---- reftable/reader.c | 105 ++++++++++++------------ reftable/record.c | 176 ++++++++++++++++------------------------- reftable/record.h | 45 +++++------ reftable/record_test.c | 162 +++++++++++++++++++------------------ reftable/writer.c | 46 ++++++----- 12 files changed, 319 insertions(+), 351 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index d7347bb3152..ad713caa57b 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -382,7 +382,7 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it, .key = *want, .r = br, }; - struct reftable_record rec = reftable_new_record(block_reader_type(br)); + struct reftable_record rec = reftable_record_for(block_reader_type(br)); struct strbuf key = STRBUF_INIT; int err = 0; struct block_iter next = { @@ -424,7 +424,7 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it, done: strbuf_release(&key); strbuf_release(&next.last_key); - reftable_record_destroy(&rec); + reftable_record_release(&rec); return err; } diff --git a/reftable/block_test.c b/reftable/block_test.c index 4b3ea262dcb..670329f2224 100644 --- a/reftable/block_test.c +++ b/reftable/block_test.c @@ -26,8 +26,9 @@ static void test_block_read_write(void) struct block_writer bw = { .last_key = STRBUF_INIT, }; - struct reftable_ref_record ref = { NULL }; - struct reftable_record rec = { NULL }; + struct reftable_record rec = { + .type = BLOCK_TYPE_REF, + }; int i = 0; int n; struct block_reader br = { 0 }; @@ -40,7 +41,6 @@ static void test_block_read_write(void) block.source = malloc_block_source(); block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size, header_off, hash_size(GIT_SHA1_FORMAT_ID)); - reftable_record_from_ref(&rec, &ref); for (i = 0; i < N; i++) { char name[100]; @@ -48,14 +48,14 @@ static void test_block_read_write(void) snprintf(name, sizeof(name), "branch%02d", i); memset(hash, i, sizeof(hash)); - ref.refname = name; - ref.value_type = REFTABLE_REF_VAL1; - ref.value.val1 = hash; + rec.ref.refname = name; + rec.ref.value_type = REFTABLE_REF_VAL1; + rec.ref.value.val1 = hash; names[i] = xstrdup(name); n = block_writer_add(&bw, &rec); - ref.refname = NULL; - ref.value_type = REFTABLE_REF_DELETION; + rec.ref.refname = NULL; + rec.ref.value_type = REFTABLE_REF_DELETION; EXPECT(n == 0); } @@ -74,7 +74,7 @@ static void test_block_read_write(void) if (r > 0) { break; } - EXPECT_STREQ(names[j], ref.refname); + EXPECT_STREQ(names[j], rec.ref.refname); j++; } @@ -92,7 +92,7 @@ static void test_block_read_write(void) n = block_iter_next(&it, &rec); EXPECT(n == 0); - EXPECT_STREQ(names[i], ref.refname); + EXPECT_STREQ(names[i], rec.ref.refname); want.len--; n = block_reader_seek(&br, &it, &want); @@ -100,7 +100,7 @@ static void test_block_read_write(void) n = block_iter_next(&it, &rec); EXPECT(n == 0); - EXPECT_STREQ(names[10 * (i / 10)], ref.refname); + EXPECT_STREQ(names[10 * (i / 10)], rec.ref.refname); block_iter_close(&it); } diff --git a/reftable/generic.c b/reftable/generic.c index 7a8a738d860..7f00aa9cdbe 100644 --- a/reftable/generic.c +++ b/reftable/generic.c @@ -7,6 +7,7 @@ https://developers.google.com/open-source/licenses/bsd */ #include "basics.h" +#include "constants.h" #include "record.h" #include "generic.h" #include "reftable-iterator.h" @@ -15,23 +16,21 @@ https://developers.google.com/open-source/licenses/bsd int reftable_table_seek_ref(struct reftable_table *tab, struct reftable_iterator *it, const char *name) { - struct reftable_ref_record ref = { - .refname = (char *)name, - }; - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, &ref); + struct reftable_record rec = { .type = BLOCK_TYPE_REF, + .ref = { + .refname = (char *)name, + } }; return tab->ops->seek_record(tab->table_arg, it, &rec); } int reftable_table_seek_log(struct reftable_table *tab, struct reftable_iterator *it, const char *name) { - struct reftable_log_record log = { - .refname = (char *)name, - .update_index = ~((uint64_t)0), - }; - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, &log); + struct reftable_record rec = { .type = BLOCK_TYPE_LOG, + .log = { + .refname = (char *)name, + .update_index = ~((uint64_t)0), + } }; return tab->ops->seek_record(tab->table_arg, it, &rec); } @@ -129,17 +128,19 @@ void reftable_iterator_destroy(struct reftable_iterator *it) int reftable_iterator_next_ref(struct reftable_iterator *it, struct reftable_ref_record *ref) { - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, ref); - return iterator_next(it, &rec); + struct reftable_record rec = { .type = BLOCK_TYPE_REF }; + int err = iterator_next(it, &rec); + *ref = rec.ref; + return err; } int reftable_iterator_next_log(struct reftable_iterator *it, struct reftable_log_record *log) { - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, log); - return iterator_next(it, &rec); + struct reftable_record rec = { .type = BLOCK_TYPE_LOG }; + int err = iterator_next(it, &rec); + *log = rec.log; + return err; } int iterator_next(struct reftable_iterator *it, struct reftable_record *rec) diff --git a/reftable/iter.c b/reftable/iter.c index 93d04f735b8..fedf5d8ab0b 100644 --- a/reftable/iter.c +++ b/reftable/iter.c @@ -32,7 +32,7 @@ static int filtering_ref_iterator_next(void *iter_arg, struct reftable_record *rec) { struct filtering_ref_iterator *fri = iter_arg; - struct reftable_ref_record *ref = rec->data; + struct reftable_ref_record *ref = &rec->ref; int err = 0; while (1) { err = reftable_iterator_next_ref(&fri->it, ref); @@ -127,7 +127,7 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it) static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec) { struct indexed_table_ref_iter *it = p; - struct reftable_ref_record *ref = rec->data; + struct reftable_ref_record *ref = &rec->ref; while (1) { int err = block_iter_next(&it->cur, rec); diff --git a/reftable/merged.c b/reftable/merged.c index e5b53da6db3..e69955d7c2d 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -22,7 +22,7 @@ static int merged_iter_init(struct merged_iter *mi) { int i = 0; for (i = 0; i < mi->stack_len; i++) { - struct reftable_record rec = reftable_new_record(mi->typ); + struct reftable_record rec = reftable_record_for(mi->typ); int err = iterator_next(&mi->stack[i], &rec); if (err < 0) { return err; @@ -30,7 +30,7 @@ static int merged_iter_init(struct merged_iter *mi) if (err > 0) { reftable_iterator_destroy(&mi->stack[i]); - reftable_record_destroy(&rec); + reftable_record_release(&rec); } else { struct pq_entry e = { .rec = rec, @@ -57,18 +57,17 @@ static void merged_iter_close(void *p) static int merged_iter_advance_nonnull_subiter(struct merged_iter *mi, size_t idx) { - struct reftable_record rec = reftable_new_record(mi->typ); struct pq_entry e = { - .rec = rec, + .rec = reftable_record_for(mi->typ), .index = idx, }; - int err = iterator_next(&mi->stack[idx], &rec); + int err = iterator_next(&mi->stack[idx], &e.rec); if (err < 0) return err; if (err > 0) { reftable_iterator_destroy(&mi->stack[idx]); - reftable_record_destroy(&rec); + reftable_record_release(&e.rec); return 0; } @@ -126,11 +125,11 @@ static int merged_iter_next_entry(struct merged_iter *mi, if (err < 0) { return err; } - reftable_record_destroy(&top.rec); + reftable_record_release(&top.rec); } reftable_record_copy_from(rec, &entry.rec, hash_size(mi->hash_id)); - reftable_record_destroy(&entry.rec); + reftable_record_release(&entry.rec); strbuf_release(&entry_key); return 0; } @@ -246,7 +245,7 @@ static int merged_table_seek_record(struct reftable_merged_table *mt, sizeof(struct reftable_iterator) * mt->stack_len); struct merged_iter merged = { .stack = iters, - .typ = reftable_record_type(rec), + .typ = rec->type, .hash_id = mt->hash_id, .suppress_deletions = mt->suppress_deletions, }; @@ -290,11 +289,12 @@ int reftable_merged_table_seek_ref(struct reftable_merged_table *mt, struct reftable_iterator *it, const char *name) { - struct reftable_ref_record ref = { - .refname = (char *)name, + struct reftable_record rec = { + .type = BLOCK_TYPE_REF, + .ref = { + .refname = (char *)name, + }, }; - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, &ref); return merged_table_seek_record(mt, it, &rec); } @@ -302,12 +302,13 @@ int reftable_merged_table_seek_log_at(struct reftable_merged_table *mt, struct reftable_iterator *it, const char *name, uint64_t update_index) { - struct reftable_log_record log = { - .refname = (char *)name, - .update_index = update_index, + struct reftable_record rec = { + .type = BLOCK_TYPE_LOG, + .log = { + .refname = (char *)name, + .update_index = update_index, + } }; - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, &log); return merged_table_seek_record(mt, it, &rec); } diff --git a/reftable/pq.c b/reftable/pq.c index efc474017a2..96ca6dd37b3 100644 --- a/reftable/pq.c +++ b/reftable/pq.c @@ -74,6 +74,7 @@ struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq) void merged_iter_pqueue_add(struct merged_iter_pqueue *pq, struct pq_entry e) { int i = 0; + if (pq->len == pq->cap) { pq->cap = 2 * pq->cap + 1; pq->heap = reftable_realloc(pq->heap, @@ -98,7 +99,7 @@ void merged_iter_pqueue_release(struct merged_iter_pqueue *pq) { int i = 0; for (i = 0; i < pq->len; i++) { - reftable_record_destroy(&pq->heap[i].rec); + reftable_record_release(&pq->heap[i].rec); } FREE_AND_NULL(pq->heap); pq->len = pq->cap = 0; diff --git a/reftable/pq_test.c b/reftable/pq_test.c index c9bb05e37b7..89d069d674d 100644 --- a/reftable/pq_test.c +++ b/reftable/pq_test.c @@ -31,7 +31,7 @@ static void test_pq(void) int N = ARRAY_SIZE(names) - 1; struct merged_iter_pqueue pq = { NULL }; - const char *last = NULL; + char *last = NULL; int i = 0; for (i = 0; i < N; i++) { @@ -42,12 +42,14 @@ static void test_pq(void) i = 1; do { - struct reftable_record rec = - reftable_new_record(BLOCK_TYPE_REF); - struct pq_entry e = { 0 }; - - reftable_record_as_ref(&rec)->refname = names[i]; - e.rec = rec; + struct pq_entry e = { + .rec = { + .type = BLOCK_TYPE_REF, + .ref = { + .refname = names[i], + } + } + }; merged_iter_pqueue_add(&pq, e); merged_iter_pqueue_check(pq); i = (i * 7) % N; @@ -55,19 +57,18 @@ static void test_pq(void) while (!merged_iter_pqueue_is_empty(pq)) { struct pq_entry e = merged_iter_pqueue_remove(&pq); - struct reftable_ref_record *ref = - reftable_record_as_ref(&e.rec); - + struct reftable_record *rec = &e.rec; merged_iter_pqueue_check(pq); + EXPECT(rec->type == BLOCK_TYPE_REF); if (last) { - EXPECT(strcmp(last, ref->refname) < 0); + EXPECT(strcmp(last, rec->ref.refname) < 0); } - last = ref->refname; - ref->refname = NULL; - reftable_free(ref); + // this is names[i], so don't dealloc. + last = rec->ref.refname; + rec->ref.refname = NULL; + reftable_record_release(rec); } - for (i = 0; i < N; i++) { reftable_free(names[i]); } diff --git a/reftable/reader.c b/reftable/reader.c index 8d308d858f8..0159f131122 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -238,9 +238,8 @@ static int table_iter_next_in_block(struct table_iter *ti, struct reftable_record *rec) { int res = block_iter_next(&ti->bi, rec); - if (res == 0 && reftable_record_type(rec) == BLOCK_TYPE_REF) { - ((struct reftable_ref_record *)rec->data)->update_index += - ti->r->min_update_index; + if (res == 0 && rec->type == BLOCK_TYPE_REF) { + rec->ref.update_index += ti->r->min_update_index; } return res; @@ -345,7 +344,7 @@ static int table_iter_next_block(struct table_iter *dest, static int table_iter_next(struct table_iter *ti, struct reftable_record *rec) { - if (reftable_record_type(rec) != ti->typ) + if (rec->type != ti->typ) return REFTABLE_API_ERROR; while (1) { @@ -437,8 +436,7 @@ static int reader_start(struct reftable_reader *r, struct table_iter *ti, static int reader_seek_linear(struct reftable_reader *r, struct table_iter *ti, struct reftable_record *want) { - struct reftable_record rec = - reftable_new_record(reftable_record_type(want)); + struct reftable_record rec = reftable_record_for(want->type); struct strbuf want_key = STRBUF_INIT; struct strbuf got_key = STRBUF_INIT; struct table_iter next = TABLE_ITER_INIT; @@ -475,7 +473,7 @@ static int reader_seek_linear(struct reftable_reader *r, struct table_iter *ti, done: block_iter_close(&next.bi); - reftable_record_destroy(&rec); + reftable_record_release(&rec); strbuf_release(&want_key); strbuf_release(&got_key); return err; @@ -485,38 +483,40 @@ static int reader_seek_indexed(struct reftable_reader *r, struct reftable_iterator *it, struct reftable_record *rec) { - struct reftable_index_record want_index = { .last_key = STRBUF_INIT }; - struct reftable_record want_index_rec = { NULL }; - struct reftable_index_record index_result = { .last_key = STRBUF_INIT }; - struct reftable_record index_result_rec = { NULL }; + struct reftable_record want_index = { + .type = BLOCK_TYPE_INDEX, + .idx = { .last_key = STRBUF_INIT } + }; + struct reftable_record index_result = { + .type = BLOCK_TYPE_INDEX, + .idx = { .last_key = STRBUF_INIT }, + }; struct table_iter index_iter = TABLE_ITER_INIT; struct table_iter next = TABLE_ITER_INIT; int err = 0; - reftable_record_key(rec, &want_index.last_key); - reftable_record_from_index(&want_index_rec, &want_index); - reftable_record_from_index(&index_result_rec, &index_result); - - err = reader_start(r, &index_iter, reftable_record_type(rec), 1); + reftable_record_key(rec, &want_index.idx.last_key); + err = reader_start(r, &index_iter, rec->type, 1); if (err < 0) goto done; - err = reader_seek_linear(r, &index_iter, &want_index_rec); + err = reader_seek_linear(r, &index_iter, &want_index); while (1) { - err = table_iter_next(&index_iter, &index_result_rec); + err = table_iter_next(&index_iter, &index_result); table_iter_block_done(&index_iter); if (err != 0) goto done; - err = reader_table_iter_at(r, &next, index_result.offset, 0); + err = reader_table_iter_at(r, &next, index_result.idx.offset, + 0); if (err != 0) goto done; - err = block_iter_seek(&next.bi, &want_index.last_key); + err = block_iter_seek(&next.bi, &want_index.idx.last_key); if (err < 0) goto done; - if (next.typ == reftable_record_type(rec)) { + if (next.typ == rec->type) { err = 0; break; } @@ -540,8 +540,8 @@ static int reader_seek_indexed(struct reftable_reader *r, done: block_iter_close(&next.bi); table_iter_close(&index_iter); - reftable_record_release(&want_index_rec); - reftable_record_release(&index_result_rec); + reftable_record_release(&want_index); + reftable_record_release(&index_result); return err; } @@ -549,15 +549,14 @@ static int reader_seek_internal(struct reftable_reader *r, struct reftable_iterator *it, struct reftable_record *rec) { - struct reftable_reader_offsets *offs = - reader_offsets_for(r, reftable_record_type(rec)); + struct reftable_reader_offsets *offs = reader_offsets_for(r, rec->type); uint64_t idx = offs->index_offset; struct table_iter ti = TABLE_ITER_INIT; int err = 0; if (idx > 0) return reader_seek_indexed(r, it, rec); - err = reader_start(r, &ti, reftable_record_type(rec), 0); + err = reader_start(r, &ti, rec->type, 0); if (err < 0) return err; err = reader_seek_linear(r, &ti, rec); @@ -576,7 +575,7 @@ static int reader_seek_internal(struct reftable_reader *r, static int reader_seek(struct reftable_reader *r, struct reftable_iterator *it, struct reftable_record *rec) { - uint8_t typ = reftable_record_type(rec); + uint8_t typ = rec->type; struct reftable_reader_offsets *offs = reader_offsets_for(r, typ); if (!offs->is_present) { @@ -590,11 +589,12 @@ static int reader_seek(struct reftable_reader *r, struct reftable_iterator *it, int reftable_reader_seek_ref(struct reftable_reader *r, struct reftable_iterator *it, const char *name) { - struct reftable_ref_record ref = { - .refname = (char *)name, + struct reftable_record rec = { + .type = BLOCK_TYPE_REF, + .ref = { + .refname = (char *)name, + }, }; - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, &ref); return reader_seek(r, it, &rec); } @@ -602,12 +602,13 @@ int reftable_reader_seek_log_at(struct reftable_reader *r, struct reftable_iterator *it, const char *name, uint64_t update_index) { - struct reftable_log_record log = { - .refname = (char *)name, - .update_index = update_index, + struct reftable_record rec = { + .type = BLOCK_TYPE_LOG, + .log = { + .refname = (char *)name, + .update_index = update_index, + } }; - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, &log); return reader_seek(r, it, &rec); } @@ -649,31 +650,33 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r, struct reftable_iterator *it, uint8_t *oid) { - struct reftable_obj_record want = { - .hash_prefix = oid, - .hash_prefix_len = r->object_id_len, + struct reftable_record want = { + .type = BLOCK_TYPE_OBJ, + .obj = { + .hash_prefix = oid, + .hash_prefix_len = r->object_id_len, + }, }; - struct reftable_record want_rec = { NULL }; struct reftable_iterator oit = { NULL }; - struct reftable_obj_record got = { NULL }; - struct reftable_record got_rec = { NULL }; + struct reftable_record got = { + .type = BLOCK_TYPE_OBJ, + .obj = { 0 }, + }; int err = 0; struct indexed_table_ref_iter *itr = NULL; /* Look through the reverse index. */ - reftable_record_from_obj(&want_rec, &want); - err = reader_seek(r, &oit, &want_rec); + err = reader_seek(r, &oit, &want); if (err != 0) goto done; /* read out the reftable_obj_record */ - reftable_record_from_obj(&got_rec, &got); - err = iterator_next(&oit, &got_rec); + err = iterator_next(&oit, &got); if (err < 0) goto done; - if (err > 0 || - memcmp(want.hash_prefix, got.hash_prefix, r->object_id_len)) { + if (err > 0 || memcmp(want.obj.hash_prefix, got.obj.hash_prefix, + r->object_id_len)) { /* didn't find it; return empty iterator */ iterator_set_empty(it); err = 0; @@ -681,15 +684,15 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r, } err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id), - got.offsets, got.offset_len); + got.obj.offsets, got.obj.offset_len); if (err < 0) goto done; - got.offsets = NULL; + got.obj.offsets = NULL; iterator_from_indexed_table_ref_iter(it, itr); done: reftable_iterator_destroy(&oit); - reftable_record_release(&got_rec); + reftable_record_release(&got); return err; } diff --git a/reftable/record.c b/reftable/record.c index 8536bd03aa9..b665feb709f 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -15,6 +15,10 @@ https://developers.google.com/open-source/licenses/bsd #include "reftable-error.h" #include "basics.h" +static struct reftable_record_vtable * +reftable_record_vtable(struct reftable_record *rec); +static void *reftable_record_data(struct reftable_record *rec); + int get_var_int(uint64_t *dest, struct string_view *in) { int ptr = 0; @@ -926,58 +930,6 @@ static struct reftable_record_vtable reftable_log_record_vtable = { .is_deletion = &reftable_log_record_is_deletion_void, }; -struct reftable_record reftable_new_record(uint8_t typ) -{ - struct reftable_record rec = { NULL }; - switch (typ) { - case BLOCK_TYPE_REF: { - struct reftable_ref_record *r = - reftable_calloc(sizeof(struct reftable_ref_record)); - reftable_record_from_ref(&rec, r); - return rec; - } - - case BLOCK_TYPE_OBJ: { - struct reftable_obj_record *r = - reftable_calloc(sizeof(struct reftable_obj_record)); - reftable_record_from_obj(&rec, r); - return rec; - } - case BLOCK_TYPE_LOG: { - struct reftable_log_record *r = - reftable_calloc(sizeof(struct reftable_log_record)); - reftable_record_from_log(&rec, r); - return rec; - } - case BLOCK_TYPE_INDEX: { - struct reftable_index_record empty = { .last_key = - STRBUF_INIT }; - struct reftable_index_record *r = - reftable_calloc(sizeof(struct reftable_index_record)); - *r = empty; - reftable_record_from_index(&rec, r); - return rec; - } - } - abort(); - return rec; -} - -/* clear out the record, yielding the reftable_record data that was - * encapsulated. */ -static void *reftable_record_yield(struct reftable_record *rec) -{ - void *p = rec->data; - rec->data = NULL; - return p; -} - -void reftable_record_destroy(struct reftable_record *rec) -{ - reftable_record_release(rec); - reftable_free(reftable_record_yield(rec)); -} - static void reftable_index_record_key(const void *r, struct strbuf *dest) { const struct reftable_index_record *rec = r; @@ -1055,91 +1007,47 @@ static struct reftable_record_vtable reftable_index_record_vtable = { void reftable_record_key(struct reftable_record *rec, struct strbuf *dest) { - rec->ops->key(rec->data, dest); -} - -uint8_t reftable_record_type(struct reftable_record *rec) -{ - return rec->ops->type; + reftable_record_vtable(rec)->key(reftable_record_data(rec), dest); } int reftable_record_encode(struct reftable_record *rec, struct string_view dest, int hash_size) { - return rec->ops->encode(rec->data, dest, hash_size); + return reftable_record_vtable(rec)->encode(reftable_record_data(rec), + dest, hash_size); } void reftable_record_copy_from(struct reftable_record *rec, struct reftable_record *src, int hash_size) { - assert(src->ops->type == rec->ops->type); + assert(src->type == rec->type); - rec->ops->copy_from(rec->data, src->data, hash_size); + reftable_record_vtable(rec)->copy_from(reftable_record_data(rec), + reftable_record_data(src), + hash_size); } uint8_t reftable_record_val_type(struct reftable_record *rec) { - return rec->ops->val_type(rec->data); + return reftable_record_vtable(rec)->val_type(reftable_record_data(rec)); } int reftable_record_decode(struct reftable_record *rec, struct strbuf key, uint8_t extra, struct string_view src, int hash_size) { - return rec->ops->decode(rec->data, key, extra, src, hash_size); + return reftable_record_vtable(rec)->decode(reftable_record_data(rec), + key, extra, src, hash_size); } void reftable_record_release(struct reftable_record *rec) { - rec->ops->release(rec->data); + reftable_record_vtable(rec)->release(reftable_record_data(rec)); } int reftable_record_is_deletion(struct reftable_record *rec) { - return rec->ops->is_deletion(rec->data); -} - -void reftable_record_from_ref(struct reftable_record *rec, - struct reftable_ref_record *ref_rec) -{ - assert(!rec->ops); - rec->data = ref_rec; - rec->ops = &reftable_ref_record_vtable; -} - -void reftable_record_from_obj(struct reftable_record *rec, - struct reftable_obj_record *obj_rec) -{ - assert(!rec->ops); - rec->data = obj_rec; - rec->ops = &reftable_obj_record_vtable; -} - -void reftable_record_from_index(struct reftable_record *rec, - struct reftable_index_record *index_rec) -{ - assert(!rec->ops); - rec->data = index_rec; - rec->ops = &reftable_index_record_vtable; -} - -void reftable_record_from_log(struct reftable_record *rec, - struct reftable_log_record *log_rec) -{ - assert(!rec->ops); - rec->data = log_rec; - rec->ops = &reftable_log_record_vtable; -} - -struct reftable_ref_record *reftable_record_as_ref(struct reftable_record *rec) -{ - assert(reftable_record_type(rec) == BLOCK_TYPE_REF); - return rec->data; -} - -struct reftable_log_record *reftable_record_as_log(struct reftable_record *rec) -{ - assert(reftable_record_type(rec) == BLOCK_TYPE_LOG); - return rec->data; + return reftable_record_vtable(rec)->is_deletion( + reftable_record_data(rec)); } static int hash_equal(uint8_t *a, uint8_t *b, int hash_size) @@ -1210,3 +1118,53 @@ void string_view_consume(struct string_view *s, int n) s->buf += n; s->len -= n; } + +static void *reftable_record_data(struct reftable_record *rec) +{ + switch (rec->type) { + case BLOCK_TYPE_REF: + return &rec->ref; + case BLOCK_TYPE_LOG: + return &rec->log; + case BLOCK_TYPE_INDEX: + return &rec->idx; + case BLOCK_TYPE_OBJ: + return &rec->obj; + } + abort(); +} + +static struct reftable_record_vtable * +reftable_record_vtable(struct reftable_record *rec) +{ + switch (rec->type) { + case BLOCK_TYPE_REF: + return &reftable_ref_record_vtable; + case BLOCK_TYPE_LOG: + return &reftable_log_record_vtable; + case BLOCK_TYPE_INDEX: + return &reftable_index_record_vtable; + case BLOCK_TYPE_OBJ: + return &reftable_obj_record_vtable; + } + abort(); +} + +struct reftable_record reftable_record_for(uint8_t typ) +{ + struct reftable_record clean_idx = { + .type = BLOCK_TYPE_INDEX, + .idx = { + .last_key = STRBUF_INIT, + }, + }; + struct reftable_record clean = { + .type = typ, + }; + + if (typ == BLOCK_TYPE_INDEX) { + return clean_idx; + } + + return clean; +} diff --git a/reftable/record.h b/reftable/record.h index 498e8c50bf4..a702c67e857 100644 --- a/reftable/record.h +++ b/reftable/record.h @@ -60,18 +60,9 @@ struct reftable_record_vtable { int (*is_deletion)(const void *rec); }; -/* record is a generic wrapper for different types of records. */ -struct reftable_record { - void *data; - struct reftable_record_vtable *ops; -}; - /* returns true for recognized block types. Block start with the block type. */ int reftable_is_block_type(uint8_t typ); -/* creates a malloced record of the given type. Dispose with record_destroy */ -struct reftable_record reftable_new_record(uint8_t typ); - /* Encode `key` into `dest`. Sets `is_restart` to indicate a restart. Returns * number of bytes written. */ int reftable_encode_key(int *is_restart, struct string_view dest, @@ -97,8 +88,26 @@ struct reftable_obj_record { int offset_len; }; +/* record is a generic wrapper for different types of records. It is normally + * created on the stack, or embedded within another struct. If the type is + * known, a fresh instance can be initialized explicitly. Otherwise, use + * reftable_record_for() to initialize generically (as the index_record is not + * valid as 0-initialized structure) + */ +struct reftable_record { + uint8_t type; + union { + struct reftable_ref_record ref; + struct reftable_log_record log; + struct reftable_obj_record obj; + struct reftable_index_record idx; + }; +}; + /* see struct record_vtable */ +/* return an initialized record for the given type */ +struct reftable_record reftable_record_for(uint8_t typ); void reftable_record_key(struct reftable_record *rec, struct strbuf *dest); uint8_t reftable_record_type(struct reftable_record *rec); void reftable_record_copy_from(struct reftable_record *rec, @@ -111,25 +120,9 @@ int reftable_record_decode(struct reftable_record *rec, struct strbuf key, int hash_size); int reftable_record_is_deletion(struct reftable_record *rec); -/* zeroes out the embedded record */ +/* frees and zeroes out the embedded record */ void reftable_record_release(struct reftable_record *rec); -/* clear and deallocate embedded record, and zero `rec`. */ -void reftable_record_destroy(struct reftable_record *rec); - -/* initialize generic records from concrete records. The generic record should - * be zeroed out. */ -void reftable_record_from_obj(struct reftable_record *rec, - struct reftable_obj_record *objrec); -void reftable_record_from_index(struct reftable_record *rec, - struct reftable_index_record *idxrec); -void reftable_record_from_ref(struct reftable_record *rec, - struct reftable_ref_record *refrec); -void reftable_record_from_log(struct reftable_record *rec, - struct reftable_log_record *logrec); -struct reftable_ref_record *reftable_record_as_ref(struct reftable_record *ref); -struct reftable_log_record *reftable_record_as_log(struct reftable_record *ref); - /* for qsort. */ int reftable_ref_record_compare_name(const void *a, const void *b); diff --git a/reftable/record_test.c b/reftable/record_test.c index f4ad7cace41..23abfb037b3 100644 --- a/reftable/record_test.c +++ b/reftable/record_test.c @@ -16,24 +16,22 @@ static void test_copy(struct reftable_record *rec) { - struct reftable_record copy = - reftable_new_record(reftable_record_type(rec)); + struct reftable_record copy = reftable_record_for(rec->type); + reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ); /* do it twice to catch memory leaks */ reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ); - switch (reftable_record_type(©)) { + switch (copy.type) { case BLOCK_TYPE_REF: - EXPECT(reftable_ref_record_equal(reftable_record_as_ref(©), - reftable_record_as_ref(rec), + EXPECT(reftable_ref_record_equal(©.ref, &rec->ref, GIT_SHA1_RAWSZ)); break; case BLOCK_TYPE_LOG: - EXPECT(reftable_log_record_equal(reftable_record_as_log(©), - reftable_record_as_log(rec), + EXPECT(reftable_log_record_equal(©.log, &rec->log, GIT_SHA1_RAWSZ)); break; } - reftable_record_destroy(©); + reftable_record_release(©); } static void test_varint_roundtrip(void) @@ -106,61 +104,60 @@ static void test_reftable_ref_record_roundtrip(void) int i = 0; for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) { - struct reftable_ref_record in = { NULL }; - struct reftable_ref_record out = { NULL }; - struct reftable_record rec_out = { NULL }; + struct reftable_record in = { + .type = BLOCK_TYPE_REF, + }; + struct reftable_record out = { + .type = BLOCK_TYPE_REF + }; struct strbuf key = STRBUF_INIT; - struct reftable_record rec = { NULL }; uint8_t buffer[1024] = { 0 }; struct string_view dest = { .buf = buffer, .len = sizeof(buffer), }; - int n, m; - in.value_type = i; + in.ref.value_type = i; switch (i) { case REFTABLE_REF_DELETION: break; case REFTABLE_REF_VAL1: - in.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ); - set_hash(in.value.val1, 1); + in.ref.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ); + set_hash(in.ref.value.val1, 1); break; case REFTABLE_REF_VAL2: - in.value.val2.value = reftable_malloc(GIT_SHA1_RAWSZ); - set_hash(in.value.val2.value, 1); - in.value.val2.target_value = + in.ref.value.val2.value = + reftable_malloc(GIT_SHA1_RAWSZ); + set_hash(in.ref.value.val2.value, 1); + in.ref.value.val2.target_value = reftable_malloc(GIT_SHA1_RAWSZ); - set_hash(in.value.val2.target_value, 2); + set_hash(in.ref.value.val2.target_value, 2); break; case REFTABLE_REF_SYMREF: - in.value.symref = xstrdup("target"); + in.ref.value.symref = xstrdup("target"); break; } - in.refname = xstrdup("refs/heads/master"); + in.ref.refname = xstrdup("refs/heads/master"); - reftable_record_from_ref(&rec, &in); - test_copy(&rec); + test_copy(&in); - EXPECT(reftable_record_val_type(&rec) == i); + EXPECT(reftable_record_val_type(&in) == i); - reftable_record_key(&rec, &key); - n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); + reftable_record_key(&in, &key); + n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); EXPECT(n > 0); /* decode into a non-zero reftable_record to test for leaks. */ - - reftable_record_from_ref(&rec_out, &out); - m = reftable_record_decode(&rec_out, key, i, dest, - GIT_SHA1_RAWSZ); + m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ); EXPECT(n == m); - EXPECT(reftable_ref_record_equal(&in, &out, GIT_SHA1_RAWSZ)); - reftable_record_release(&rec_out); + EXPECT(reftable_ref_record_equal(&in.ref, &out.ref, + GIT_SHA1_RAWSZ)); + reftable_record_release(&in); strbuf_release(&key); - reftable_ref_record_release(&in); + reftable_record_release(&out); } } @@ -213,7 +210,9 @@ static void test_reftable_log_record_roundtrip(void) set_test_hash(in[0].value.update.new_hash, 1); set_test_hash(in[0].value.update.old_hash, 2); for (i = 0; i < ARRAY_SIZE(in); i++) { - struct reftable_record rec = { NULL }; + struct reftable_record rec = { + .type = BLOCK_TYPE_LOG + }; struct strbuf key = STRBUF_INIT; uint8_t buffer[1024] = { 0 }; struct string_view dest = { @@ -221,8 +220,10 @@ static void test_reftable_log_record_roundtrip(void) .len = sizeof(buffer), }; /* populate out, to check for leaks. */ - struct reftable_log_record out = { - .refname = xstrdup("old name"), + struct reftable_record out = { + .type = BLOCK_TYPE_LOG, + .log = { + .refname = xstrdup("old name"), .value_type = REFTABLE_LOG_UPDATE, .value = { .update = { @@ -233,11 +234,11 @@ static void test_reftable_log_record_roundtrip(void) .message = xstrdup("old message"), }, }, + }, }; - struct reftable_record rec_out = { NULL }; int n, m, valtype; - reftable_record_from_log(&rec, &in[i]); + rec.log = in[i]; test_copy(&rec); @@ -245,16 +246,16 @@ static void test_reftable_log_record_roundtrip(void) n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); EXPECT(n >= 0); - reftable_record_from_log(&rec_out, &out); valtype = reftable_record_val_type(&rec); - m = reftable_record_decode(&rec_out, key, valtype, dest, + m = reftable_record_decode(&out, key, valtype, dest, GIT_SHA1_RAWSZ); EXPECT(n == m); - EXPECT(reftable_log_record_equal(&in[i], &out, GIT_SHA1_RAWSZ)); + EXPECT(reftable_log_record_equal(&in[i], &out.log, + GIT_SHA1_RAWSZ)); reftable_log_record_release(&in[i]); strbuf_release(&key); - reftable_record_release(&rec_out); + reftable_record_release(&out); } } @@ -322,47 +323,51 @@ static void test_reftable_obj_record_roundtrip(void) } }; int i = 0; for (i = 0; i < ARRAY_SIZE(recs); i++) { - struct reftable_obj_record in = recs[i]; uint8_t buffer[1024] = { 0 }; struct string_view dest = { .buf = buffer, .len = sizeof(buffer), }; - struct reftable_record rec = { NULL }; + struct reftable_record in = { + .type = BLOCK_TYPE_OBJ, + .obj = recs[i], + }; struct strbuf key = STRBUF_INIT; - struct reftable_obj_record out = { NULL }; - struct reftable_record rec_out = { NULL }; + struct reftable_record out = { + .type = BLOCK_TYPE_OBJ + }; int n, m; uint8_t extra; - reftable_record_from_obj(&rec, &in); - test_copy(&rec); - reftable_record_key(&rec, &key); - n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); + test_copy(&in); + reftable_record_key(&in, &key); + n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); EXPECT(n > 0); - extra = reftable_record_val_type(&rec); - reftable_record_from_obj(&rec_out, &out); - m = reftable_record_decode(&rec_out, key, extra, dest, + extra = reftable_record_val_type(&in); + m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ); EXPECT(n == m); - EXPECT(in.hash_prefix_len == out.hash_prefix_len); - EXPECT(in.offset_len == out.offset_len); + EXPECT(in.obj.hash_prefix_len == out.obj.hash_prefix_len); + EXPECT(in.obj.offset_len == out.obj.offset_len); - EXPECT(!memcmp(in.hash_prefix, out.hash_prefix, - in.hash_prefix_len)); - EXPECT(0 == memcmp(in.offsets, out.offsets, - sizeof(uint64_t) * in.offset_len)); + EXPECT(!memcmp(in.obj.hash_prefix, out.obj.hash_prefix, + in.obj.hash_prefix_len)); + EXPECT(0 == memcmp(in.obj.offsets, out.obj.offsets, + sizeof(uint64_t) * in.obj.offset_len)); strbuf_release(&key); - reftable_record_release(&rec_out); + reftable_record_release(&out); } } static void test_reftable_index_record_roundtrip(void) { - struct reftable_index_record in = { - .offset = 42, - .last_key = STRBUF_INIT, + struct reftable_record in = { + .type = BLOCK_TYPE_INDEX, + .idx = { + .offset = 42, + .last_key = STRBUF_INIT, + }, }; uint8_t buffer[1024] = { 0 }; struct string_view dest = { @@ -370,31 +375,30 @@ static void test_reftable_index_record_roundtrip(void) .len = sizeof(buffer), }; struct strbuf key = STRBUF_INIT; - struct reftable_record rec = { NULL }; - struct reftable_index_record out = { .last_key = STRBUF_INIT }; - struct reftable_record out_rec = { NULL }; + struct reftable_record out = { + .type = BLOCK_TYPE_INDEX, + .idx = { .last_key = STRBUF_INIT }, + }; int n, m; uint8_t extra; - strbuf_addstr(&in.last_key, "refs/heads/master"); - reftable_record_from_index(&rec, &in); - reftable_record_key(&rec, &key); - test_copy(&rec); + strbuf_addstr(&in.idx.last_key, "refs/heads/master"); + reftable_record_key(&in, &key); + test_copy(&in); - EXPECT(0 == strbuf_cmp(&key, &in.last_key)); - n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); + EXPECT(0 == strbuf_cmp(&key, &in.idx.last_key)); + n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); EXPECT(n > 0); - extra = reftable_record_val_type(&rec); - reftable_record_from_index(&out_rec, &out); - m = reftable_record_decode(&out_rec, key, extra, dest, GIT_SHA1_RAWSZ); + extra = reftable_record_val_type(&in); + m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ); EXPECT(m == n); - EXPECT(in.offset == out.offset); + EXPECT(in.idx.offset == out.idx.offset); - reftable_record_release(&out_rec); + reftable_record_release(&out); strbuf_release(&key); - strbuf_release(&in.last_key); + strbuf_release(&in.idx.last_key); } int record_test_main(int argc, const char *argv[]) diff --git a/reftable/writer.c b/reftable/writer.c index 3ca721e9f64..df256f3a8b5 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -221,10 +221,10 @@ static int writer_add_record(struct reftable_writer *w, strbuf_reset(&w->last_key); strbuf_addbuf(&w->last_key, &key); if (w->block_writer == NULL) { - writer_reinit_block_writer(w, reftable_record_type(rec)); + writer_reinit_block_writer(w, rec->type); } - assert(block_writer_type(w->block_writer) == reftable_record_type(rec)); + assert(block_writer_type(w->block_writer) == rec->type); if (block_writer_add(w->block_writer, rec) == 0) { err = 0; @@ -236,7 +236,7 @@ static int writer_add_record(struct reftable_writer *w, goto done; } - writer_reinit_block_writer(w, reftable_record_type(rec)); + writer_reinit_block_writer(w, rec->type); err = block_writer_add(w->block_writer, rec); if (err < 0) { goto done; @@ -251,8 +251,10 @@ done: int reftable_writer_add_ref(struct reftable_writer *w, struct reftable_ref_record *ref) { - struct reftable_record rec = { NULL }; - struct reftable_ref_record copy = *ref; + struct reftable_record rec = { + .type = BLOCK_TYPE_REF, + .ref = *ref, + }; int err = 0; if (ref->refname == NULL) @@ -261,8 +263,7 @@ int reftable_writer_add_ref(struct reftable_writer *w, ref->update_index > w->max_update_index) return REFTABLE_API_ERROR; - reftable_record_from_ref(&rec, ©); - copy.update_index -= w->min_update_index; + rec.ref.update_index -= w->min_update_index; err = writer_add_record(w, &rec); if (err < 0) @@ -301,7 +302,10 @@ int reftable_writer_add_refs(struct reftable_writer *w, static int reftable_writer_add_log_verbatim(struct reftable_writer *w, struct reftable_log_record *log) { - struct reftable_record rec = { NULL }; + struct reftable_record rec = { + .type = BLOCK_TYPE_LOG, + .log = *log, + }; if (w->block_writer && block_writer_type(w->block_writer) == BLOCK_TYPE_REF) { int err = writer_finish_public_section(w); @@ -311,8 +315,6 @@ static int reftable_writer_add_log_verbatim(struct reftable_writer *w, w->next -= w->pending_padding; w->pending_padding = 0; - - reftable_record_from_log(&rec, log); return writer_add_record(w, &rec); } @@ -393,8 +395,10 @@ static int writer_finish_section(struct reftable_writer *w) w->index_len = 0; w->index_cap = 0; for (i = 0; i < idx_len; i++) { - struct reftable_record rec = { NULL }; - reftable_record_from_index(&rec, idx + i); + struct reftable_record rec = { + .type = BLOCK_TYPE_INDEX, + .idx = idx[i], + }; if (block_writer_add(w->block_writer, &rec) == 0) { continue; } @@ -462,17 +466,18 @@ static void write_object_record(void *void_arg, void *key) { struct write_record_arg *arg = void_arg; struct obj_index_tree_node *entry = key; - struct reftable_obj_record obj_rec = { - .hash_prefix = (uint8_t *)entry->hash.buf, - .hash_prefix_len = arg->w->stats.object_id_len, - .offsets = entry->offsets, - .offset_len = entry->offset_len, + struct reftable_record rec = { + .type = BLOCK_TYPE_OBJ, + .obj = { + .hash_prefix = (uint8_t *)entry->hash.buf, + .hash_prefix_len = arg->w->stats.object_id_len, + .offsets = entry->offsets, + .offset_len = entry->offset_len, + } }; - struct reftable_record rec = { NULL }; if (arg->err < 0) goto done; - reftable_record_from_obj(&rec, &obj_rec); arg->err = block_writer_add(arg->w->block_writer, &rec); if (arg->err == 0) goto done; @@ -485,7 +490,8 @@ static void write_object_record(void *void_arg, void *key) arg->err = block_writer_add(arg->w->block_writer, &rec); if (arg->err == 0) goto done; - obj_rec.offset_len = 0; + + rec.obj.offset_len = 0; arg->err = block_writer_add(arg->w->block_writer, &rec); /* Should be able to write into a fresh block. */