From patchwork Wed Dec 8 21:49:36 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: 12665307 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 25CE1C433F5 for ; Wed, 8 Dec 2021 21:49:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237611AbhLHVxY (ORCPT ); Wed, 8 Dec 2021 16:53:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234666AbhLHVxW (ORCPT ); Wed, 8 Dec 2021 16:53:22 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7746FC0617A1 for ; Wed, 8 Dec 2021 13:49:50 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id t9so6465553wrx.7 for ; Wed, 08 Dec 2021 13:49:50 -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=OB+CDOyrxKNWyetGQkSPP8KzYXqK/12B8IKMFPLDCcnHrBMdFnHj2+q97bV1VZ9Ox9 IXBCYDRsPwBhzArIocnn1q8nw3Zq5HFiw1Fy3Oi7nvg5Bmb7RGb/H/bxg8juY9EPJHoW 9tye7lKh2N3k70m+2c/O/jE0lhUWHX+yWeWsYmaygH+uDsfEBM1XOKG75NWOZZBsg7rd Du1Z4lkVLqRc1ksLjal25TdmajbkCOUxQA52wP+V4Lw2BCE1yk5QuUMlCJF3+2oRW2S4 F2ew7VbTjdEQ/KoEjO1Guc1wBgtAi3acxILNU+yxDwDpFlklqeg9toP8F/SchNrfU5dT qvdg== 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=E5M0BOyfinGgdEXSXcL2tPMXTD53m6XAfQRnEwizcxI0fSi1aNroYeJdyFPtFcvJxl 26+Sl0461RSl3SiPWQdt2kv599AOSpUcqZFJ8b5P4tNM6gSHx9i+lZAMaEDN308YYuUG l+q86BXN1tSRcPPsPSt4RS000ASfvkAD2ffI3DptJQ9T2BdTtvjeH4cF++eIeOYPMJyJ si0x7uD0MOpqy0T/F5y98aG3x5KCrp2nBAuI5kMXi1kGqcYDIgDVkjXMJ0UCsuXLoIk5 H+V3tJacSsh3jtPOlenVpOTxl53VYKOru/E+UNxr6phVsywElplTXPhv775s9lR03G23 APzw== X-Gm-Message-State: AOAM533eiKRvZT965dst1v1e/X37zH/e9pHYHMROdRyL+Eo2UtV0/cmL tSnOGCN2d2KtWaDrAc8oNcw/NOs4NFM= X-Google-Smtp-Source: ABdhPJyacIO6Y0Sg4PEG2n0F3MUs6e9SCrZUA8cv7PjYg9oBJlVW7dPNzpSwmoUKJi4VeqTTa9/zNw== X-Received: by 2002:adf:aa08:: with SMTP id p8mr1538238wrd.572.1639000188964; Wed, 08 Dec 2021 13:49:48 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id h27sm7833232wmc.43.2021.12.08.13.49.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:48 -0800 (PST) Message-Id: <7c033815183cc8ad7a76bc2b9456b17041107c2c.1639000187.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:36 +0000 Subject: [PATCH v2 01/11] reftable: fix OOB stack write in print functions Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 Wed Dec 8 21:49:37 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: 12665309 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 98E3CC433FE for ; Wed, 8 Dec 2021 21:49:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240001AbhLHVx0 (ORCPT ); Wed, 8 Dec 2021 16:53:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234681AbhLHVxX (ORCPT ); Wed, 8 Dec 2021 16:53:23 -0500 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AD5CC0617A2 for ; Wed, 8 Dec 2021 13:49:51 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id o29so2817081wms.2 for ; Wed, 08 Dec 2021 13:49:51 -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=Sz2gS9C3a6Ypo/mE/oVriGH4YMP1EwjLNkuHgyL7+Sw=; b=qbF/WlXRvD8gQ784cEAdY2AKxTEWYDhVL1S9LRl9NU1lFs3TnFHi412fmwD0m6a7gJ r5FAauOBP4QO0D+ug+td9TAxl48WcXLlU768QMKf0FqRQI8fXcdYIz6hRKRSYLnQMraK PXj9r99O8Mrxm+ADZ288cro6v1MuO1g2JJGuTT/4UcZhM6BQZ54XiYQNqeMtjS9kVRW1 bXsiQubaDM89y34qc0NOa4F7jrd2ZcUvryKOAx/HBXXiyN5aHFOONgYi1iCfeKn/2L8e aysnOpdpGhZRLsOQHXRmx9lOjRsOzy5S1FtlJ+jtAQJ/GZLvhnHLpYmmtID8H4ep1Zqs PDHQ== 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=Sz2gS9C3a6Ypo/mE/oVriGH4YMP1EwjLNkuHgyL7+Sw=; b=qVk6kuAisOcsslwIuqy9owF+mOP/RWMw0qmEWcglYhUKFBzKfJEK62UqScarhlGt5e lWfzNZvcPKjb1e2IwCxvWlUf0+MGXoDKkLRohnldxtUnPFu0qDl7/XVw4TuHHki88m7S oYFdYLAsXVZiww5eM2PJRfxr8OimfPIhylk8M4blzq3Q5YNR3N8ZSp23eqw44qVSGCil KdNP9GHIvoTBNu9U2czIW+a5MkQl+xpsZOW8PePzv+JiHTS6x/gZkh4OZ6t4HiDTYuuA Cdw3KnAa/bfMmWiiH4a5Kuc5iDbn/RNV18zbzn/sD6jJCQasFWy0CULcfZLPd2zKOfhS +pvg== X-Gm-Message-State: AOAM530er7ZNs5N+umaPycA34t/ie3yQ8YkNq/Z8UTF+ZNcDwzu43pDK /8dwecjMpAcUxzqWdYvU/06C474Tms0= X-Google-Smtp-Source: ABdhPJzT4yR0i5hhgX3Ehq6HrVdubWVkJ/ATn9shKxzI9x+R9/JAnqEMMA/PHDaU77oIxp8uFtd7cw== X-Received: by 2002:a05:600c:3486:: with SMTP id a6mr1685820wmq.32.1639000189665; Wed, 08 Dec 2021 13:49:49 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f19sm7798059wmq.34.2021.12.08.13.49.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:49 -0800 (PST) Message-Id: <1ddcfe61ebc2c4ccb5fa82380ac00a62bf3aebdf.1639000187.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:37 +0000 Subject: [PATCH v2 02/11] reftable: fix resource leak in error path Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 855e3f5c947..79b6f3aac72 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; @@ -203,7 +206,7 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, uLongf src_len = block->len - block_header_skip; /* Log blocks specify the *uncompressed* size in their header. */ - uint8_t *uncompressed = reftable_malloc(sz); + uncompressed = reftable_malloc(sz); /* Copy over the block header verbatim. It's not compressed. */ memcpy(uncompressed, block->data, block_header_skip); @@ -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 Wed Dec 8 21:49:38 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: 12665313 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 417ACC4332F for ; Wed, 8 Dec 2021 21:49:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240094AbhLHVx1 (ORCPT ); Wed, 8 Dec 2021 16:53:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237603AbhLHVxY (ORCPT ); Wed, 8 Dec 2021 16:53:24 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01929C0617A1 for ; Wed, 8 Dec 2021 13:49:52 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id c6-20020a05600c0ac600b0033c3aedd30aso2738562wmr.5 for ; Wed, 08 Dec 2021 13:49:51 -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=j36lfctMVzSyWmiNML11FbAVejkxmhnzBbtU3dl9pnrwpcpvFOslWchiwssaBDr+qx h5ysMGpDUW7MeriNqkGdCOeoOwp1XgoxuHuHFBA7UDUYRhi+Z4Le76/u6kQ7R6Oxa6fk BMLCqGbQTECqZB3VMMbZHQcCSmqXEMsgOzkaXqna6rskOq+t+4TcR4JUXkRG2aRhX7OX HFwKgR60Rwy1ubcHq/DQxkTZkA+/2N7iKV2UBEkbqcmNDKtQdAklCsoiNVuQyMWUzAtC 5i7ulLsl/fSuLqrQO0by50KvUN79SVfo3vtu0p5cml58AZ7MnyASwEtR4oKq3pq+64M+ /sHA== 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=B9ivOe8PAOgrnfLmnBCCVdhas3wLYFb1kQnT/StwwQdA67HOPfEdjhx5yr/ay3hm9X GxyyeBI9yh1HgvH8imRKFzQEqb4NlqgrEWnHaJR59bMJfhb8qHDZoNp5f+/gtN94msJx +1t77Y775MI3qOpfA+OTG61mehLPjQKAhUEgDc9jDeYeliPT8t52XJG2Y3QPorIk8e36 DL720PreM8q+sqK1CnKLA50WZQuD1hCLA7aJpf1w8N6SCXo3405Zk6oFkqicCgUeM44r GDl5NKO7blevlB5FNcJ2RxEoi7tzkEw5reMhlplqdTEWumHsoKpT+xolhWBack6OelYN KnOQ== X-Gm-Message-State: AOAM5330dViJW4hZijJJONxu44w5IG2w2CgpRYltvXqmdB7lx0AiCr6e tPynuLM22L/UD0HgAl1BRDwz9DMfZNc= X-Google-Smtp-Source: ABdhPJxLjp8NCv4GtGPw/XUgi6VK6mU4Y4Kdc2Q4CgzSos+3CURtjcmGcXAGCTmDeIPeNn7P9xuQNg== X-Received: by 2002:a05:600c:34c2:: with SMTP id d2mr1757175wmq.102.1639000190364; Wed, 08 Dec 2021 13:49:50 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m17sm3636344wrz.22.2021.12.08.13.49.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:50 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:38 +0000 Subject: [PATCH v2 03/11] reftable: fix resource leak blocksource.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 Wed Dec 8 21:49:39 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: 12665315 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 40924C433EF for ; Wed, 8 Dec 2021 21:49:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240339AbhLHVx3 (ORCPT ); Wed, 8 Dec 2021 16:53:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237618AbhLHVxZ (ORCPT ); Wed, 8 Dec 2021 16:53:25 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E66FC061746 for ; Wed, 8 Dec 2021 13:49:52 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id i5so6487229wrb.2 for ; Wed, 08 Dec 2021 13:49:52 -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=JtstPlWm2erKcX0Egh9guZzuJ2D70SoXGGiZtrmQAsTYy+QCJunDEpxEZ8rl0KpLpJ edsvHo5e3KGPpQl7tDJeWZocXfC+I6ldWwEDXd7v2u/vptjyxaZ0dSP3m+sE1/waG/34 pp6sIoMBMcpCV1VZQq1aRolsBnqJSftVm90zV7CZsvNBLgAROXQntxhbV5H6iF7q2ZCf hC4L2Ha5vAvppu37RnJezjR/x0ZPXxwoBVpiUmihzBnOxmbQu9fGOB3DaoOYnchQbhrB enQmuAtcqoM7Q9kZQPuFiLUAl0e7SEdHttUmTmmpE0toQtCAcjVGWMUpfwjD+JgTe8CD hcIw== 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=ypmNBRF5a2tS1+OT6hPtay9yCGBSyOqtSLrJ3sn6HXX7MK3vAxtaBY2lDbC/j4cHRm ONbdBTEG1snTcfQSe+tEZzpjiUgHLhpsKZ20cL0n8S2aeYUCfunNRUqT7J9CDC8LnOJ8 j1k4twTw5eUsJfahnqturoZ8garUoGbdk8jTJZBgdP08ulKHX/xLYRwrCS4Uaoah1foR dOghlN7okSYR1Z0PC7XzdoXT0HaV1lYCS0G/NCa/TXmKt7ORLYLm1ORVboyU43SN/Hx5 ZEahLLoEP+zkBUjsoo2gE37lkbQtg8pO6sovOL2kwhizE9N1S1jF3wlvjG3ryCmsNt/V SSkA== X-Gm-Message-State: AOAM532V3YuoFhyyHCTDxw2l+n/Bd0iuAKIp91Vib9OQf/ckmD5s8hA/ 5tSVr6Zs9QhdcUvqbEdFIvpXafPef2U= X-Google-Smtp-Source: ABdhPJwsCiDpW/IX8UgskGqeIzgaT8svAoUdx4K8HogKXbGkc83+KtQ0vpGjHq/dKI+v4FwoKglQfw== X-Received: by 2002:a5d:4704:: with SMTP id y4mr1491154wrq.85.1639000191019; Wed, 08 Dec 2021 13:49:51 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o25sm3867637wms.17.2021.12.08.13.49.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:50 -0800 (PST) Message-Id: <9063137457be97352e99538808c914339b2e93c6.1639000187.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:39 +0000 Subject: [PATCH v2 04/11] reftable: check reftable_stack_auto_compact() return value Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 Wed Dec 8 21:49:40 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: 12665317 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 14D5AC433F5 for ; Wed, 8 Dec 2021 21:50:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240501AbhLHVxd (ORCPT ); Wed, 8 Dec 2021 16:53:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229507AbhLHVxZ (ORCPT ); Wed, 8 Dec 2021 16:53:25 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 339EAC061746 for ; Wed, 8 Dec 2021 13:49:53 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id o13so6403725wrs.12 for ; Wed, 08 Dec 2021 13:49:53 -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=R+c9uwgWKTJdxpPDVAcNaECEciLE7541l6VfZWljJ7HKZn48Lhql0i/uJ4QEm/ESQS vsZu9CRK1wHsNnAYx8vCqfMBDXw0hiIEq3FlQ4YZqP53ejM3d5QyKT0bCiuaUhca4CMf VCIoaSmFTR8W6nXpQk+k0LFRFTh2zuA/9ehZNYnzgamh2Njs3NLdaBizPfCcROUkCrXh TpVqL5SpPPkxeAboK9mQ5zCroi0xkZgMM1m4s5HxfEHnm7myKM0JVV0mC2uHD3c8tN5x YnROrgJ80+KP2wbhaI0uWWCJuiTIVKiynL2kAHFOXHMbkSqQ/R9Urm3irw0A+BUWcFvh ufwg== 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=5K80O4/ItToJAv08n9KppuLhHOCm8SfdovkrI+6FvBYzsEqHbD64E+TnVWkRuPQi7t CTYhVumxQykQHqNTygBP/wDImbPbtIZJx2xhqQExeetvd7s48/6CKifo/CgqDwL1iswO R6MEGgm8ohNipXCVaJZT9F+LpmimOUNKkbj6GmRHf9+hoNquARgJmfm31wucqZA55WHg RDHH77vFTou9L/ElPWV4sthWIUC5fUuYyVC29AUWjk2KN1EJ0bOPUNgQWmebzNnWUQdA iwdoy4fMySSOD4eUJpQ8JgE1PRxbRFGxeu3WDXeT6uvt5x38QVcyQP9sWObbq8TzsVMH aY2g== X-Gm-Message-State: AOAM5337/AWP84NQeuoZ+xZOQ1tKdjqLjAZ7wKK5zXFJL6EJ/KTfSZGv thg/y6uMVOev9GaLsEuLT8YSQKj//HU= X-Google-Smtp-Source: ABdhPJxNDXuyBsQyM6fYo3VrLGVyBSxS28SdhV3c68oj8Ni36A+RNnQ/yea6J2QljM1nh7B/y2gA7Q== X-Received: by 2002:a5d:6091:: with SMTP id w17mr1541217wrt.65.1639000191675; Wed, 08 Dec 2021 13:49:51 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d2sm7366260wmb.24.2021.12.08.13.49.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:51 -0800 (PST) Message-Id: <5020be156aeb5efe08c0377f15d8c27fa5c4d588.1639000187.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:40 +0000 Subject: [PATCH v2 05/11] reftable: ignore remove() return value in stack_test.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 Wed Dec 8 21:49:41 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: 12665319 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 D0447C433FE for ; Wed, 8 Dec 2021 21:50:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240522AbhLHVxg (ORCPT ); Wed, 8 Dec 2021 16:53:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239997AbhLHVx0 (ORCPT ); Wed, 8 Dec 2021 16:53:26 -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 E3900C061746 for ; Wed, 8 Dec 2021 13:49:53 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id t18so6412290wrg.11 for ; Wed, 08 Dec 2021 13:49:53 -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=qA+Dv+tMHZneapriRbSVKpTRDGidk8etOG2aZKvLLVUowDAftni+I8YlothgMcqs0M FkF9YrkZQ/epl2REmnjXpbsCMis3OqSe/Dg5ezTWSSNzQYNcrKdYszaD/ky9LQ67wD9k /F0LmGwUWwLzemMhD5nPLBUCO1O9k+hMeVXGOe0IJoZl0Guf44QU2LvGvXYGFqoHqILn Yni2l5AWGiRqCSbf3XjvS8rpLep/aRYz9J5Pc3AsSUePjzFUqfOcN/ywrPwW2SHhpyFM pp9QrG31g7zBDAHD34JzUuwvOfAsBxtZg6wS2Avw9rXMtlLlt4bIChKTohV+xE5z332T +MqA== 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=rc1L7D/O3XAJwCd1m9qCxSqeoKyhHWvAxhTjzC/DmsUohlFWFJkzAcPIyehZbnZTQ0 +2HZ8Z7COH3wiDMXnW/sE45qAnFFSlu4V6tBRQ8GN8qvaYCILkzrZ9giKTniWUxOj/EJ rdStz2aCLSgDhpUVSey1/BLP9H70eYL0u77taTUcWQt1yhZesGi8sA5h3gXSpf2yM1ka ALsdtn/Jlz4Jp67x7xXmqXylptCAycMbCoLfyAUV0AbRItVxujxEBXMZNvnCmJVOm53Z YupeYWamW5rZgeDRM0ed5BVOSIuVjLMZrs0PH/atjI+qP8OKjf4D201/6xwSfuadsKsN S33Q== X-Gm-Message-State: AOAM532jvZsTYtU5bIcwkGC/xYeBfwuk/KMC+4JGequsoR40JKpaLzSu x2akSYfkE+lUKOqEnPuC0SbRtBdV46w= X-Google-Smtp-Source: ABdhPJwN+0jx0vMtKi1Yj2N8C2lYD2acRauwhp3WudfGY+hmFx74mnfLGxYE4a3aT29IvZwVewBqLw== X-Received: by 2002:a5d:4bc9:: with SMTP id l9mr1516094wrt.503.1639000192372; Wed, 08 Dec 2021 13:49:52 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w17sm3777127wmc.14.2021.12.08.13.49.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:52 -0800 (PST) Message-Id: <64c18d01cad70545e43c6a8e8d0547881051055b.1639000187.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:41 +0000 Subject: [PATCH v2 06/11] reftable: fix resource warning Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 Wed Dec 8 21:49:42 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: 12665321 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 94749C433EF for ; Wed, 8 Dec 2021 21:50:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240536AbhLHVxi (ORCPT ); Wed, 8 Dec 2021 16:53:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240033AbhLHVx1 (ORCPT ); Wed, 8 Dec 2021 16:53:27 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA459C061A72 for ; Wed, 8 Dec 2021 13:49:54 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id u1so6397459wru.13 for ; Wed, 08 Dec 2021 13:49:54 -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=WMo+TbC2YIt0dpXyaNnSilQxJoYv8xFd+nk66k+0NiFEs37koFhhsWAGnoP7B5+O7f RA2fo/d4pnjyuuVu5OsbR6tqszr0Mww4nhya9UCfllMCNdjB0lS10sXlTSX6Zjy/rIi0 0GjX3k6EHRx0hZe+Ja82Vuh9fAhBXsvHgwx/FBHJiJ0tlUbYaSEgJ2Rmj/ByIvKYF/dH Q9zl3Pl+m4Tt9ZkHo/FRQdiP5nhRbtk75964PfvZdkDv06AWJHlq6n8fKMe/XnHwrPJF nzGlC7sncbnGQrhapTF6qdSEd43IXbbcaRWDM/Z71/bXLaAmKIbD3INd+EcG/CA4OIWN QgAg== 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=Q8hg+8qKt46d4mlo8yh/AS0iiRL+oHWwW5+icCk6rjdcN8YlaKVGcu6grP/NUyMrN/ zHqukGHyrPI27dNt4O+7NFsb/y8vKFlvnx+IahBNnlVcYDby7nT5Hb9W7NE8XHV32Gm6 JmOztMTn+i71I35K2pafvucqTSWcsL1yz+zpe9SSALe5ViaVn+gxlQ+tSWGXySDTdOfe kqUbmYSoxmP0zCdMBLgTcitUlqhRT8KHAzbExbY+obu/PfU66siPBRMkymqU69S1qKwh E18g9/c68dr7fR5xetXbjim91yzM3+uCBG9TnP9vxw0Tq91ouS2BUG88ZW0bO1kDq3Vk jDeg== X-Gm-Message-State: AOAM532elQohZe6g30qvQxd6jZZWhAp9zRy2hfk5dHECjOXGJs4p80Zp BC5n7MXpdkEpu1wSMWusM4fEiMr3J0A= X-Google-Smtp-Source: ABdhPJyqvomfav/XeUCDDsHcqeCyp13zivoQHp0Ce7K+F946MVnaSNEOln7llvLTEUUeeTxdNlin7w== X-Received: by 2002:adf:dbd1:: with SMTP id e17mr1509617wrj.480.1639000193241; Wed, 08 Dec 2021 13:49:53 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m17sm3636414wrz.22.2021.12.08.13.49.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:52 -0800 (PST) Message-Id: <700387ac5d386aec5c1525223bd5952b037b15cf.1639000187.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:42 +0000 Subject: [PATCH v2 07/11] reftable: fix NULL derefs in error paths Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 Wed Dec 8 21:49:43 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: 12665323 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 ADA3AC433EF for ; Wed, 8 Dec 2021 21:50:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240499AbhLHVxl (ORCPT ); Wed, 8 Dec 2021 16:53:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237618AbhLHVxa (ORCPT ); Wed, 8 Dec 2021 16:53:30 -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 11AFCC061B38 for ; Wed, 8 Dec 2021 13:49:56 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id a18so6460026wrn.6 for ; Wed, 08 Dec 2021 13:49:55 -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=CgxF01eesAt2N12zppZULnuPPcWo0qaBj3g90nNOoAgHd1j1SiDE4KTDWSr9ftEMG9 gH5gyl2a7vpcNuCQZ/csDzFwUylCLLfqUZ0Qqe5cfuXdn9e4pDVKvbPeEXjWpQxkLZgD Z5HXCT4OM7tLuQ6WheXgWQvXzZ1WZHeQsXMb1TQtfm+mmdJeih41HxX8WGOEte3TVG6F r1BSnLs3wrVCGFTrg/H78YH8O88eZ8AXpVxqZbKyCjPnnLoUv+LfYWcWfH2BpVG2okam JfIxAsL+oY3kY8+2N8lBSnOzjdUsQmUbJYDO9rv9Ui61vwiwHTqX8wwdqaal9OFubDA6 6EwQ== 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=39w8cGMDaUyZaXzuvsstTc7KYMxpDZldWgW6BGndTAOSLSOgpuipAvb8EJnA7aMyhe 7C5saFzu2mz17T3TDBI7dz3K9q2L7oGcF88bg0y2z8uI6KP0XBKshDnVNMeIv/I8zHWG HnKxqK1fNzxACQw3WaFCnsJnRxLBdzWDChfzpwduyntlUPdpcPINRgSlIv1b9AjbCpPG 4oKlb2fs6DhwAMt3QNbq4AydpRVONq1nQPj1yQQRztu8fL+4eYF7FkDZAxvNd4/qXef4 lu3SlRLKM0fPgXes4Qce3o/iNGzhRyAKpAYIUASjALVd1mR2Uz5A4Q+Kx1AsaliPafAq Zm3A== X-Gm-Message-State: AOAM531JqR/IqaI4FijWqWMjV8Mn6SgEE88VRo6fmr+8FjUmEtawRtzW t+YTRo0wkcl2qmgfgag0bWaarKliFW8= X-Google-Smtp-Source: ABdhPJxCsKITNxKqchbjTBw7D2y/RDSpGmsM3ocE3ZmM94SmqWMZvdrwfEkZFGRP6y/jWziBJppL4A== X-Received: by 2002:a5d:4ed1:: with SMTP id s17mr1631787wrv.310.1639000194253; Wed, 08 Dec 2021 13:49:54 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d15sm5287158wri.50.2021.12.08.13.49.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:53 -0800 (PST) Message-Id: <713f1d09f6837989e57026cffcaffea755d6a3f7.1639000187.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:43 +0000 Subject: [PATCH v2 08/11] reftable: order unittests by complexity Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 Wed Dec 8 21:49:44 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: 12665325 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 10A6AC433EF for ; Wed, 8 Dec 2021 21:50:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240525AbhLHVxo (ORCPT ); Wed, 8 Dec 2021 16:53:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240510AbhLHVxe (ORCPT ); Wed, 8 Dec 2021 16:53:34 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C40AC061D76 for ; Wed, 8 Dec 2021 13:49:57 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id t18so6412509wrg.11 for ; Wed, 08 Dec 2021 13:49:57 -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=J+/e2RpwNbxg4jXYqrrr/b8U4fZxasc6Qmmm4bpPyXEFiHBq04wtAQd/bJJuQNtpQW 8wdgLst48waYdwQLjq+iRf1AoXhOZdFtL455fyoHTqpzXop7bixzznIdmME5TZf+PH6T DvofWfCSYtoG+CevIjPgjN4U2EFa5C7HipACGzoURqMzW06AkTs5hdyUTMAxo3c1ha/f 3vwW92GYfjzASDogspKk59KCNtTLp86M+pjovEjsZWQ4Iww2fTo5g2wWqhH57dweYUw/ webb4TEdYjw2OWPXfhpeLwpWtJrWpjShfnS/6Ej9zF4vnmK17fBVjlwkny1K1sgBb00H Cs0g== 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=AnM3J5kYIjnUyoIy4OQqEWKE/yhBdb6yVBeZ6NBZMjOP3HQiBsImLYLkRjxwtkNwtW yALHqaJI9WwOdRrzAulDNdxIKhO9ShU2o9VDh4ofRNt5xx5WjRmgt33/iWb6DYDtr7Ym gy2Vf4pNciMKeE1lJiTxkBXJAUvGK0cJWZ0Uh4WaB8VULBKe1wOjFvMhJWJoHq1G3k98 Mhe4zI00XIYb6kLFoOR0dZzHXBXUROzwxdFmo5NzVLqhbqkgoWn6uAJTbCmC9eOttGFP mhjVI/sZwLTwmvev0GLAGbt6P51qfxRVxZslovwd/0q2DpQYE23arZCyR/m+XJkQXOe1 EJIg== X-Gm-Message-State: AOAM532DxNniFSDnmMi0ryfk1wpA15Xu5ZS4eV1DMls9FDznJBhwexAQ 0dSwS8AA67es4OYVojEZi0w8SFvBTp4= X-Google-Smtp-Source: ABdhPJxo0ddzZZdyeM3mAu6aW/7x82HeVFBpNZmMHN688svVYYtUY2s+z4OUUyFihpMApWNjUbTwdg== X-Received: by 2002:adf:b355:: with SMTP id k21mr1435731wrd.451.1639000195593; Wed, 08 Dec 2021 13:49:55 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b13sm4062315wrh.32.2021.12.08.13.49.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:55 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:44 +0000 Subject: [PATCH v2 09/11] reftable: drop stray printf in readwrite_test Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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 Wed Dec 8 21:49:45 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: 12665327 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 4DC6CC433FE for ; Wed, 8 Dec 2021 21:50:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240582AbhLHVxp (ORCPT ); Wed, 8 Dec 2021 16:53:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240358AbhLHVxk (ORCPT ); Wed, 8 Dec 2021 16:53:40 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 829D3C0698C8 for ; Wed, 8 Dec 2021 13:49:58 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id d9so6471310wrw.4 for ; Wed, 08 Dec 2021 13:49:58 -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=IY/CPJQ0Q+sLVf7jURIMds3wahY4HaQuVDcXfMmDpdE=; b=fkxddrRnYKSqvAFy0bHm04B90J6lUIXARzrshRRCarAxljnBeDPf58EavcXgjkhO70 GnO1uRLznLCkQc8q79joT8dfF2v0JC5auNHZ9kD+TbfLPQTiwh5SBqQ9yaQAdmN7ItT4 R3YrChh+gWI7P/S2/npxmPBuxvk9cfHalCBgSBjxBkyO2tbxmy3gL7PMOIoZBqv+xTC9 YkctGwTsVVBOPsPhzJmid87kQGl3bRcZgExJwPTserXTJuoyfnwoD1BZwrErnnY8cq1P I6bwjULSLSYn41QJGM/6BqJm/gDIYPzvkzeREWjm0lT3mkaIpKclS87EFbpvCh/E9APY faWQ== 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=IY/CPJQ0Q+sLVf7jURIMds3wahY4HaQuVDcXfMmDpdE=; b=onmKmcqWoBUTrnG/o/kd4krITpGHH0knM5uGMn99hGH+Y2WBGoG1SzUq8tj1Uaa+WF AL2dMZz8GdfP+EkBn+DiVlC2MuVC2XTaq9JPrzH6TgL/FNm8HfCzent5vIuGfZzOW+zS MKizqEfr9MWhpYMYjfnaRBVXhaVKIUnwj4w1VtCtWJdmioZB8cSmGR6Sj/ShasE+wpjh VEEToJOIcxjYfXhOuG+I8G/jciv6fhMRAGmw68F/gI/LUpH4w+vnl+WoZksN+c1zjzSu xq4ueIDYzuNTOb0YY8la5Y5J/TngeLb5OJvVyZT+g7uotY0QU+jLVSYsBThsC3mNZar5 g0nA== X-Gm-Message-State: AOAM533H9YIC4qmkOg7FOZB2DzK1MhovjeR15oIVJUXuKZVjInhXo2C+ JSXUB55KHRZNmPQdd8ZjpfMwaGxuRAw= X-Google-Smtp-Source: ABdhPJzP9Fmq6Wu/28HyA0GLeLoabi/VfS4UGBS9874Ht0NV4BfLxP0lFnnxZtAsGJb3+KjZjvGpSw== X-Received: by 2002:adf:cc8d:: with SMTP id p13mr1583875wrj.274.1639000196688; Wed, 08 Dec 2021 13:49:56 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p12sm3816586wro.33.2021.12.08.13.49.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:56 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:45 +0000 Subject: [PATCH v2 10/11] reftable: handle null refnames in reftable_ref_record_equal Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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/record.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 8536bd03aa9..8bbcbff1e69 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -1154,9 +1154,11 @@ int reftable_ref_record_equal(struct reftable_ref_record *a, struct reftable_ref_record *b, int hash_size) { assert(hash_size > 0); - if (!(0 == strcmp(a->refname, b->refname) && - a->update_index == b->update_index && - a->value_type == b->value_type)) + if (!null_streq(a->refname, b->refname)) + return 0; + + if (a->update_index != b->update_index || + a->value_type != b->value_type) return 0; switch (a->value_type) { From patchwork Wed Dec 8 21:49:46 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: 12665329 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 DF8C5C433EF for ; Wed, 8 Dec 2021 21:50:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240473AbhLHVxr (ORCPT ); Wed, 8 Dec 2021 16:53:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240476AbhLHVxn (ORCPT ); Wed, 8 Dec 2021 16:53:43 -0500 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E13BC0698D0 for ; Wed, 8 Dec 2021 13:50:00 -0800 (PST) Received: by mail-wr1-x42a.google.com with SMTP id a9so6448468wrr.8 for ; Wed, 08 Dec 2021 13:50:00 -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=OJiTlztBhf/+mXulKX+KFM2Slb5ga8731mhov9tRNZ0=; b=J+rmPwKghLqrn44EZVmvfJU54HuCrryA2iXa8ZKHyVRh+dpCxJA0+Fz8M1ni7uRCOS j4qzB6KhYwjLKT8yOQ6wH783Deb3KPx/7cJ6kgwFfPMPuPI491icEgO3ki606em6lTCb N05g78jXopR4Hotk3wogac5kNuSJWomDlLpquywd7rhLjDZNATHsGteKygrIXB90aD49 b3mXpu5p8fXNtw+e7iUIwUi8teRjKMu4CuesmkaCPFm8TGfxRd0MCcBafhMoskA57R/8 t6MN0zKFGsjFpQo0cmYtns+R1ovPEOmO915Px16996GUnt4rh+AdYggKlHkKkCUaqDyK RS0Q== 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=OJiTlztBhf/+mXulKX+KFM2Slb5ga8731mhov9tRNZ0=; b=6zhA8erBlujaBsY6W+9ZE6zjEuS9uxZDjb11lAE6VZYtfHp9GBA+I4OVXYJJbG5qIE SuRJJNb2IutWy2Btd4KOCQs2GrZCEDOZ9OM1Sde+gaGklmvHJh9NhYKOF+7rC32O+OzB L5B6z4cS0kCU2z7hyq0YGhNe6wPF8AghzoKWTi1aKoi/WTdbQsDAo+1rAnzGKVNRTm4Y YD86DnOCUueAIBhD6psu2b7lsy7IQcdt9SB7j7j07alKan1bPVXNqFbc0GM5AL1N4/HK fWjiAmbtnEDidGUP/oXBH+RNHcizxnfsXr3NicdnmrFFah3T9+bSmh2N8ogjoAz0OaoH XSDg== X-Gm-Message-State: AOAM532YRgh5qRqdJzMEnFTWbU5tFj0SSTAp/DTHSXpt/pZ4w/sBNRPr TPgcfrsU9X9Zicg7kCLgCtgiRnl3LWo= X-Google-Smtp-Source: ABdhPJyz+1cEzXWRyXPfKUNBT+z2iIYL4VanSiRRC5vR5TYTo5509Sy/MfeveRCOMfeYDey9q2VSpQ== X-Received: by 2002:a5d:4fcc:: with SMTP id h12mr1614693wrw.434.1639000197981; Wed, 08 Dec 2021 13:49:57 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id z6sm3782892wrm.93.2021.12.08.13.49.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 13:49:57 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 08 Dec 2021 21:49:46 +0000 Subject: [PATCH v2 11/11] reftable: make reftable_record a tagged union Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , 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. Helped-by: Jeff King 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 | 181 +++++++++++++++++++++-------------------- reftable/writer.c | 46 ++++++----- 12 files changed, 329 insertions(+), 360 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 79b6f3aac72..0bc4a5d84b3 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..fa2ee092ec0 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.u.ref.refname = name; + rec.u.ref.value_type = REFTABLE_REF_VAL1; + rec.u.ref.value.val1 = hash; names[i] = xstrdup(name); n = block_writer_add(&bw, &rec); - ref.refname = NULL; - ref.value_type = REFTABLE_REF_DELETION; + rec.u.ref.refname = NULL; + rec.u.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.u.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.u.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.u.ref.refname); block_iter_close(&it); } diff --git a/reftable/generic.c b/reftable/generic.c index 7a8a738d860..4446b8ed360 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, + .u.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, + .u.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.u.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.u.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..a8d174c0406 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->u.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->u.ref; while (1) { int err = block_iter_next(&it->cur, rec); diff --git a/reftable/merged.c b/reftable/merged.c index e5b53da6db3..e9b0edec97d 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, + .u.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, + .u.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..9f04d84fed6 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, + .u.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->u.ref.refname) < 0); } - last = ref->refname; - ref->refname = NULL; - reftable_free(ref); + // this is names[i], so don't dealloc. + last = rec->u.ref.refname; + rec->u.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..ef322791fda 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->u.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, + .u.idx = { .last_key = STRBUF_INIT } + }; + struct reftable_record index_result = { + .type = BLOCK_TYPE_INDEX, + .u.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.u.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.u.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.u.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, + .u.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, + .u.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, + .u.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, + .u.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.u.obj.hash_prefix, got.u.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.u.obj.offsets, got.u.obj.offset_len); if (err < 0) goto done; - got.offsets = NULL; + got.u.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 8bbcbff1e69..8cfd67b21e6 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) @@ -1212,3 +1120,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->u.ref; + case BLOCK_TYPE_LOG: + return &rec->u.log; + case BLOCK_TYPE_INDEX: + return &rec->u.idx; + case BLOCK_TYPE_OBJ: + return &rec->u.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, + .u.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..1fe0c14a198 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; + } u; +}; + /* 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..12161c2895a 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(©.u.ref, &rec->u.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(©.u.log, &rec->u.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.u.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.u.ref.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ); + set_hash(in.u.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.u.ref.value.val2.value = + reftable_malloc(GIT_SHA1_RAWSZ); + set_hash(in.u.ref.value.val2.value, 1); + in.u.ref.value.val2.target_value = reftable_malloc(GIT_SHA1_RAWSZ); - set_hash(in.value.val2.target_value, 2); + set_hash(in.u.ref.value.val2.target_value, 2); break; case REFTABLE_REF_SYMREF: - in.value.symref = xstrdup("target"); + in.u.ref.value.symref = xstrdup("target"); break; } - in.refname = xstrdup("refs/heads/master"); + in.u.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.u.ref, &out.u.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,23 +220,25 @@ 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"), - .value_type = REFTABLE_LOG_UPDATE, - .value = { - .update = { - .new_hash = reftable_calloc(GIT_SHA1_RAWSZ), - .old_hash = reftable_calloc(GIT_SHA1_RAWSZ), - .name = xstrdup("old name"), - .email = xstrdup("old@email"), - .message = xstrdup("old message"), + struct reftable_record out = { + .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = xstrdup("old name"), + .value_type = REFTABLE_LOG_UPDATE, + .value = { + .update = { + .new_hash = reftable_calloc(GIT_SHA1_RAWSZ), + .old_hash = reftable_calloc(GIT_SHA1_RAWSZ), + .name = xstrdup("old name"), + .email = xstrdup("old@email"), + .message = xstrdup("old message"), + }, }, }, }; - struct reftable_record rec_out = { NULL }; int n, m, valtype; - reftable_record_from_log(&rec, &in[i]); + rec.u.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.u.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,52 @@ 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, + .u.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(!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(in.u.obj.hash_prefix_len == out.u.obj.hash_prefix_len); + EXPECT(in.u.obj.offset_len == out.u.obj.offset_len); + if (in.u.obj.hash_prefix_len) + EXPECT(!memcmp(in.u.obj.hash_prefix, out.u.obj.hash_prefix, + in.u.obj.hash_prefix_len)); + if (in.u.obj.offset_len) + EXPECT(!memcmp(in.u.obj.offsets, out.u.obj.offsets, + sizeof(uint64_t) * in.u.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, + .u.idx = { + .offset = 42, + .last_key = STRBUF_INIT, + }, }; uint8_t buffer[1024] = { 0 }; struct string_view dest = { @@ -370,31 +376,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, + .u.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.u.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.u.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.u.idx.offset == out.u.idx.offset); - reftable_record_release(&out_rec); + reftable_record_release(&out); strbuf_release(&key); - strbuf_release(&in.last_key); + strbuf_release(&in.u.idx.last_key); } int record_test_main(int argc, const char *argv[]) diff --git a/reftable/writer.c b/reftable/writer.c index 3ca721e9f64..f9544c5f97b 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, + .u.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.u.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, + .u.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, + .u.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, + .u.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.u.obj.offset_len = 0; arg->err = block_writer_add(arg->w->block_writer, &rec); /* Should be able to write into a fresh block. */