From patchwork Mon Jan 24 19:13:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722861 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 E6007C4332F for ; Mon, 24 Jan 2022 19:38:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346943AbiAXTin (ORCPT ); Mon, 24 Jan 2022 14:38:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352024AbiAXT30 (ORCPT ); Mon, 24 Jan 2022 14:29:26 -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 E9CADC02980B for ; Mon, 24 Jan 2022 11:13:47 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id a203-20020a1c98d4000000b0034d2956eb04so62436wme.5 for ; Mon, 24 Jan 2022 11:13:47 -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=l1ClT/iYWGksB0Zp15m6EJ9+FS/ahsYnhWO0zjx+q6I=; b=D+MxVKm2z61hp2+JAkhfFABs5CUAPj0CvKput9w7RUaLcdvpS0a8EUc3OP8ICQYFcw 3SvIcHfUfS3Y0hc9+fLgg9SYjLefJmLGxT7nc6mj3Wn8jo0OMepW9bBZ4GNncpmhPWrY XBLYRWrJiycuXwvTgbFQsW/yKwnzp4zeB9s+at9ue+H0vaHyjUI5g5IppdEs3y2q7TkL WSd9ljxuBaeGT/HAttbUncODqb8pboNxBvKmnzLLXhZvINpMDDu4oH160FyZndy2keM5 JbMdJOuKd+X1UZxTVEWzzc38jpiL5jjh/Xw8FkPhw0zQrqUL6+ltcpNfMT287jrblflN 35gg== 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=l1ClT/iYWGksB0Zp15m6EJ9+FS/ahsYnhWO0zjx+q6I=; b=AJZ5xBhl6bqIAANJCU4iqTsDiGRSEVv8C/6UoaCP6hc4lapNgyIo6jUiN9aK77OFJ0 kei1f5yvy/uF3LiqY1oCtDYbO8L9YKOsI0a9cCfE9HVWJeYtPRIxSYZ4n4FJj88AHk29 Xx8t4uk81lwB0O3Jd4mihUnASwFOmxGzCpru5FLYYjL+6WWynVeHzYSB5FRFP3aGdp8X LSjjQKyXsci20Mf/6Tvhcz8SvOAsHoMVEHeTNlzA+yglI1JRNe/NgfxKPfmfTy5kkJER LbGIzGJTVSr+WcnHqgypjXL44CvXr2g+nbXOFLOhHxdJzRIcEkOtbp+n5nUtRWaaAG/q RmwA== X-Gm-Message-State: AOAM530hUaKtKzeb74IgAWxHyDdWLfsXuESQRcTVM6RFTR7xVC4k9Yq0 G6x9NzY8l7kEQWTyLLyr8vpAioJl4P/yjg== X-Google-Smtp-Source: ABdhPJxW9cFYDSWC6B/xGiIH0y4pCIW9tahCCpg3A4oXC5joMe2HEFbRfkNIoZKheNIRzo4dPenx8w== X-Received: by 2002:a05:600c:3589:: with SMTP id p9mr3050867wmq.109.1643051626372; Mon, 24 Jan 2022 11:13:46 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id a18sm15847765wrw.5.2022.01.24.11.13.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:45 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:29 +0000 Subject: [PATCH v7 01/16] 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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 Remove /* BUG */ comment, whose meaning has escaped my memory. 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..1f236b47d1c 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]; 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 Mon Jan 24 19:13:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722859 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 C6F9EC433FE for ; Mon, 24 Jan 2022 19:38:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346785AbiAXTib (ORCPT ); Mon, 24 Jan 2022 14:38:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352025AbiAXT30 (ORCPT ); Mon, 24 Jan 2022 14:29:26 -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 E7677C02980D for ; Mon, 24 Jan 2022 11:13:48 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id a13so15475423wrh.9 for ; Mon, 24 Jan 2022 11:13:48 -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=6BcPZYQ0vVLjzOTN+CeTC1tPkkZaBRc4T5nwILhJrdk=; b=W0E6lg+OH5VvYwLZreuhQWq3P+Lz9Ey2zrmcz6lmSYNpz29vcKKxck+LYLY32IhTfn xw00uuk7JAiQJ7DYInnW8NeozMNsyNFDR5KYkG3jnFUUs6wi539OvXh8Ch5p8F0wT5ue NbRZzHG7kRrPLoA3PqtW2g3zp3q2E011Oqz1H9arFFDGn+LCRnENnnpS12fhsI0057g8 yTT6FSXzMsjfIRhneMzqckiTu3Hv1rfeg5b9xgc0EumLCUPum0v9ypenZao9tr2myjUz 6YB7/FmRTZ8LtYMwSITslHmHiV82OYGpXtLnbL2HffTyXvjDg3DY0oWK29ZmutWsA8o4 ViWA== 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=6BcPZYQ0vVLjzOTN+CeTC1tPkkZaBRc4T5nwILhJrdk=; b=mjBaNdQ+nMuUsgASygh+jbngWszw/rtuz95ySHlAiHTnt62ZcFlloAjCHYC3ekDzf0 stin7Do67wJgS/7ENahFSIWsPwXUYCNcvIrrXRB7V5uHXAWmu0As/GrtHqKMVw1UpbAv fhiM0zeoXxHhLhnpEnyX5xGWFInoU7HFdPLHoxpXTMVgLT39sh/s1ox3ybOsUy9d0WTh tKQJHMIQn8kwchua8bUEvv1mSahvYCZ/E4xYISDIfKVdpn/2ER5aythNX9LtoLWfyKGv gclMEe1RwZ2eLpDzk/h+/OGcWPnCcI+vlr6E78qsIZ7wKrQdZrE9zxlPIqlCyN8H+i0T mtNg== X-Gm-Message-State: AOAM532WoUaka9C4wQ4+07khqhDM9U6bLOMhr3nDB9eBRsMKE+3MxpEp XpjcY8GkrBNGovAIQuFENk+a90NKlRndew== X-Google-Smtp-Source: ABdhPJxETZnIsGladkPwu7EkFh9/jtrOO77dbQ1rqVW6IAX37vwyAuTkxrYrGa3dxudS3iCBSw5Y2w== X-Received: by 2002:a5d:5548:: with SMTP id g8mr2042339wrw.534.1643051627361; Mon, 24 Jan 2022 11:13:47 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o15sm170542wms.9.2022.01.24.11.13.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:46 -0800 (PST) Message-Id: <1c5441d2f7e81307550194f44d235174fee49441.1643051624.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:30 +0000 Subject: [PATCH v7 02/16] reftable: fix resource leak in block.c error path Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 Add test coverage for corrupt zlib data. Fix memory leaks demonstrated by unittest. This problem was discovered by a Coverity scan. Signed-off-by: Han-Wen Nienhuys --- reftable/block.c | 25 ++++++++++----- reftable/reader.c | 23 ++++++++------ reftable/readwrite_test.c | 66 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 17 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 855e3f5c947..5832f85501d 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -189,12 +189,16 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, 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 +207,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); @@ -212,16 +216,19 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, if (Z_OK != 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,9 @@ 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: + reftable_free(uncompressed); + return err; } static uint32_t block_reader_restart_offset(struct block_reader *br, int i) diff --git a/reftable/reader.c b/reftable/reader.c index 006709a645a..35781593a29 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -290,28 +290,33 @@ int reader_init_block_reader(struct reftable_reader *r, struct block_reader *br, err = reader_get_block(r, &block, next_off, guess_block_size); if (err < 0) - return err; + goto done; block_size = extract_block_size(block.data, &block_typ, next_off, r->version); - if (block_size < 0) - return block_size; - + if (block_size < 0) { + err = block_size; + goto done; + } if (want_typ != BLOCK_TYPE_ANY && block_typ != want_typ) { - reftable_block_done(&block); - return 1; + err = 1; + goto done; } if (block_size > guess_block_size) { reftable_block_done(&block); err = reader_get_block(r, &block, next_off, block_size); if (err < 0) { - return err; + goto done; } } - return block_reader_init(br, &block, header_off, r->block_size, - hash_size(r->hash_id)); + err = block_reader_init(br, &block, header_off, r->block_size, + hash_size(r->hash_id)); +done: + reftable_block_done(&block); + + return err; } static int table_iter_next_block(struct table_iter *dest, diff --git a/reftable/readwrite_test.c b/reftable/readwrite_test.c index 70c7aedba2c..edb9cfc1930 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -288,6 +288,71 @@ static void test_log_write_read(void) reader_close(&rd); } +static void test_log_zlib_corruption(void) +{ + struct reftable_write_options opts = { + .block_size = 256, + }; + struct reftable_iterator it = { 0 }; + struct reftable_reader rd = { 0 }; + struct reftable_block_source source = { 0 }; + struct strbuf buf = STRBUF_INIT; + struct reftable_writer *w = + reftable_new_writer(&strbuf_add_void, &buf, &opts); + const struct reftable_stats *stats = NULL; + uint8_t hash1[GIT_SHA1_RAWSZ] = { 1 }; + uint8_t hash2[GIT_SHA1_RAWSZ] = { 2 }; + char message[100] = { 0 }; + int err, i, n; + + struct reftable_log_record log = { + .refname = "refname", + .value_type = REFTABLE_LOG_UPDATE, + .value = { + .update = { + .new_hash = hash1, + .old_hash = hash2, + .name = "My Name", + .email = "myname@invalid", + .message = message, + }, + }, + }; + + for (i = 0; i < sizeof(message) - 1; i++) + message[i] = (uint8_t)(rand() % 64 + ' '); + + reftable_writer_set_limits(w, 1, 1); + + err = reftable_writer_add_log(w, &log); + EXPECT_ERR(err); + + n = reftable_writer_close(w); + EXPECT(n == 0); + + stats = writer_stats(w); + EXPECT(stats->log_stats.blocks > 0); + reftable_writer_free(w); + w = NULL; + + /* corrupt the data. */ + buf.buf[50] ^= 0x99; + + block_source_from_strbuf(&source, &buf); + + err = init_reader(&rd, &source, "file.log"); + EXPECT_ERR(err); + + err = reftable_reader_seek_log(&rd, &it, "refname"); + EXPECT(err == REFTABLE_ZLIB_ERROR); + + reftable_iterator_destroy(&it); + + /* cleanup. */ + strbuf_release(&buf); + reader_close(&rd); +} + static void test_table_read_write_sequential(void) { char **names; @@ -667,6 +732,7 @@ static void test_corrupt_table(void) int readwrite_test_main(int argc, const char *argv[]) { + RUN_TEST(test_log_zlib_corruption); RUN_TEST(test_corrupt_table); RUN_TEST(test_corrupt_table_empty); RUN_TEST(test_log_write_read); From patchwork Mon Jan 24 19:13:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722860 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 D1B5CC4332F for ; Mon, 24 Jan 2022 19:38:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345591AbiAXTid (ORCPT ); Mon, 24 Jan 2022 14:38:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352026AbiAXT30 (ORCPT ); Mon, 24 Jan 2022 14:29:26 -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 AE458C029821 for ; Mon, 24 Jan 2022 11:13:49 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id w11so3315768wra.4 for ; Mon, 24 Jan 2022 11:13:49 -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=AMPYXTx+eFm7luhlSKnYJkvbRFLbh+PvTxpJ8Mh2QOA=; b=oHW/bOCJgxJfcv4yY8j9a0joK9KaJeAwPyp6VFnYhK7UEALUR7UiCeUFkhTQdJLbdL Kfm6BoXFentZfYFGtB76C1Sxsgl7NFl5oYXGJaZk0tY+jOLTOOKV8mgIurvY4VgPyDhQ E8ag4crhJocWEV0e3PtJIf0o+LhBBzDxdNLRCuZ45khZ7Wh0hZp+2ZfzVmpJTc5NjePv 5Hkg7WI91he5luh1Zd1Dr5010xmoe3RNbrZOUtH/zukbZl1X1UG4xYcvmwx+wfD/JJtQ 29609OWMMu3wRJi3qRG4zJ2I/JiOrY/CwhOi9qhdphEqQkKZG8sG25BrAobbTnP9JfUj bMdA== 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=AMPYXTx+eFm7luhlSKnYJkvbRFLbh+PvTxpJ8Mh2QOA=; b=4mcEPwm/qhqX2GLz0TD29lRBsj7oN0BvfeM2GdvXrxgRUa1bO5yxbxoGT4lhAFQtRL um6lbajJQ6lBXD92/7wxicgupuoHetXKqhLc+UW7ez7pLR/Na/xlC2QJfBtMDLMGdOc0 Ue0hXP5McY+o/VBzzveWl/zZCiWsVLYrkEsF/F1xJnLKNQE16x/jfSSnjBl8VOMJCq+O ieTWO6BUDZdVCE3R2TrFCGGGnQC5tZeL7gwVWZYMSIkNjFeQHKJnXqFZ3361VS4a07Yl wrkkIDmRp8F5xHEJ162nM8BwcdnsQsgTr9IBbGKq2j2YKvLbRd6UWVWxmjrsXdpCdy8+ SknQ== X-Gm-Message-State: AOAM532fJDDjwVAzOhAP91fFrd8pR/1ULeQ0EtGDBV/srDaRWRZ5ew64 1i3egwRjA4Y7TaBDWLQjhUmQqlLe3YpDBA== X-Google-Smtp-Source: ABdhPJwgdekz6gT/R48Q6oddCqS8G3a2O3xGNWu2/IJ5HiaiG+X+ujRGmc+/9x2G8BNVm7CbEKQcKw== X-Received: by 2002:adf:fa8a:: with SMTP id h10mr15237716wrr.372.1643051628223; Mon, 24 Jan 2022 11:13:48 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id g20sm216975wmq.9.2022.01.24.11.13.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:47 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:31 +0000 Subject: [PATCH v7 03/16] reftable: fix resource leak blocksource.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 Mon Jan 24 19:13:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722848 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 EF73BC433EF for ; Mon, 24 Jan 2022 19:32:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345887AbiAXTch (ORCPT ); Mon, 24 Jan 2022 14:32:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352137AbiAXT3n (ORCPT ); Mon, 24 Jan 2022 14:29:43 -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 8EB0BC029822 for ; Mon, 24 Jan 2022 11:13:50 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id r22so7845452wra.8 for ; Mon, 24 Jan 2022 11:13: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=zdzjp5OSTkr13zGSSSD1ncEu7unm1sp1sVMxlp9/SYY=; b=bZsxT6yMLEmvjlg02ixw/z555DzPxmKBcXWSxLmgnmvuyB/UFD1YG2XyfzOqTPkf9K zpdrmm23RGgHoTb6Qi/nA0BZt+yw6rx2RbHHZcfRyptbKqNhXMn3pV2BiX/IZf1Ja7QQ 24NTK3wwtpdgzbE/RgDS1lhyBgkgVDUyCDFl8BIy/TghVnll9WbHpc5NFOebV+KUiM2B +cHdMyWcYcMsM9NrLt2TMIR6wbQMdDeFmIJdeA6Sgh/y5jYTKQ+fwOvNF/A2k1xVAEpK YtXDd7mBT71Yq0kxkJgcB5AX8aDb7mOsur4EEqGz7l0KR89suAcN6gyI0ATdQBXCbFZW uOqw== 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=zdzjp5OSTkr13zGSSSD1ncEu7unm1sp1sVMxlp9/SYY=; b=rZ5ssmPUjcFXXAcBhkXFMaUPihPTliA+JIAiQwXvuUF0eGGRvIPIahr/lELB3HeCTA KquZhydz1yTAM/az6p9FeTw6Ww2nEkBA22LtEdWQpTCyEQy7B6540KrtT0KmKo0sDlZs 9PpqZ4p6XLDuNagHlXIO549RthF1EzAJKfzoMSD1+7pL0b8FlVI1xdeqUMqGqhbqvS6y kxLegQPoDrZ1HTW4rSiaZ0d41dlwoudxGloA42iwgDGWu0a4pBNtFAlvR1/A51m0raqU OJxEW4Z0CaTZPQD/7ogCFM/KnZxjifldjwvtHFqLTYUtIzhur0/lTAHUGubVRdacLOT5 JA5g== X-Gm-Message-State: AOAM532DcWYTooGkRLF601vIQoDmizaoJ1gADfMAsgKsWITU9pEys85t QIlLFq8za50QpFu6ZsoCRiBiPGXP25RoKg== X-Google-Smtp-Source: ABdhPJw3VCC07YOdNqzccAL36wcrI3PXE95ktB7arTDL0eRcRtgUmcwqRG7/ii0NfmCMSBM3jssOcg== X-Received: by 2002:adf:f68e:: with SMTP id v14mr16271949wrp.585.1643051629089; Mon, 24 Jan 2022 11:13:49 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c10sm14783702wrq.11.2022.01.24.11.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:48 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:32 +0000 Subject: [PATCH v7 04/16] 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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 compaction consistently errors out, we would accumulate reftables, and the follow-on EXPECT would start failing, but catching errors early helps debugging. Spotted 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 f4c743db80c..e84f50d27ff 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -839,6 +839,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 Mon Jan 24 19:13:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722863 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 20E78C433EF for ; Mon, 24 Jan 2022 19:38:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352965AbiAXTir (ORCPT ); Mon, 24 Jan 2022 14:38:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352220AbiAXT3z (ORCPT ); Mon, 24 Jan 2022 14:29:55 -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 6CA2BC02982B for ; Mon, 24 Jan 2022 11:13:51 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id e2so77660wra.2 for ; Mon, 24 Jan 2022 11:13: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=O7HpXaalYCxCk5dynhjgMwF4vaC9v1JOAuJFl5oFBLE=; b=mpzoFdQ6xRsx9KeiS5rx2Fu1usGT++GEGIBxlmKivtwFksSQkFH9Ig5//p/9dewHVU Ios1du7Ju0frrEdMqDtG2mdozmAndqIToh3/1RQxx+Cr3m3PQd/1LYGcT2tziVp+sg1m /IWcvQNzO5pu0pMmlrFREoGopN09BNhA7dJ8qCjAznLiipOFlhebgJftrOSiSkMw3x8t 52CPivbzyHPE7i5Ba4Z7p5/GanaXOsGNL9NatDzkJch6TdQMlYdt90duFpWgrP92TykY shpU40Hc6owemHUfriDuN70t3RCzxj8ix98KRVgvNPCzaVrHEmptumzFOcUOqZQWqLHF wyJg== 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=O7HpXaalYCxCk5dynhjgMwF4vaC9v1JOAuJFl5oFBLE=; b=0Tpjumiisv3pzHaFrHQdyHgoT8j+oYknYiglZSeBmSOxWV6E9pjQZ+iA1L2ZBIOx17 Avb9MNrQmOe7nGQqLaFLZWOtkYoMhIUGw2vOnhkFA70qx1vAPpO4ETZCCI1D60N/ZNs5 Y3T+DhRs2prjTg5F9MpquRVJ0oMr9YanJRbI9x9dheTtlztEsCY/4Qsc/O6XwHUgBTVD wv6PyRKQ8XFXsqnJv2cLsTqiasFGJGUeR+VlM0lgZRtTkFFAbCZrXW5/aQGMsZ+zTyKX oGVcODavIfiIet+rTfQDqLcvSbWfAgstEg2GwaL3bkc2cdrVNKbFDXBrFRxPW+iAuM8d UOxg== X-Gm-Message-State: AOAM532I8IxklF6XMtEKZeYwkXZFP6sludEU8fRHOINR0mSKRVVv2SH7 wds5UUjd58TkKuEFLPDyaC2GN9PvVjC7qg== X-Google-Smtp-Source: ABdhPJz78TUlHuELEpYyQegrfflik7h0t6X/9uurn/6zUYS5e9BUQJi8ffHzJoIyVv7m6BtnGvTpBA== X-Received: by 2002:a05:6000:2c6:: with SMTP id o6mr4580589wry.601.1643051629956; Mon, 24 Jan 2022 11:13:49 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m64sm152491wmm.31.2022.01.24.11.13.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:49 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:33 +0000 Subject: [PATCH v7 05/16] 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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 e84f50d27ff..f0be0be9fee 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -90,7 +90,7 @@ static void test_read_file(void) EXPECT(0 == strcmp(want[i], names[i])); } free_names(names); - remove(fn); + EXPECT(unlink(fn) == 0); } static void test_parse_names(void) From patchwork Mon Jan 24 19:13:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722864 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 D4737C433F5 for ; Mon, 24 Jan 2022 19:39:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348588AbiAXTi6 (ORCPT ); Mon, 24 Jan 2022 14:38:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352222AbiAXT3z (ORCPT ); Mon, 24 Jan 2022 14:29:55 -0500 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D203C02982C for ; Mon, 24 Jan 2022 11:13:52 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id j5-20020a05600c1c0500b0034d2e956aadso65177wms.4 for ; Mon, 24 Jan 2022 11:13: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=4f+I3S+5kXlrq2dgprxTUst4Q5HxOS1EHDxdA1BG70w=; b=Si/u+4TPSyEMzc/ap5YZiAeiObHTh99k3NL/Kg68RSu8QGtNJLZtIfuhvrgXSQtf8E HPXeH2YGoOnC4e7LEJmVQOvzBjNWkdg8emyk6qAPVDfYUqF/uqUaapF3FK3YHFgNz0j2 SxqPXVY6wVMWaS4bC2pTCSV6YPiymH3c/xD8/k4sIZWgMYf3fINrHKRRvaQei3f5AOvT bNqvFhQBmxe5tm/PE1ar7nXVlXQwJFfH7rvCQ0V8RwoH+MZjpB9Zdb2w5FseQQMCgx6L JYrSc2Y5dKP5wCFJGs5vbrzA9Iwn5sG0ZBWQd8/VC7/9J7Lv6SF+LACdQknVApF6fR+p qvmg== 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=4f+I3S+5kXlrq2dgprxTUst4Q5HxOS1EHDxdA1BG70w=; b=gIowHVUrKZeAIrqvs5NSFcEpwqkUZKYFlJaL5zvbI4Ad58u7019JxxpRBz6a/aro3c /w4GGocqEDbB5lJr3cCQ6W1HmgSTNZSuIlFSPLeaLH0nPc8na58bKdYm1fdVRr7Kh8XP xNAU0wP4J0kv+fR3nitI97xUme70mDm6UHmJ6+17hiEkky9o//teiX4oRWrKSuORwUg0 wU6Abe1tfweJivWC5MBzpJvd+Zu3bA+acLW1B8i6SsO7RoOFaPaEta6nurbQ4XdTyBUe GRKIrqAucw/LjryrHKZb73r7hKlTfABwU8vYJzzX51K1nCaFMgL6Bnw6RLjn3Q/LweMm 9TOg== X-Gm-Message-State: AOAM531Bb1xuMj7Jzg292RCPRuklmQAOhvhltB+EdZ29w2N+5GarlUxz GvcFubrtQcGKwM/vRx/BnaH5Kru2rq1rQQ== X-Google-Smtp-Source: ABdhPJwG17XQ+bpoWxHEFy8cHQ2VtbR+/GNvSXNCwmhnP7RnT/fj5rqsLUvXVnpNiTGx3GKHcC+zcw== X-Received: by 2002:a1c:c915:: with SMTP id f21mr3026425wmb.39.1643051630895; Mon, 24 Jan 2022 11:13:50 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y7sm8706527wrr.74.2022.01.24.11.13.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:50 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:34 +0000 Subject: [PATCH v7 06/16] reftable: fix resource warning Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 56bf5f2d84a..ddbdf1b9c8b 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -889,7 +889,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 = @@ -923,7 +923,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); @@ -1031,7 +1031,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); @@ -1068,9 +1068,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 Mon Jan 24 19:13:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722865 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 02DABC4332F for ; Mon, 24 Jan 2022 19:39:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353343AbiAXTjA (ORCPT ); Mon, 24 Jan 2022 14:39:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352460AbiAXTab (ORCPT ); Mon, 24 Jan 2022 14:30:31 -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 241ECC02982F for ; Mon, 24 Jan 2022 11:13:53 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id v13so15448942wrv.10 for ; Mon, 24 Jan 2022 11:13: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=BFxgNN28edqdOZ+Gb6YENsO0axQWrnuWxBbtDVaOk9s=; b=K7Ey1QshAj/m9etUkmYB4NnjIKRtkZq0o96gf5SXUQF6sDwsNRUj3YSaPzRi1F1rPP Z2Is8a8toeFUcqmnzdbPuFHDcKaTt6QQ6ZYxQFTi7GX0144Q1h08qWNhoBGfHKVa8mu/ g36dIBVbl6qkuRU8pS1vG3wPM/bvUotUhBKb1dXsNFXMxDu/IRz5rfaDmn0BhL2vkY8k YjjYn+mXgSxg1hZ9Dz1SNUUnURKFavQjA+anjKB8kNQWKZfo4wzeC7doo4Etg/n6HNtA wtDqAEfREs8J9113h27lsCMlKTkREBe92fLM+Pu9s1GP429hHAFBkydMCqhLkwsZqLlw JAxg== 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=BFxgNN28edqdOZ+Gb6YENsO0axQWrnuWxBbtDVaOk9s=; b=EqMdsIUsIRYgkYN1Smw0s5e+dfQoez2dU4TwZhDiwKEeNQpEJyk8EbFgN7bm8I62we QL+Gv7u/18EDlIYL25YfcjzbYyL+yvFFOdfatUtR82ShzV/FRL0GbOebLXdPVcQZTu3w E5gvfkRweo7Bafe6H4AuCESjkceSbaBEgLP/Lm7ri7jJ1fjRy0yqbIzydnlIc+awZO0+ Shi0pMJ7NEiYcQqLENKdMSCB/IB84/iXP5546JqlnCYeHi2D8CqxCTlIzrXcjs2FseQp 6+SMBGlO5VV9lsaWngvLD2fE1Zv2a94/jwiDb3gFdmj3qHwWA35lFB5cz9p3R6xSCLEu 9ldA== X-Gm-Message-State: AOAM530H1p3YNnkbTKCegcRaBGs6GEYnQ2+Tfjbz7QA+9xSZ0aL0BUMF BF7/3ZCtdKByg5HPyf2nh8qE2/IwyJP5vQ== X-Google-Smtp-Source: ABdhPJxWbbN+8cmlrk1892jJxmrEs9PMHFxtNuVqfyFIG6u5wl0JZ5ioOsNZwmmf5FLMbKEZkKONLA== X-Received: by 2002:a05:6000:144c:: with SMTP id v12mr15525636wrx.218.1643051631604; Mon, 24 Jan 2022 11:13:51 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id v5sm18710259wrx.114.2022.01.24.11.13.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:51 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:35 +0000 Subject: [PATCH v7 07/16] reftable: all xxx_free() functions accept NULL arguments Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 fixes NULL derefs in error paths. Spotted by Coverity. Signed-off-by: Han-Wen Nienhuys --- reftable/reader.c | 2 ++ reftable/writer.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/reftable/reader.c b/reftable/reader.c index 35781593a29..272378ed1d5 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -646,6 +646,8 @@ int reftable_new_reader(struct reftable_reader **p, void reftable_reader_free(struct reftable_reader *r) { + if (!r) + return; reader_close(r); reftable_free(r); } diff --git a/reftable/writer.c b/reftable/writer.c index 35c8649c9b7..83a23daf60b 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -150,6 +150,8 @@ void reftable_writer_set_limits(struct reftable_writer *w, uint64_t min, void reftable_writer_free(struct reftable_writer *w) { + if (!w) + return; reftable_free(w->block); reftable_free(w); } From patchwork Mon Jan 24 19:13:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722866 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 4AC11C433EF for ; Mon, 24 Jan 2022 19:39:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353915AbiAXTjF (ORCPT ); Mon, 24 Jan 2022 14:39:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352471AbiAXTac (ORCPT ); Mon, 24 Jan 2022 14:30:32 -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 7A07AC029831 for ; Mon, 24 Jan 2022 11:13:54 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id s18so15475113wrv.7 for ; Mon, 24 Jan 2022 11:13: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=pMYa0dfQa1Y/HGAo4moCXBwMLCmicYjMXhnW/sy5Md0=; b=flqUX0JJ/zsFXKm0Hr5VEKIRZKVVIZnkbA5mXVvB9USAUt0OfvF5tU0nktr7RwJdR2 ZExVZw/upwIPckTgwpSCZ4mlf1RVBxLs0r6QB+94Uy1oz2nOsb8zpo1I42lc17lcIh3b YyHxcElORhkln7O3LOmDMy4fSKoWsSPXwHhtkhw96ifeZvZfG/33CtHWdi7kpTllTynB 4RPmUgL6hBHLpOc6pYUAznbfaT51Nl2gWuL64GOtw0Rh6O/KJ8lQVB7VN2E0F6bDN1K3 toQ5OzciL/1OJx/yYgraSWoS/R4h51VPvfiemc+yqNycpKJUuK8nSQV6AcU7wao35FTA yLiw== 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=pMYa0dfQa1Y/HGAo4moCXBwMLCmicYjMXhnW/sy5Md0=; b=OIma378o4hJW8vrHrke14ndX3C5NSecKMKWta0rWXjZlfBX/Llcc1NI0qQ7gGy4j8n r2HMwbbyftECn33T0mv+POBgILvJ+FNKIxXkRECq3CNiMie6ndOLYAzmXh+FKvLtxfYB HoTVWKADjlKvi+GXuHeVdTuv8+xDqSEShnBAf8MFTQn22m38hfukX3p5eAo/da+Tlczn Gbl4E85ATJYy3X1qaliqLXxLGXpY37miDnZq1QF8T4MnH99lYhkmORwJ+kRkJG9pzLFP GsEImQ61dgsnqmPjoJPl2splX+5hlZrXhbiw7tBl8BUtlPVaZahI5LNnxCNl+1lIX5Ez 3W5w== X-Gm-Message-State: AOAM532BclSW85AbCyJ6eVnVJqOBqRHHFlYPBjNBqJ4BiejE+PTTjyzI TxLNxSHhkaBGo8lNMFTJjLuPX2vfjTQ3vQ== X-Google-Smtp-Source: ABdhPJwe3LRl8nX4gREMPIuq6IjBb2PK8dqy4kDB90g9/SDQzVnusv9IElZvRG0gSw5HwHeUm2JQeQ== X-Received: by 2002:a05:6000:1ac8:: with SMTP id i8mr904224wry.34.1643051632987; Mon, 24 Jan 2022 11:13:52 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f8sm14219925wry.46.2022.01.24.11.13.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:51 -0800 (PST) Message-Id: <7a1f32a2ff0926179066f09659f990d4a3f99566.1643051624.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:36 +0000 Subject: [PATCH v7 08/16] reftable: order unittests by complexity Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 practical 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..1f0a28cbb64 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 Mon Jan 24 19:13:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722851 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 720A9C433F5 for ; Mon, 24 Jan 2022 19:33:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348996AbiAXTdI (ORCPT ); Mon, 24 Jan 2022 14:33:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352680AbiAXTa4 (ORCPT ); Mon, 24 Jan 2022 14:30:56 -0500 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DE99C02983A for ; Mon, 24 Jan 2022 11:13:55 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id e8so13692889wrc.0 for ; Mon, 24 Jan 2022 11:13: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=s8KHLkLqupAFa85TGasfQB8YpksyOQa7dTuyqNPbJZM=; b=gl9LdwkeKA9rVlPXCSOyNw4ZfwE3nPJ+rr9DrGCy8eThQAH1DXUnZ/qI0Fln5vvqUU L2sM/YsF6I1kSCbPhBCWPTTsNzlAAWOdPNYftkP8DBK33W+UffqVWAHOse6qhTponqAs YEL6bnJdVfICUem0wKZwpHpltIO641ertP2fZ6eYhCDn0u1bAdAXYEBZvtLNa7dJZmL6 6iBuBuIzxoBQMxsRTZa/akHLncm+fnxCtrgVCDlRRHq95fmj+p5x087p0lNwKNupQmyf ipLa7GNXGELsI5xPaqSwbEIeWsP79tZQtq9IXCWBhFvBqqa152pjR62rCRLIocFVuTVe X2Qw== 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=s8KHLkLqupAFa85TGasfQB8YpksyOQa7dTuyqNPbJZM=; b=YaivSM0AaOhEQEUfew/XIK0ttnpVfyk92SH0N1crdLc+IJCVd5dN1iPNjeOA+YcQ38 UNVRxbXjlXjuG0d23PG1/8+V6XMhpaNS3zrV2vQFKfdlRljj9xWw79ZaQadrDUUe1MP7 WIuVIaASErme8x1odDhhdxrnN2nHOBF2wIg+gY5W0oe6O83hBYxLDrjA9H0CYJ2yE93k u9puoPLuHV8iwjmhiBkNPzXuXVW53VzPa2oOdgiCdBXo/5SFE/zZySAAGlgIlUuMOqp9 aFjB4NlGc8u78sonbKE+J6JgytRFVZV0BlYUIhkVx3uh43DR9ce+kTlKKoCI7qembGjX oCTA== X-Gm-Message-State: AOAM530Mvu8zXMUEvIJkBYAk8hvuoa/dCvTeGHSO81O7+VbWIAsI4GRw O1Cqnntv9xtO5pQ4oH1+iWOhk/m/tgxORg== X-Google-Smtp-Source: ABdhPJy/NULrMrpWtXSn0pFxNDLVVbRNyTROKZyFf+3373ftDb54KGdi/5nNMk0Yg/qR5UCfOdibvw== X-Received: by 2002:a05:6000:1705:: with SMTP id n5mr3059696wrc.283.1643051633766; Mon, 24 Jan 2022 11:13:53 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t1sm891562wre.45.2022.01.24.11.13.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:53 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:37 +0000 Subject: [PATCH v7 09/16] reftable: drop stray printf in readwrite_test Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 edb9cfc1930..605ba0f9fd4 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -696,7 +696,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 Mon Jan 24 19:13:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722867 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 56A95C433F5 for ; Mon, 24 Jan 2022 19:39:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346530AbiAXTjI (ORCPT ); Mon, 24 Jan 2022 14:39:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352685AbiAXTa4 (ORCPT ); Mon, 24 Jan 2022 14:30:56 -0500 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20B3DC02983B for ; Mon, 24 Jan 2022 11:13:56 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id f202-20020a1c1fd3000000b0034dd403f4fbso75262wmf.1 for ; Mon, 24 Jan 2022 11:13:56 -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=i2f7QHneSrgd8E5xUIeesO4h/9FI000eSeYDY3cV1H8=; b=LdpuExOgSfZ9sSVCoI1q/AnjiFFkk4YspfSMoz8ZTwKDIC/SXTo5XFvCvQfKd9wJaI lSRmD6cWmnpqWEYdY5EwKIzGaSar1ctAK8kVhxseg2YmWGBrvH+JVT8mInYpXK8AL4ez DGYVmC0bEaK907UBVj07UaKg4pVf+Yn+oc+gGOWrGF7/NeEVpbR+hnzJ5sebMTpHiKBS f8IcPwi0JkfmCOZrtpqsODQVTMxFLXeixVNvzcK5DDoF1VIxblAdLurLyDT1Dcw1wMyh BYBc2GpGxx6b5OTjAVzBd73TKQ5ot/4XOTF6DwiZOvPLeBdbVwt0srLr/lLhDczhfKJH pI7w== 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=i2f7QHneSrgd8E5xUIeesO4h/9FI000eSeYDY3cV1H8=; b=gWORh+swTghjpDlceUW/kqrE+povwFBxJUZ5DuxEPYhUJ8sAAMrGnUhSgRz6pycd7S HZhDbe9hjtDMd+hEazHPHjiomYsvgTY6Gmh7e8FuQtfwfvS/qSDHQZoqONNiqahOgiRP IugnFFQzTE9DSa2aosa5hHvLbB7J2QJMUHzxLRX7FGeGDeE7fVeTJuSAXJUjgVh/OPcG UL7ePbFvTbHNh08d4sI/xKsluPtFjRDimvxFH1tR4wvrJUVKl/F5ksZqEcWL3S87y4hK /HVir8UwNXeAVt4tj6xEYZRtWWnSqhUW1ZexIsqIntkFB9WZp/KGXVqJ1CC/bE6MKHWr Clvw== X-Gm-Message-State: AOAM531zPXWzGWxwxaKoP/LqN78ICWLfy8nB3S/p2+3kyLnFhysh66pv HXL03ObSVUyvnioCGXUHjAKoJpcdoS/KeQ== X-Google-Smtp-Source: ABdhPJxqGHG6ARDWQF9WUEdHLGG+WicaFzfzkrIH4PBtqatpmxYsVUjXDUidygX/1O7kZXN5RsH0zA== X-Received: by 2002:a7b:c187:: with SMTP id y7mr3034782wmi.93.1643051634542; Mon, 24 Jan 2022 11:13:54 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b2sm5088011wri.88.2022.01.24.11.13.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:54 -0800 (PST) Message-Id: <1d5cfbbc47a36e1f94d8432900da064a15c61b2d.1643051624.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:38 +0000 Subject: [PATCH v7 10/16] 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 , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 1f236b47d1c..53d83e4a41a 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 Mon Jan 24 19:13:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722868 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 25D18C433FE for ; Mon, 24 Jan 2022 19:39:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345761AbiAXTjO (ORCPT ); Mon, 24 Jan 2022 14:39:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352683AbiAXTa4 (ORCPT ); Mon, 24 Jan 2022 14:30:56 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F39DC02983C for ; Mon, 24 Jan 2022 11:13:57 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id i187-20020a1c3bc4000000b0034d2ed1be2aso59042wma.1 for ; Mon, 24 Jan 2022 11:13: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=7fOxL6/59cAuU6DFZOWe6UrF2Y5v3xI+JhobzCD2hfU=; b=kIuc2ZKWA+NL+vU3T7lhH9hdUTYG3bU1HQgBQSZBZI4Xuq/qpX0/NNmbQkIUrTCTQ/ nF4GQfceRpUibCJkItg8dr4AOo2GbwzoiqmM+NIjn08EV14jrLS2lL8r4zPK19XZBcqu 4lxcmeGLo6mLF/FEkLET862FQdBtg//fSfJ6t25oVQf4g/USngd1OKxRFVcO4AWIpSKS /hAG1aZb1td1Ktt89H0I0PO2m1NlpktPUB6qu7OQxfJhE5CrEVWa4VhCtAIIMwZoYebe fqkQIouqf1SWtJlJvDl+Mv8lRgvkY/qXImGSmiUkvnwj3TZe6EuORNm+6GsOwyAG3sCi mpLw== 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=7fOxL6/59cAuU6DFZOWe6UrF2Y5v3xI+JhobzCD2hfU=; b=tQqmWt1K/imyOfomGz/ync20QdHyk9Ds3LpLAVkECPiH3MkUWSSSOEjCnQX1vdvVJs /pudfVueSRusLfCOwOshn9/gTInrFdqdOIxOqIWZme+Y6O+PX13ye0+9o/gaWo4mToaf YPvDJ7ZE5qzCmslAvtNOqO4yrxZ+o6f5iZXUEMVwv9S0JUzfpxfRyg89JzBrmDmkwj9b nxKDlMB8I7srvqoNSL3X042cC4IFMvFPiN7kyejdcPTzAh8GiCKRlkSia9c4Fvbi41Zy yKqVPkHrAmLCkqvvGIA3i2/ESKFSNVov9hfR/dHrh01FQWZkqHzIjOabyFWLJDoGbAj0 Hvyg== X-Gm-Message-State: AOAM531HBiVx8LppWQqPgEgZKU4XwqN5Itfn9LhzyeYSF9Ry0icCF0Dv g9EN+A8HXpcbKrvZ6+AZu2St+KOdQWbNKw== X-Google-Smtp-Source: ABdhPJzlNUVznVIK8k0WSeEaSXHf3cmgE4nufwWz909lJQ4gs2y3Nz27avR3xrONkGwNDP5WYe15uA== X-Received: by 2002:a1c:1f04:: with SMTP id f4mr2940853wmf.161.1643051635524; Mon, 24 Jan 2022 11:13:55 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c10sm14783900wrq.11.2022.01.24.11.13.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:54 -0800 (PST) Message-Id: <90c72a2df0c40e5b4f1ad925a73cf0bed334348d.1643051624.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:39 +0000 Subject: [PATCH v7 11/16] reftable: make reftable-record.h function signatures const correct Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 | 14 +++++++------- reftable/reftable-record.h | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 53d83e4a41a..1ae344c745b 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -72,7 +72,7 @@ int reftable_is_block_type(uint8_t typ) return 0; } -uint8_t *reftable_ref_record_val1(struct reftable_ref_record *rec) +uint8_t *reftable_ref_record_val1(const struct reftable_ref_record *rec) { switch (rec->value_type) { case REFTABLE_REF_VAL1: @@ -84,7 +84,7 @@ uint8_t *reftable_ref_record_val1(struct reftable_ref_record *rec) } } -uint8_t *reftable_ref_record_val2(struct reftable_ref_record *rec) +uint8_t *reftable_ref_record_val2(const struct reftable_ref_record *rec) { switch (rec->value_type) { case REFTABLE_REF_VAL2: @@ -251,7 +251,7 @@ static void hex_format(char *dest, uint8_t *src, int hash_size) } } -void reftable_ref_record_print(struct reftable_ref_record *ref, +void reftable_ref_record_print(const struct reftable_ref_record *ref, uint32_t hash_id) { char hex[GIT_MAX_HEXSZ + 1]; @@ -881,8 +881,8 @@ static int zero_hash_eq(uint8_t *a, uint8_t *b, int sz) return !memcmp(a, b, sz); } -int reftable_log_record_equal(struct reftable_log_record *a, - struct reftable_log_record *b, int hash_size) +int reftable_log_record_equal(const struct reftable_log_record *a, + const struct reftable_log_record *b, int hash_size) { if (!(null_streq(a->refname, b->refname) && a->update_index == b->update_index && @@ -1150,8 +1150,8 @@ static int hash_equal(uint8_t *a, uint8_t *b, int hash_size) return a == b; } -int reftable_ref_record_equal(struct reftable_ref_record *a, - struct reftable_ref_record *b, int hash_size) +int reftable_ref_record_equal(const struct reftable_ref_record *a, + const struct reftable_ref_record *b, int hash_size) { assert(hash_size > 0); if (!null_streq(a->refname, b->refname)) diff --git a/reftable/reftable-record.h b/reftable/reftable-record.h index 5370d2288c7..67104f8fbfe 100644 --- a/reftable/reftable-record.h +++ b/reftable/reftable-record.h @@ -49,25 +49,25 @@ struct reftable_ref_record { /* Returns the first hash, or NULL if `rec` is not of type * REFTABLE_REF_VAL1 or REFTABLE_REF_VAL2. */ -uint8_t *reftable_ref_record_val1(struct reftable_ref_record *rec); +uint8_t *reftable_ref_record_val1(const struct reftable_ref_record *rec); /* Returns the second hash, or NULL if `rec` is not of type * REFTABLE_REF_VAL2. */ -uint8_t *reftable_ref_record_val2(struct reftable_ref_record *rec); +uint8_t *reftable_ref_record_val2(const struct reftable_ref_record *rec); /* returns whether 'ref' represents a deletion */ int reftable_ref_record_is_deletion(const struct reftable_ref_record *ref); /* prints a reftable_ref_record onto stdout. Useful for debugging. */ -void reftable_ref_record_print(struct reftable_ref_record *ref, +void reftable_ref_record_print(const struct reftable_ref_record *ref, uint32_t hash_id); /* frees and nulls all pointer values inside `ref`. */ void reftable_ref_record_release(struct reftable_ref_record *ref); /* returns whether two reftable_ref_records are the same. Useful for testing. */ -int reftable_ref_record_equal(struct reftable_ref_record *a, - struct reftable_ref_record *b, int hash_size); +int reftable_ref_record_equal(const struct reftable_ref_record *a, + const struct reftable_ref_record *b, int hash_size); /* reftable_log_record holds a reflog entry */ struct reftable_log_record { @@ -104,8 +104,8 @@ int reftable_log_record_is_deletion(const struct reftable_log_record *log); void reftable_log_record_release(struct reftable_log_record *log); /* returns whether two records are equal. Useful for testing. */ -int reftable_log_record_equal(struct reftable_log_record *a, - struct reftable_log_record *b, int hash_size); +int reftable_log_record_equal(const struct reftable_log_record *a, + const struct reftable_log_record *b, int hash_size); /* dumps a reftable_log_record on stdout, for debugging/testing. */ void reftable_log_record_print(struct reftable_log_record *log, From patchwork Mon Jan 24 19:13:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722849 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 54B73C433EF for ; Mon, 24 Jan 2022 19:33:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347470AbiAXTdC (ORCPT ); Mon, 24 Jan 2022 14:33:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352687AbiAXTa4 (ORCPT ); Mon, 24 Jan 2022 14:30:56 -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 30D4CC061A7E for ; Mon, 24 Jan 2022 11:13:58 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id a13so15476104wrh.9 for ; Mon, 24 Jan 2022 11:13: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=FbnsH1HGDpSv3hUkzpcagL9He39C7BCnALhMQYFFCrc=; b=KiNdi8n/RnpKZqmvMAVQ1opHBitsn2dXvvbDSNNhUnDCZH8XjdBOR8iawD4x4DqIz8 w3k5jnFyxXTdSxWHcDEnKGwe3GZJXpflQxNhEdJZuKyE9rVe6xvE5PoVFao1lyffSPwx 8gVLDB+uGupiKyaT9eX2mcYOOA704LEQ1Fxz2KcVePvYAv6LvGcjo/4xwo+suerjUnoi o8eEP4O6ux6WuJfp1aAikbMRDRKAxQN3mUsXjknf4f5BFTA/8IEy7+f49Xl9hpPzfuCx J2ZRGRl8Bh5H0BBHN/nq+/5BQjQ88lWMxjZhNNqNdk8Q02snRsraDIJ2sVALmM9M+gSi aL4A== 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=FbnsH1HGDpSv3hUkzpcagL9He39C7BCnALhMQYFFCrc=; b=UqJW2Qb8uwEhGyOhCsj01+9s2eMuOocIsvCN2wjgv2eAhA3oinA5e2u9VtFrCLUQYg Xq8/sq5f+Ym+3qk2Yr0+aWb9VXsjDOI7RMCzthDxXGLiYKm9Ji4vUdWygloPG+Qhorkx S1vnhmg820BDT7RtycoCM4MYblHoSi0eaqrRJKAkbYgokBv5c0u4YOHpxTq9bL/qM+Tu UqGeRxzb1WRbRvHEwiTubfyweZk2lmFqlYzNZYwmLxRWYFAIEIXp5576+cXZ5CIvbp21 U12KuudelAXo3xXP2opVKmR0bPjHDZTqVf1amFABWqmyCoKZ1USBxEHGkvleaBlXqK0X NVlg== X-Gm-Message-State: AOAM532gAYsI8AfM9SoHP34NSdA1zlLnZ5i8tAMPB+KxBxXvONUJ5D9F 9Xzb2pl4cYvq1hTZI/SD+Ql4zGslJYQ1vA== X-Google-Smtp-Source: ABdhPJzH73HBaEZYn7YzxEvjaToGK/nj9aiKWnQZ3DtKOC4P1X7cbfKWj1va5flStwWePR26doZknA== X-Received: by 2002:adf:fc0c:: with SMTP id i12mr6703795wrr.173.1643051636631; Mon, 24 Jan 2022 11:13:56 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u18sm192229wmq.24.2022.01.24.11.13.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:55 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:40 +0000 Subject: [PATCH v7 12/16] reftable: implement record equality generically Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 simplifies unittests a little, and provides further coverage for reftable_record_copy(). Signed-off-by: Han-Wen Nienhuys --- reftable/record.c | 54 +++++++++++++++++++++++++++++++++++++++++- reftable/record.h | 5 +++- reftable/record_test.c | 23 +++--------------- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 1ae344c745b..b543585ad39 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -430,6 +430,14 @@ static int reftable_ref_record_is_deletion_void(const void *p) (const struct reftable_ref_record *)p); } +static int reftable_ref_record_equal_void(const void *a, + const void *b, int hash_size) +{ + struct reftable_ref_record *ra = (struct reftable_ref_record *) a; + struct reftable_ref_record *rb = (struct reftable_ref_record *) b; + return reftable_ref_record_equal(ra, rb, hash_size); +} + static struct reftable_record_vtable reftable_ref_record_vtable = { .key = &reftable_ref_record_key, .type = BLOCK_TYPE_REF, @@ -439,6 +447,7 @@ static struct reftable_record_vtable reftable_ref_record_vtable = { .decode = &reftable_ref_record_decode, .release = &reftable_ref_record_release_void, .is_deletion = &reftable_ref_record_is_deletion_void, + .equal = &reftable_ref_record_equal_void, }; static void reftable_obj_record_key(const void *r, struct strbuf *dest) @@ -572,6 +581,23 @@ static int not_a_deletion(const void *p) return 0; } +static int reftable_obj_record_equal_void(const void *a, const void *b, int hash_size) +{ + struct reftable_obj_record *ra = (struct reftable_obj_record *) a; + struct reftable_obj_record *rb = (struct reftable_obj_record *) b; + + if (ra->hash_prefix_len != rb->hash_prefix_len + || ra->offset_len != rb->offset_len) + return 0; + + if (memcmp(ra->hash_prefix, rb->hash_prefix, ra->hash_prefix_len)) + return 0; + if (memcmp(ra->offsets, rb->offsets, ra->offset_len * sizeof(uint64_t))) + return 0; + + return 1; +} + static struct reftable_record_vtable reftable_obj_record_vtable = { .key = &reftable_obj_record_key, .type = BLOCK_TYPE_OBJ, @@ -580,7 +606,8 @@ static struct reftable_record_vtable reftable_obj_record_vtable = { .encode = &reftable_obj_record_encode, .decode = &reftable_obj_record_decode, .release = &reftable_obj_record_release, - .is_deletion = not_a_deletion, + .is_deletion = ¬_a_deletion, + .equal = &reftable_obj_record_equal_void, }; void reftable_log_record_print(struct reftable_log_record *log, @@ -881,6 +908,14 @@ static int zero_hash_eq(uint8_t *a, uint8_t *b, int sz) return !memcmp(a, b, sz); } +static int reftable_log_record_equal_void(const void *a, + const void *b, int hash_size) +{ + return reftable_log_record_equal((struct reftable_log_record *) a, + (struct reftable_log_record *) b, + hash_size); +} + int reftable_log_record_equal(const struct reftable_log_record *a, const struct reftable_log_record *b, int hash_size) { @@ -924,6 +959,7 @@ static struct reftable_record_vtable reftable_log_record_vtable = { .decode = &reftable_log_record_decode, .release = &reftable_log_record_release_void, .is_deletion = &reftable_log_record_is_deletion_void, + .equal = &reftable_log_record_equal_void }; struct reftable_record reftable_new_record(uint8_t typ) @@ -1042,6 +1078,14 @@ static int reftable_index_record_decode(void *rec, struct strbuf key, return start.len - in.len; } +static int reftable_index_record_equal(const void *a, const void *b, int hash_size) +{ + struct reftable_index_record *ia = (struct reftable_index_record *) a; + struct reftable_index_record *ib = (struct reftable_index_record *) b; + + return ia->offset == ib->offset && !strbuf_cmp(&ia->last_key, &ib->last_key); +} + static struct reftable_record_vtable reftable_index_record_vtable = { .key = &reftable_index_record_key, .type = BLOCK_TYPE_INDEX, @@ -1051,6 +1095,7 @@ static struct reftable_record_vtable reftable_index_record_vtable = { .decode = &reftable_index_record_decode, .release = &reftable_index_record_release, .is_deletion = ¬_a_deletion, + .equal = &reftable_index_record_equal, }; void reftable_record_key(struct reftable_record *rec, struct strbuf *dest) @@ -1098,6 +1143,13 @@ int reftable_record_is_deletion(struct reftable_record *rec) return rec->ops->is_deletion(rec->data); } +int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size) +{ + if (a->ops != b->ops) + return 0; + return a->ops->equal(a->data, b->data, hash_size); +} + void reftable_record_from_ref(struct reftable_record *rec, struct reftable_ref_record *ref_rec) { diff --git a/reftable/record.h b/reftable/record.h index 498e8c50bf4..da75d7d1f11 100644 --- a/reftable/record.h +++ b/reftable/record.h @@ -58,6 +58,9 @@ struct reftable_record_vtable { /* is this a tombstone? */ int (*is_deletion)(const void *rec); + + /* Are two records equal? This assumes they have the same type. Returns 0 for non-equal. */ + int (*equal)(const void *a, const void *b, int hash_size); }; /* record is a generic wrapper for different types of records. */ @@ -98,7 +101,7 @@ struct reftable_obj_record { }; /* see struct record_vtable */ - +int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size); 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, diff --git a/reftable/record_test.c b/reftable/record_test.c index f4ad7cace41..92680848156 100644 --- a/reftable/record_test.c +++ b/reftable/record_test.c @@ -21,18 +21,7 @@ static void test_copy(struct reftable_record *rec) 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(©)) { - case BLOCK_TYPE_REF: - EXPECT(reftable_ref_record_equal(reftable_record_as_ref(©), - reftable_record_as_ref(rec), - GIT_SHA1_RAWSZ)); - break; - case BLOCK_TYPE_LOG: - EXPECT(reftable_log_record_equal(reftable_record_as_log(©), - reftable_record_as_log(rec), - GIT_SHA1_RAWSZ)); - break; - } + EXPECT(reftable_record_equal(rec, ©, GIT_SHA1_RAWSZ)); reftable_record_destroy(©); } @@ -346,13 +335,7 @@ static void test_reftable_obj_record_roundtrip(void) 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(reftable_record_equal(&rec, &rec_out, GIT_SHA1_RAWSZ)); strbuf_release(&key); reftable_record_release(&rec_out); } @@ -390,7 +373,7 @@ static void test_reftable_index_record_roundtrip(void) m = reftable_record_decode(&out_rec, key, extra, dest, GIT_SHA1_RAWSZ); EXPECT(m == n); - EXPECT(in.offset == out.offset); + EXPECT(reftable_record_equal(&rec, &out_rec, GIT_SHA1_RAWSZ)); reftable_record_release(&out_rec); strbuf_release(&key); From patchwork Mon Jan 24 19:13:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722850 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 6AC4BC433F5 for ; Mon, 24 Jan 2022 19:33:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348954AbiAXTdE (ORCPT ); Mon, 24 Jan 2022 14:33:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352690AbiAXTa5 (ORCPT ); Mon, 24 Jan 2022 14:30:57 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17906C02983F for ; Mon, 24 Jan 2022 11:13:59 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id n8so23598402wmk.3 for ; Mon, 24 Jan 2022 11:13:59 -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=TUCZrT0R8cpMEB18cBkDpHwmjdJOKsrx7vpHWxOMd7M=; b=Bah38H0CUreC1+kek28c1yeXF8E/18Qcjc4+6w3qUYDA4NGkjEh035a1fB9lTxf48b Xt23bSGOJ/q9G519omBYe7TWgap7Rd9jWPUgqBUlEX4iOUD/5c12jpgCXITQ1KQo79MH zMOkBoBwV+z91OGuBQDp8Lx0zHXkpk6B3hKQaM7bRGyefM/Dd+AKHS2zQpmSpZFmBeYA G3MFyAwQc8IBJHjqAjyRyA45FtlTxgpM+2fJCR1VBk3jZglABiOiw2qtDahlGTHvT8rQ vky2+cg6K9LfY3qFUkz/gSDwwK2SGDUHb8L3OT+LAjpcqXhs7kAkUT6WCJ2IixXoNMvf OZSw== 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=TUCZrT0R8cpMEB18cBkDpHwmjdJOKsrx7vpHWxOMd7M=; b=P1zOWJ2ybto4RHxgbBiDDIYag6twYrNNiMICfVCipmwTKFo/DQqkkjibkX06bFs9CE Jmy0/VOwxUCrHZWQhPbZjsSmvRzdWu0SrNN9F3aho6UwiFv706ELMxZzvrKcuUUZuSC3 9olNuSMCFG/4Cdm0AugOIjKAjtM8yW2tkxQwCXN9jfSkNZTyqvw1Aaltph0SUtkzewVQ wCjq8VQQ2jzt3b2PK4i3i9+wZWrjV74C/EzC0TBzFz+Vy/cqKImAyxpXTFwGwS7I3001 IqdB+iJEfLjmY+SJD4Wp9wPp726sy1tEGV+/vQLYzSvIowcJhyIP5HP2Ga8bdsRVa4sL xPZQ== X-Gm-Message-State: AOAM531tEJXEnXxpUQb3HPhEA8OahkVuAPEGeFVIVe7Gk6Hx5DfOuR4j VXiLjX6uH1ekZeYEOYUN0Qj2txHEOXOjBw== X-Google-Smtp-Source: ABdhPJyeNokdloFAdvP09PmlJ2lDDj91FLPbN6FUl6mZMyP8+KcNEi5LOGUz7O+h+UfNqzg6KGtqkA== X-Received: by 2002:a05:600c:4ed3:: with SMTP id g19mr3054065wmq.63.1643051637503; Mon, 24 Jan 2022 11:13:57 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id 1sm146911wmo.37.2022.01.24.11.13.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:57 -0800 (PST) Message-Id: <7e23d14d1af8624067c0afd8f21e793ee551a78f.1643051624.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:41 +0000 Subject: [PATCH v7 13/16] reftable: remove outdated file reftable.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 was renamed to generic.c, but the origin was never removed Signed-off-by: Han-Wen Nienhuys --- reftable/reftable.c | 115 -------------------------------------------- 1 file changed, 115 deletions(-) delete mode 100644 reftable/reftable.c diff --git a/reftable/reftable.c b/reftable/reftable.c deleted file mode 100644 index 0e4607a7cd6..00000000000 --- a/reftable/reftable.c +++ /dev/null @@ -1,115 +0,0 @@ -/* -Copyright 2020 Google LLC - -Use of this source code is governed by a BSD-style -license that can be found in the LICENSE file or at -https://developers.google.com/open-source/licenses/bsd -*/ - -#include "basics.h" -#include "record.h" -#include "generic.h" -#include "reftable-iterator.h" -#include "reftable-generic.h" - -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); - return tab->ops->seek_record(tab->table_arg, it, &rec); -} - -int reftable_table_read_ref(struct reftable_table *tab, const char *name, - struct reftable_ref_record *ref) -{ - struct reftable_iterator it = { NULL }; - int err = reftable_table_seek_ref(tab, &it, name); - if (err) - goto done; - - err = reftable_iterator_next_ref(&it, ref); - if (err) - goto done; - - if (strcmp(ref->refname, name) || - reftable_ref_record_is_deletion(ref)) { - reftable_ref_record_release(ref); - err = 1; - goto done; - } - -done: - reftable_iterator_destroy(&it); - return err; -} - -uint64_t reftable_table_max_update_index(struct reftable_table *tab) -{ - return tab->ops->max_update_index(tab->table_arg); -} - -uint64_t reftable_table_min_update_index(struct reftable_table *tab) -{ - return tab->ops->min_update_index(tab->table_arg); -} - -uint32_t reftable_table_hash_id(struct reftable_table *tab) -{ - return tab->ops->hash_id(tab->table_arg); -} - -void reftable_iterator_destroy(struct reftable_iterator *it) -{ - if (!it->ops) { - return; - } - it->ops->close(it->iter_arg); - it->ops = NULL; - FREE_AND_NULL(it->iter_arg); -} - -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); -} - -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); -} - -int iterator_next(struct reftable_iterator *it, struct reftable_record *rec) -{ - return it->ops->next(it->iter_arg, rec); -} - -static int empty_iterator_next(void *arg, struct reftable_record *rec) -{ - return 1; -} - -static void empty_iterator_close(void *arg) -{ -} - -static struct reftable_iterator_vtable empty_vtable = { - .next = &empty_iterator_next, - .close = &empty_iterator_close, -}; - -void iterator_set_empty(struct reftable_iterator *it) -{ - assert(!it->ops); - it->iter_arg = NULL; - it->ops = &empty_vtable; -} From patchwork Mon Jan 24 19:13:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722870 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 339DDC4332F for ; Mon, 24 Jan 2022 19:40:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348577AbiAXTjV (ORCPT ); Mon, 24 Jan 2022 14:39:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352701AbiAXTa5 (ORCPT ); Mon, 24 Jan 2022 14:30:57 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93368C028C16 for ; Mon, 24 Jan 2022 11:14:00 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id a203-20020a1c98d4000000b0034d2956eb04so62856wme.5 for ; Mon, 24 Jan 2022 11:14: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=LSlnqzr1ZYzdBnbAS7yfckCZJ4mp6nmarof+7B5s4BE=; b=N/03hvTNX5o0ESMratW0cjxO91kF+qEdTBGzRD93pelqkD0pHbyD9g+Shk94ZuNCyh rjaMIR5W7SsQEEhAb96PnSb7jvBvUo+PDXGvNg2CVl+gmAmc5pMNAhxXHsKC5eqHSNWv V8pJgGYSUfbAnPA+MXE+Pf4z4k6b+owrjYfUXT3sfHs+7d/nSMPRZqGg6nprtkWqtBrw sRzXonJ/J834r6+stwUEyskN5UiZKRCJvodrsbYuCtbRFaBJcZhVyD69bLUrwiJiucmd Jjbhket4cPNwuomnhcBpLgIJAmxaFA5bUbRXw2n198lMRkEPzw0vyFalCHrzNVy01hJX 0woA== 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=LSlnqzr1ZYzdBnbAS7yfckCZJ4mp6nmarof+7B5s4BE=; b=yLD/9ghOoOFAKTOaHqskku0nZs6+4ffPq/+MqaHMikN74wuNuEEqRHa2+eah1j5P7i bTKWksY1s/WVWUaTABDygBoCaKTyZO+P4K6tfphvuws8Lth9ucYpLEy866Ck1hltkbLm BuoaydvO8EP4J3VZGIU50sVCUT0IZubZtLLjfuxmTMkkbXlQs/bJuuiZDEcZu3bTVgkw ifW9Pqg5zhg2P8lwTfpjQUD6dSxjDcbx6QJZRrXhalgSqqpFXaV7/uofF8FrhR8ve3F5 NchHa7eoW8Yil3pV2m/0jVXHYfiREmECAcTVZq7Z4q+eQF1Lf5RVvjZN3R2QVRkL+f+G Dbog== X-Gm-Message-State: AOAM531jBy6q22fPsL2SWnYQCy2Un/A/5Cr7D6qr9ulu71Ei+YdpjCvE uYOmYGw8+uNbjAzoEynrEUtVZDuE8hZf4A== X-Google-Smtp-Source: ABdhPJy0IA82ZH1984k1+6nYCnJ0e8SJ6DmE3qUfz+CsJBUZp59TiBwSESXJtvOCvVk0ypxUhnaG7Q== X-Received: by 2002:a7b:c14c:: with SMTP id z12mr3126168wmi.84.1643051638531; Mon, 24 Jan 2022 11:13:58 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id i6sm178428wma.22.2022.01.24.11.13.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:57 -0800 (PST) Message-Id: <047bbb75fcbb67c41d307bf5fa44587a76cc0777.1643051624.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:42 +0000 Subject: [PATCH v7 14/16] reftable: make reftable_record a tagged union Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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, use reftable_new_record() to return fresh instance, given a record type. Since reftable_new_record() doesn't cause heap allocation anymore, it should be balanced with reftable_record_release() rather than reftable_record_destroy(). Thanks to Peff for the suggestion. Helped-by: Jeff King Signed-off-by: Han-Wen Nienhuys --- reftable/block.c | 2 +- reftable/block_test.c | 22 ++--- reftable/generic.c | 39 +++++--- reftable/iter.c | 4 +- reftable/merged.c | 31 +++--- reftable/pq.c | 2 +- reftable/pq_test.c | 29 +++--- reftable/reader.c | 84 +++++++++-------- reftable/record.c | 209 +++++++++++++++++++---------------------- reftable/record.h | 42 ++++----- reftable/record_test.c | 154 +++++++++++++++--------------- reftable/writer.c | 38 ++++---- 12 files changed, 328 insertions(+), 328 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 5832f85501d..80ad10bb794 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -422,7 +422,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..b176c1cde0a 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,23 @@ 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 = { + .type = BLOCK_TYPE_REF, + .u.ref = { .refname = (char *)name, } }; - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, &ref); 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 = { + .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = (char *)name, + .update_index = ~((uint64_t)0), + } }; - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, &log); return tab->ops->seek_record(tab->table_arg, it, &rec); } @@ -129,17 +130,25 @@ 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, + .u.ref = *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, + .u.log = *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..82511762d2a 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -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_new_record(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; } @@ -290,11 +289,10 @@ 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 +300,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..5ffc8380c4a 100644 --- a/reftable/pq.c +++ b/reftable/pq.c @@ -98,7 +98,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..5d58a4c05b6 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,12 @@ 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 +55,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(reftable_record_type(rec) == 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 272378ed1d5..565cda8d5a2 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -239,8 +239,7 @@ static int table_iter_next_in_block(struct table_iter *ti, { 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; + rec->u.ref.update_index += ti->r->min_update_index; } return res; @@ -480,7 +479,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; @@ -490,34 +489,36 @@ 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); + reftable_record_key(rec, &want_index.u.idx.last_key); err = reader_start(r, &index_iter, reftable_record_type(rec), 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; @@ -545,8 +546,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; } @@ -595,11 +596,10 @@ 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); } @@ -607,12 +607,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); } @@ -656,31 +657,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; @@ -688,15 +691,16 @@ 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 b543585ad39..238ff882aab 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -15,6 +15,24 @@ 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) +{ + 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(); +} + int get_var_int(uint64_t *dest, struct string_view *in) { int ptr = 0; @@ -474,12 +492,13 @@ static void reftable_obj_record_copy_from(void *rec, const void *src_rec, (const struct reftable_obj_record *)src_rec; reftable_obj_record_release(obj); - *obj = *src; - obj->hash_prefix = reftable_malloc(obj->hash_prefix_len); + obj->hash_prefix = reftable_malloc(src->hash_prefix_len); + obj->hash_prefix_len = src->hash_prefix_len; memcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len); - obj->offsets = reftable_malloc(obj->offset_len * sizeof(uint64_t)); - COPY_ARRAY(obj->offsets, src->offsets, obj->offset_len); + obj->offsets = reftable_malloc(src->offset_len * sizeof(uint64_t)); + obj->offset_len = src->offset_len; + COPY_ARRAY(obj->offsets, src->offsets, src->offset_len); } static uint8_t reftable_obj_record_val_type(const void *rec) @@ -962,58 +981,6 @@ static struct reftable_record_vtable reftable_log_record_vtable = { .equal = &reftable_log_record_equal_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; @@ -1036,6 +1003,7 @@ static void reftable_index_record_release(void *rec) { struct reftable_index_record *idx = rec; strbuf_release(&idx->last_key); + idx->offset = 0; } static uint8_t reftable_index_record_val_type(const void *rec) @@ -1100,98 +1068,60 @@ 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); + reftable_record_vtable(rec)->key(reftable_record_data(rec), dest); } uint8_t reftable_record_type(struct reftable_record *rec) { - return rec->ops->type; + return rec->type; } 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); + return reftable_record_vtable(rec)->is_deletion( + reftable_record_data(rec)); } int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size) { - if (a->ops != b->ops) + if (a->type != b->type) return 0; - return a->ops->equal(a->data, b->data, hash_size); -} - -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(a)->equal( + reftable_record_data(a), reftable_record_data(b), hash_size); } static int hash_equal(uint8_t *a, uint8_t *b, int hash_size) @@ -1264,3 +1194,60 @@ void string_view_consume(struct string_view *s, int n) s->buf += n; s->len -= n; } + +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_new_record(uint8_t type) +{ + struct reftable_record clean = { + .type = type, + }; + + /* the following is involved, but the naive solution (just return + * `clean` as is, except for BLOCK_TYPE_INDEX), returns a garbage + * clean.u.obj.offsets pointer on Windows VS CI. Go figure. + */ + switch (type) { + case BLOCK_TYPE_OBJ: + { + struct reftable_obj_record obj = { 0 }; + clean.u.obj = obj; + break; + } + case BLOCK_TYPE_INDEX: + { + struct reftable_index_record idx = { + .last_key = STRBUF_INIT, + }; + clean.u.idx = idx; + break; + } + case BLOCK_TYPE_REF: + { + struct reftable_ref_record ref = { 0 }; + clean.u.ref = ref; + break; + } + case BLOCK_TYPE_LOG: + { + struct reftable_log_record log = { 0 }; + clean.u.log = log; + break; + } + } + return clean; +} diff --git a/reftable/record.h b/reftable/record.h index da75d7d1f11..010a322e901 100644 --- a/reftable/record.h +++ b/reftable/record.h @@ -63,16 +63,10 @@ struct reftable_record_vtable { int (*equal)(const void *a, const void *b, int hash_size); }; -/* 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 */ +/* return an initialized record for the given type */ struct reftable_record reftable_new_record(uint8_t typ); /* Encode `key` into `dest`. Sets `is_restart` to indicate a restart. Returns @@ -100,6 +94,22 @@ 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_new_record() 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 */ int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size); void reftable_record_key(struct reftable_record *rec, struct strbuf *dest); @@ -114,25 +124,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 92680848156..4a025c48104 100644 --- a/reftable/record_test.c +++ b/reftable/record_test.c @@ -16,13 +16,14 @@ static void test_copy(struct reftable_record *rec) { - struct reftable_record copy = - reftable_new_record(reftable_record_type(rec)); + struct reftable_record copy = { 0 }; + uint8_t type = reftable_record_type(rec); + copy = reftable_new_record(type); reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ); /* do it twice to catch memory leaks */ reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ); EXPECT(reftable_record_equal(rec, ©, GIT_SHA1_RAWSZ)); - reftable_record_destroy(©); + reftable_record_release(©); } static void test_varint_roundtrip(void) @@ -95,11 +96,11 @@ 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, @@ -108,48 +109,46 @@ static void test_reftable_ref_record_roundtrip(void) 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); } } @@ -202,7 +201,7 @@ 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 = { @@ -210,23 +209,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); @@ -234,16 +235,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); } } @@ -311,41 +312,43 @@ 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(reftable_record_equal(&rec, &rec_out, GIT_SHA1_RAWSZ)); + EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ)); 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 = { @@ -353,31 +356,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(reftable_record_equal(&rec, &out_rec, GIT_SHA1_RAWSZ)); + EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ)); - 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 83a23daf60b..96a5155ea77 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -256,8 +256,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) @@ -266,8 +268,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) @@ -306,7 +307,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); @@ -317,7 +321,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); } @@ -398,8 +401,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; } @@ -467,17 +472,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; @@ -490,7 +496,7 @@ 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. */ From patchwork Mon Jan 24 19:13:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722869 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 74364C433FE for ; Mon, 24 Jan 2022 19:39:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349720AbiAXTjS (ORCPT ); Mon, 24 Jan 2022 14:39:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352702AbiAXTa5 (ORCPT ); Mon, 24 Jan 2022 14:30:57 -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 C2904C028C17 for ; Mon, 24 Jan 2022 11:14:00 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id f202-20020a1c1fd3000000b0034dd403f4fbso75443wmf.1 for ; Mon, 24 Jan 2022 11:14: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=Eg9gLPLi4OvtGlPfHPfe6Z4nZSoiM4bTniLm404QQb8=; b=oVkuN6XBQ+/J8RT6GBgcsGk6BZ9vn16lnxgMhI1lLAZmp2KawOws3gDlE7TKbyEYF8 GPLrJMEWlz6xqWnaX8FUOlXg2DHoo+/MODKj5xc6IFYYLqGIrFeDDmZ+xNJmvLYkq6sZ m8WhsIDhPfuqnGx934Oc8i8vSXE2Foyzy6LozpBdiuS6iA8qyd9hZPtyYwsDPhG1ORQI MK0Y2Qefp/Tw+b2Clf+o3F8leWbMnYw+Y0ggZWloRkrZrsIA/MH8Oic48eGYs+YMfL2G 7Dhu9yofK1MYRPRvXRbMTi8b9iCWBo95/c7kLJSqqKPBtXtay/VOgSGIzQaaTYWMnTkh jPZQ== 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=Eg9gLPLi4OvtGlPfHPfe6Z4nZSoiM4bTniLm404QQb8=; b=gdY5oXk3yu82BmbLpozrf42bxjspF198zZ0/kraYiGgYV7uVvSEVViwLPL8vXRPqfA c5UrQ+q4VQhKnOLh9uU62mQkqoi7zgkDoYVCogMBYk1bicgDmZ9igfbmw8nwc1zX6f05 +3szQk9hBLUmfdslmkAQjhdVtamzoQuY9An9GhIsuodWbC2xlUtl/8gSrEgQbRUgAQ62 JM4sDLslyUOZqj/WbFqFWBBElgGdXxrC5/J49FX29P1RX5t0R2DO5p5OCXGwSDRXdptb HxjBbpJ6O3mzUQHFXozfdJ+3APwjY54lrqDg3ZvCufc2qAUZ/pMFcgOJ8cb8gjyRDJQn nupg== X-Gm-Message-State: AOAM530ZQ9UvMPCqE2tKoSgKO3dJSGZvBtse5Y/kam8dkbhjWE6qjT0L Ht4AWtyodWgaMAhyXSmJPpLxSL9bx/5LWA== X-Google-Smtp-Source: ABdhPJy4Vd9p5hP4PMn7pALyimNtQGeh71+TS/RLt3MOQ/FT94sxrus2aonjYbhA2Hf3V0LpW717Dw== X-Received: by 2002:a05:600c:4101:: with SMTP id j1mr2986766wmi.28.1643051639193; Mon, 24 Jan 2022 11:13:59 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o27sm210536wms.4.2022.01.24.11.13.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:58 -0800 (PST) Message-Id: <1e8785bbc2620ce7550f98d5ed6b3761f17c622e.1643051624.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:43 +0000 Subject: [PATCH v7 15/16] reftable: add print functions to the record types Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 isn't used per se, but it is useful for debugging, especially Windows CI failures. Signed-off-by: Han-Wen Nienhuys --- reftable/record.c | 88 +++++++++++++++++++++++++++++++++++------- reftable/record.h | 4 ++ reftable/record_test.c | 20 +++++++++- 3 files changed, 96 insertions(+), 16 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 238ff882aab..c57bac0e96a 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -269,8 +269,8 @@ static void hex_format(char *dest, uint8_t *src, int hash_size) } } -void reftable_ref_record_print(const struct reftable_ref_record *ref, - uint32_t hash_id) +static void reftable_ref_record_print_sz(const struct reftable_ref_record *ref, + int hash_size) { char hex[GIT_MAX_HEXSZ + 1]; printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index); @@ -279,14 +279,14 @@ void reftable_ref_record_print(const struct reftable_ref_record *ref, printf("=> %s", ref->value.symref); break; case REFTABLE_REF_VAL2: - hex_format(hex, ref->value.val2.value, hash_size(hash_id)); + hex_format(hex, ref->value.val2.value, hash_size); printf("val 2 %s", hex); hex_format(hex, ref->value.val2.target_value, - hash_size(hash_id)); + hash_size); printf("(T %s)", hex); break; case REFTABLE_REF_VAL1: - hex_format(hex, ref->value.val1, hash_size(hash_id)); + hex_format(hex, ref->value.val1, hash_size); printf("val 1 %s", hex); break; case REFTABLE_REF_DELETION: @@ -296,6 +296,11 @@ void reftable_ref_record_print(const struct reftable_ref_record *ref, printf("}\n"); } +void reftable_ref_record_print(const struct reftable_ref_record *ref, + uint32_t hash_id) { + reftable_ref_record_print_sz(ref, hash_size(hash_id)); +} + static void reftable_ref_record_release_void(void *rec) { reftable_ref_record_release(rec); @@ -456,6 +461,13 @@ static int reftable_ref_record_equal_void(const void *a, return reftable_ref_record_equal(ra, rb, hash_size); } +static void reftable_ref_record_print_void(const void *rec, + int hash_size) +{ + reftable_ref_record_print_sz( + (struct reftable_ref_record *) rec, hash_size); +} + static struct reftable_record_vtable reftable_ref_record_vtable = { .key = &reftable_ref_record_key, .type = BLOCK_TYPE_REF, @@ -466,6 +478,7 @@ static struct reftable_record_vtable reftable_ref_record_vtable = { .release = &reftable_ref_record_release_void, .is_deletion = &reftable_ref_record_is_deletion_void, .equal = &reftable_ref_record_equal_void, + .print = &reftable_ref_record_print_void, }; static void reftable_obj_record_key(const void *r, struct strbuf *dest) @@ -484,6 +497,21 @@ static void reftable_obj_record_release(void *rec) memset(obj, 0, sizeof(struct reftable_obj_record)); } +static void reftable_obj_record_print(const void *rec, int hash_size) +{ + const struct reftable_obj_record *obj = rec; + char hex[GIT_MAX_HEXSZ + 1] = { 0 }; + struct strbuf offset_str = STRBUF_INIT; + int i; + + for (i = 0; i < obj->offset_len; i++) + strbuf_addf(&offset_str, "%"PRIu64 " ", obj->offsets[i]); + hex_format(hex, obj->hash_prefix, obj->hash_prefix_len); + printf("prefix %s (len %d), offsets [%s]\n", + hex, obj->hash_prefix_len, offset_str.buf); + strbuf_release(&offset_str); +} + static void reftable_obj_record_copy_from(void *rec, const void *src_rec, int hash_size) { @@ -627,31 +655,41 @@ static struct reftable_record_vtable reftable_obj_record_vtable = { .release = &reftable_obj_record_release, .is_deletion = ¬_a_deletion, .equal = &reftable_obj_record_equal_void, + .print = &reftable_obj_record_print, }; -void reftable_log_record_print(struct reftable_log_record *log, - uint32_t hash_id) +static void reftable_log_record_print_sz(struct reftable_log_record *log, + int hash_size) { char hex[GIT_MAX_HEXSZ + 1] = { 0 }; switch (log->value_type) { case REFTABLE_LOG_DELETION: - printf("log{%s(%" PRIu64 ") delete", log->refname, + printf("log{%s(%"PRIu64 ") delete\n", log->refname, log->update_index); break; case REFTABLE_LOG_UPDATE: - printf("log{%s(%" PRIu64 ") %s <%s> %" PRIu64 " %04d\n", - log->refname, log->update_index, log->value.update.name, - log->value.update.email, log->value.update.time, + printf("log{%s(%"PRIu64 ") %s <%s> %" PRIu64 " %04d\n", + log->refname, log->update_index, + log->value.update.name ? log->value.update.name : "", + log->value.update.email ? log->value.update.email : "", + log->value.update.time, log->value.update.tz_offset); - hex_format(hex, log->value.update.old_hash, hash_size(hash_id)); + hex_format(hex, log->value.update.old_hash, hash_size); printf("%s => ", hex); - hex_format(hex, log->value.update.new_hash, hash_size(hash_id)); - printf("%s\n\n%s\n}\n", hex, log->value.update.message); + hex_format(hex, log->value.update.new_hash, hash_size); + printf("%s\n\n%s\n}\n", hex, + log->value.update.message ? log->value.update.message : ""); break; } } +void reftable_log_record_print(struct reftable_log_record *log, + uint32_t hash_id) +{ + reftable_log_record_print_sz(log, hash_size(hash_id)); +} + static void reftable_log_record_key(const void *r, struct strbuf *dest) { const struct reftable_log_record *rec = @@ -969,6 +1007,11 @@ static int reftable_log_record_is_deletion_void(const void *p) (const struct reftable_log_record *)p); } +static void reftable_log_record_print_void(const void *rec, int hash_size) +{ + reftable_log_record_print_sz((struct reftable_log_record*)rec, hash_size); +} + static struct reftable_record_vtable reftable_log_record_vtable = { .key = &reftable_log_record_key, .type = BLOCK_TYPE_LOG, @@ -978,7 +1021,8 @@ static struct reftable_record_vtable reftable_log_record_vtable = { .decode = &reftable_log_record_decode, .release = &reftable_log_record_release_void, .is_deletion = &reftable_log_record_is_deletion_void, - .equal = &reftable_log_record_equal_void + .equal = &reftable_log_record_equal_void, + .print = &reftable_log_record_print_void, }; static void reftable_index_record_key(const void *r, struct strbuf *dest) @@ -1054,6 +1098,13 @@ static int reftable_index_record_equal(const void *a, const void *b, int hash_si return ia->offset == ib->offset && !strbuf_cmp(&ia->last_key, &ib->last_key); } +static void reftable_index_record_print(const void *rec, int hash_size) +{ + const struct reftable_index_record *idx = rec; + /* TODO: escape null chars? */ + printf("\"%s\" %" PRIu64 "\n", idx->last_key.buf, idx->offset); +} + static struct reftable_record_vtable reftable_index_record_vtable = { .key = &reftable_index_record_key, .type = BLOCK_TYPE_INDEX, @@ -1064,6 +1115,7 @@ static struct reftable_record_vtable reftable_index_record_vtable = { .release = &reftable_index_record_release, .is_deletion = ¬_a_deletion, .equal = &reftable_index_record_equal, + .print = &reftable_index_record_print, }; void reftable_record_key(struct reftable_record *rec, struct strbuf *dest) @@ -1251,3 +1303,9 @@ struct reftable_record reftable_new_record(uint8_t type) } return clean; } + +void reftable_record_print(struct reftable_record *rec, int hash_size) +{ + printf("'%c': ", rec->type); + reftable_record_vtable(rec)->print(reftable_record_data(rec), hash_size); +} diff --git a/reftable/record.h b/reftable/record.h index 010a322e901..fd80cd451d5 100644 --- a/reftable/record.h +++ b/reftable/record.h @@ -61,6 +61,9 @@ struct reftable_record_vtable { /* Are two records equal? This assumes they have the same type. Returns 0 for non-equal. */ int (*equal)(const void *a, const void *b, int hash_size); + + /* Print on stdout, for debugging. */ + void (*print)(const void *rec, int hash_size); }; /* returns true for recognized block types. Block start with the block type. */ @@ -112,6 +115,7 @@ struct reftable_record { /* see struct record_vtable */ int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size); +void reftable_record_print(struct reftable_record *rec, int hash_size); 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, diff --git a/reftable/record_test.c b/reftable/record_test.c index 4a025c48104..847abfa2b04 100644 --- a/reftable/record_test.c +++ b/reftable/record_test.c @@ -23,6 +23,10 @@ static void test_copy(struct reftable_record *rec) /* do it twice to catch memory leaks */ reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ); EXPECT(reftable_record_equal(rec, ©, GIT_SHA1_RAWSZ)); + + puts("testing print coverage:\n"); + reftable_record_print(©, GIT_SHA1_RAWSZ); + reftable_record_release(©); } @@ -175,7 +179,7 @@ static void test_reftable_log_record_equal(void) static void test_reftable_log_record_roundtrip(void) { int i; - struct reftable_log_record in[2] = { + struct reftable_log_record in[] = { { .refname = xstrdup("refs/heads/master"), .update_index = 42, @@ -196,10 +200,24 @@ static void test_reftable_log_record_roundtrip(void) .refname = xstrdup("refs/heads/master"), .update_index = 22, .value_type = REFTABLE_LOG_DELETION, + }, + { + /* make sure we handle optional fields correctly. */ + .refname = xstrdup("branch"), + .update_index = 33, + .value_type = REFTABLE_LOG_UPDATE, + .value = { + .update = { + .old_hash = reftable_malloc(GIT_SHA1_RAWSZ), + .new_hash = reftable_malloc(GIT_SHA1_RAWSZ), + }, + }, } }; set_test_hash(in[0].value.update.new_hash, 1); set_test_hash(in[0].value.update.old_hash, 2); + set_test_hash(in[2].value.update.new_hash, 3); + set_test_hash(in[2].value.update.old_hash, 4); for (i = 0; i < ARRAY_SIZE(in); i++) { struct reftable_record rec = { .type = BLOCK_TYPE_LOG }; struct strbuf key = STRBUF_INIT; From patchwork Mon Jan 24 19:13:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12722852 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 5E7A8C4332F for ; Mon, 24 Jan 2022 19:33:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349045AbiAXTdL (ORCPT ); Mon, 24 Jan 2022 14:33:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352723AbiAXTa7 (ORCPT ); Mon, 24 Jan 2022 14:30:59 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD949C028C1B for ; Mon, 24 Jan 2022 11:14:01 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id w11so3316607wra.4 for ; Mon, 24 Jan 2022 11:14:01 -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=z4D1URX+3kK/Uq4IPVaN54ox5HJIb6dQy07r+RyLXLY=; b=GT4Q2oSSm3AP1RYQnmIxjZR4C7c66pkcZZ7rO8UvHbkc8VlIO4j5aRzAKyK+g9jqI5 qmXXwOA4o+hcRBCfIrO3nP5sxLEDBujgpZq+iDru9FGbd/a230BDfVNJqX618WMCX7a3 Lsgjmc+giXAf/X62hmOPmZkbOT+qnvJBJFlaVgeu5orpzw83F2S/p7tGlgxul4urt/ly CS0u9Q6QMH481+KWl4/UDK0FWIHMVjrjAJN2NOlMDL7Jn1EPHCmRo4UrBxLfM/ay5rqP rt8+daAb7RUm90q+pUBdvEWdZIApsx5YHLvazW7WWiEyWalM95maEWAmY0rUJV7xOc2t 1wXg== 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=z4D1URX+3kK/Uq4IPVaN54ox5HJIb6dQy07r+RyLXLY=; b=H1QcuFZHWVBhd7X3JXFR45zWVhnR9nehqDTjj+QYeokCrermdFESzr22rKBaV/pghj 1fjWd5Ml+BCq+rM/3GH21hLw/wxzlR46CPXixMGir8NqxpAERmUc6tVihTNCS3v1h8HE Q2HfSJQkc+fI68IJfavO6cldIt/63+xTLganBEt8nRuQ5//s3hceGsjahtn6O68OsJsg 7RCGywy+fGPgf1WNUMXVgUknwgTRX9Q27Vg+BRxyuBQFG8rlqS09UjNohHOdKRJ5M3CC 7huaLGEOmWcZjYPbxVXz7mGCuQCPUZCx83lE30UTonI/UANjF/T4Gd0ZZVHY+o87ytFy Z2PQ== X-Gm-Message-State: AOAM5330Z/3o7zYL7LeIhe1sZ9KtcaRA2EaCOhxzsOetyMMNU7tCRoBY Vfk0vtvfc0lV+PBeVNSuyoHB8rfz/sMwlA== X-Google-Smtp-Source: ABdhPJwrLqKHVzcmRRveq4ia1G8mV/1F98pilIHbY4wjAiZASaFazgzjWWGbMzp3LMNQQrARI92bEQ== X-Received: by 2002:adf:fb84:: with SMTP id a4mr15579452wrr.315.1643051640032; Mon, 24 Jan 2022 11:14:00 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n10sm155586wmr.25.2022.01.24.11.13.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jan 2022 11:13:59 -0800 (PST) Message-Id: <556fc11080a67ceda81f6c39fcdd4dc1098ac1b5.1643051624.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 24 Jan 2022 19:13:44 +0000 Subject: [PATCH v7 16/16] reftable: rename typ to type Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , =?utf-8?b?UmVuw6k=?= Scharfe , "brian m. carlson" , Johannes Schindelin , Neeraj Singh , 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 The 'typ' variable name betrays the Go origins of this code (in Go, 'type' is a reserved word). Since we now have parted ways with the Go code, simply use 'type'. Signed-off-by: Han-Wen Nienhuys --- reftable/block.c | 10 ++++---- reftable/merged.c | 6 ++--- reftable/merged.h | 2 +- reftable/reader.c | 58 +++++++++++++++++++++++------------------------ reftable/record.c | 4 ++-- reftable/writer.c | 32 +++++++++++++------------- 6 files changed, 56 insertions(+), 56 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 80ad10bb794..6c1f70ba563 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -68,14 +68,14 @@ static int block_writer_register_restart(struct block_writer *w, int n, return 0; } -void block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf, +void block_writer_init(struct block_writer *bw, uint8_t type, uint8_t *buf, uint32_t block_size, uint32_t header_off, int hash_size) { bw->buf = buf; bw->hash_size = hash_size; bw->block_size = block_size; bw->header_off = header_off; - bw->buf[header_off] = typ; + bw->buf[header_off] = type; bw->next = header_off + 4; bw->restart_interval = 16; bw->entries = 0; @@ -186,7 +186,7 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, int hash_size) { uint32_t full_block_size = table_block_size; - uint8_t typ = block->data[header_off]; + uint8_t type = block->data[header_off]; uint32_t sz = get_be24(block->data + header_off + 1); int err = 0; @@ -195,12 +195,12 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, uint8_t *restart_bytes = NULL; uint8_t *uncompressed = NULL; - if (!reftable_is_block_type(typ)) { + if (!reftable_is_block_type(type)) { err = REFTABLE_FORMAT_ERROR; goto done; } - if (typ == BLOCK_TYPE_LOG) { + if (type == BLOCK_TYPE_LOG) { int block_header_skip = 4 + header_off; uLongf dst_len = sz - block_header_skip; /* total size of dest buffer. */ diff --git a/reftable/merged.c b/reftable/merged.c index 82511762d2a..b5710d5f7a9 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_new_record(mi->type); int err = iterator_next(&mi->stack[i], &rec); if (err < 0) { return err; @@ -58,7 +58,7 @@ static int merged_iter_advance_nonnull_subiter(struct merged_iter *mi, size_t idx) { struct pq_entry e = { - .rec = reftable_new_record(mi->typ), + .rec = reftable_new_record(mi->type), .index = idx, }; int err = iterator_next(&mi->stack[idx], &e.rec); @@ -245,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), + .type = reftable_record_type(rec), .hash_id = mt->hash_id, .suppress_deletions = mt->suppress_deletions, }; diff --git a/reftable/merged.h b/reftable/merged.h index 7d9f95d27ed..63f288ff8d6 100644 --- a/reftable/merged.h +++ b/reftable/merged.h @@ -28,7 +28,7 @@ struct merged_iter { struct reftable_iterator *stack; uint32_t hash_id; size_t stack_len; - uint8_t typ; + uint8_t type; int suppress_deletions; struct merged_iter_pqueue pq; }; diff --git a/reftable/reader.c b/reftable/reader.c index 565cda8d5a2..1ecd23bba36 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -43,9 +43,9 @@ void block_source_close(struct reftable_block_source *source) } static struct reftable_reader_offsets * -reader_offsets_for(struct reftable_reader *r, uint8_t typ) +reader_offsets_for(struct reftable_reader *r, uint8_t type) { - switch (typ) { + switch (type) { case BLOCK_TYPE_REF: return &r->ref_offsets; case BLOCK_TYPE_LOG: @@ -84,7 +84,7 @@ static int parse_footer(struct reftable_reader *r, uint8_t *footer, uint8_t *header) { uint8_t *f = footer; - uint8_t first_block_typ; + uint8_t first_block_type; int err = 0; uint32_t computed_crc; uint32_t file_crc; @@ -149,10 +149,10 @@ static int parse_footer(struct reftable_reader *r, uint8_t *footer, goto done; } - first_block_typ = header[header_size(r->version)]; - r->ref_offsets.is_present = (first_block_typ == BLOCK_TYPE_REF); + first_block_type = header[header_size(r->version)]; + r->ref_offsets.is_present = (first_block_type == BLOCK_TYPE_REF); r->ref_offsets.offset = 0; - r->log_offsets.is_present = (first_block_typ == BLOCK_TYPE_LOG || + r->log_offsets.is_present = (first_block_type == BLOCK_TYPE_LOG || r->log_offsets.offset > 0); r->obj_offsets.is_present = r->obj_offsets.offset > 0; err = 0; @@ -214,7 +214,7 @@ done: struct table_iter { struct reftable_reader *r; - uint8_t typ; + uint8_t type; uint64_t block_off; struct block_iter bi; int is_finished; @@ -228,7 +228,7 @@ static void table_iter_copy_from(struct table_iter *dest, struct table_iter *src) { dest->r = src->r; - dest->typ = src->typ; + dest->type = src->type; dest->block_off = src->block_off; dest->is_finished = src->is_finished; block_iter_copy_from(&dest->bi, &src->bi); @@ -257,7 +257,7 @@ static void table_iter_block_done(struct table_iter *ti) ti->bi.next_off = 0; } -static int32_t extract_block_size(uint8_t *data, uint8_t *typ, uint64_t off, +static int32_t extract_block_size(uint8_t *data, uint8_t *type, uint64_t off, int version) { int32_t result = 0; @@ -266,20 +266,20 @@ static int32_t extract_block_size(uint8_t *data, uint8_t *typ, uint64_t off, data += header_size(version); } - *typ = data[0]; - if (reftable_is_block_type(*typ)) { + *type = data[0]; + if (reftable_is_block_type(*type)) { result = get_be24(data + 1); } return result; } int reader_init_block_reader(struct reftable_reader *r, struct block_reader *br, - uint64_t next_off, uint8_t want_typ) + uint64_t next_off, uint8_t want_type) { int32_t guess_block_size = r->block_size ? r->block_size : DEFAULT_BLOCK_SIZE; struct reftable_block block = { NULL }; - uint8_t block_typ = 0; + uint8_t block_type = 0; int err = 0; uint32_t header_off = next_off ? 0 : header_size(r->version); int32_t block_size = 0; @@ -291,13 +291,13 @@ int reader_init_block_reader(struct reftable_reader *r, struct block_reader *br, if (err < 0) goto done; - block_size = extract_block_size(block.data, &block_typ, next_off, + block_size = extract_block_size(block.data, &block_type, next_off, r->version); if (block_size < 0) { err = block_size; goto done; } - if (want_typ != BLOCK_TYPE_ANY && block_typ != want_typ) { + if (want_type != BLOCK_TYPE_ANY && block_type != want_type) { err = 1; goto done; } @@ -326,10 +326,10 @@ static int table_iter_next_block(struct table_iter *dest, int err = 0; dest->r = src->r; - dest->typ = src->typ; + dest->type = src->type; dest->block_off = next_block_off; - err = reader_init_block_reader(src->r, &br, next_block_off, src->typ); + err = reader_init_block_reader(src->r, &br, next_block_off, src->type); if (err > 0) { dest->is_finished = 1; return 1; @@ -349,7 +349,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 (reftable_record_type(rec) != ti->type) return REFTABLE_API_ERROR; while (1) { @@ -404,38 +404,38 @@ static void iterator_from_table_iter(struct reftable_iterator *it, static int reader_table_iter_at(struct reftable_reader *r, struct table_iter *ti, uint64_t off, - uint8_t typ) + uint8_t type) { struct block_reader br = { 0 }; struct block_reader *brp = NULL; - int err = reader_init_block_reader(r, &br, off, typ); + int err = reader_init_block_reader(r, &br, off, type); if (err != 0) return err; brp = reftable_malloc(sizeof(struct block_reader)); *brp = br; ti->r = r; - ti->typ = block_reader_type(brp); + ti->type = block_reader_type(brp); ti->block_off = off; block_reader_start(brp, &ti->bi); return 0; } static int reader_start(struct reftable_reader *r, struct table_iter *ti, - uint8_t typ, int index) + uint8_t type, int index) { - struct reftable_reader_offsets *offs = reader_offsets_for(r, typ); + struct reftable_reader_offsets *offs = reader_offsets_for(r, type); uint64_t off = offs->offset; if (index) { off = offs->index_offset; if (off == 0) { return 1; } - typ = BLOCK_TYPE_INDEX; + type = BLOCK_TYPE_INDEX; } - return reader_table_iter_at(r, ti, off, typ); + return reader_table_iter_at(r, ti, off, type); } static int reader_seek_linear(struct reftable_reader *r, struct table_iter *ti, @@ -522,12 +522,12 @@ static int reader_seek_indexed(struct reftable_reader *r, if (err < 0) goto done; - if (next.typ == reftable_record_type(rec)) { + if (next.type == reftable_record_type(rec)) { err = 0; break; } - if (next.typ != BLOCK_TYPE_INDEX) { + if (next.type != BLOCK_TYPE_INDEX) { err = REFTABLE_FORMAT_ERROR; break; } @@ -582,9 +582,9 @@ 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 type = reftable_record_type(rec); - struct reftable_reader_offsets *offs = reader_offsets_for(r, typ); + struct reftable_reader_offsets *offs = reader_offsets_for(r, type); if (!offs->is_present) { iterator_set_empty(it); return 0; diff --git a/reftable/record.c b/reftable/record.c index c57bac0e96a..8330b7a2589 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -78,9 +78,9 @@ int put_var_int(struct string_view *dest, uint64_t val) return n; } -int reftable_is_block_type(uint8_t typ) +int reftable_is_block_type(uint8_t type) { - switch (typ) { + switch (type) { case BLOCK_TYPE_REF: case BLOCK_TYPE_LOG: case BLOCK_TYPE_OBJ: diff --git a/reftable/writer.c b/reftable/writer.c index 96a5155ea77..5b768438625 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -26,9 +26,9 @@ static void writer_clear_index(struct reftable_writer *w); static int writer_finish_public_section(struct reftable_writer *w); static struct reftable_block_stats * -writer_reftable_block_stats(struct reftable_writer *w, uint8_t typ) +writer_reftable_block_stats(struct reftable_writer *w, uint8_t type) { - switch (typ) { + switch (type) { case 'r': return &w->stats.ref_stats; case 'o': @@ -102,7 +102,7 @@ static int writer_write_header(struct reftable_writer *w, uint8_t *dest) return header_size(writer_version(w)); } -static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ) +static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t type) { int block_start = 0; if (w->next == 0) { @@ -110,7 +110,7 @@ static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ) } strbuf_release(&w->last_key); - block_writer_init(&w->block_writer_data, typ, w->block, + block_writer_init(&w->block_writer_data, type, w->block, w->opts.block_size, block_start, hash_size(w->opts.hash_id)); w->block_writer = &w->block_writer_data; @@ -375,7 +375,7 @@ int reftable_writer_add_logs(struct reftable_writer *w, static int writer_finish_section(struct reftable_writer *w) { - uint8_t typ = block_writer_type(w->block_writer); + uint8_t type = block_writer_type(w->block_writer); uint64_t index_start = 0; int max_level = 0; int threshold = w->opts.unpadded ? 1 : 3; @@ -434,7 +434,7 @@ static int writer_finish_section(struct reftable_writer *w) if (err < 0) return err; - bstats = writer_reftable_block_stats(w, typ); + bstats = writer_reftable_block_stats(w, type); bstats->index_blocks = w->stats.idx_stats.blocks - before_blocks; bstats->index_offset = index_start; bstats->max_index_level = max_level; @@ -536,17 +536,17 @@ static int writer_dump_object_index(struct reftable_writer *w) static int writer_finish_public_section(struct reftable_writer *w) { - uint8_t typ = 0; + uint8_t type = 0; int err = 0; if (w->block_writer == NULL) return 0; - typ = block_writer_type(w->block_writer); + type = block_writer_type(w->block_writer); err = writer_finish_section(w); if (err < 0) return err; - if (typ == BLOCK_TYPE_REF && !w->opts.skip_index_objects && + if (type == BLOCK_TYPE_REF && !w->opts.skip_index_objects && w->stats.ref_stats.index_blocks > 0) { err = writer_dump_object_index(w); if (err < 0) @@ -630,10 +630,10 @@ static const int debug = 0; static int writer_flush_nonempty_block(struct reftable_writer *w) { - uint8_t typ = block_writer_type(w->block_writer); + uint8_t type = block_writer_type(w->block_writer); struct reftable_block_stats *bstats = - writer_reftable_block_stats(w, typ); - uint64_t block_typ_off = (bstats->blocks == 0) ? w->next : 0; + writer_reftable_block_stats(w, type); + uint64_t block_type_off = (bstats->blocks == 0) ? w->next : 0; int raw_bytes = block_writer_finish(w->block_writer); int padding = 0; int err = 0; @@ -641,12 +641,12 @@ static int writer_flush_nonempty_block(struct reftable_writer *w) if (raw_bytes < 0) return raw_bytes; - if (!w->opts.unpadded && typ != BLOCK_TYPE_LOG) { + if (!w->opts.unpadded && type != BLOCK_TYPE_LOG) { padding = w->opts.block_size - raw_bytes; } - if (block_typ_off > 0) { - bstats->offset = block_typ_off; + if (block_type_off > 0) { + bstats->offset = block_type_off; } bstats->entries += w->block_writer->entries; @@ -655,7 +655,7 @@ static int writer_flush_nonempty_block(struct reftable_writer *w) w->stats.blocks++; if (debug) { - fprintf(stderr, "block %c off %" PRIu64 " sz %d (%d)\n", typ, + fprintf(stderr, "block %c off %" PRIu64 " sz %d (%d)\n", type, w->next, raw_bytes, get_be24(w->block + w->block_writer->header_off + 1)); }