From patchwork Tue Dec 14 11:47:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675859 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 9DFFEC433EF for ; Tue, 14 Dec 2021 11:48:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233706AbhLNLsD (ORCPT ); Tue, 14 Dec 2021 06:48:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233697AbhLNLsA (ORCPT ); Tue, 14 Dec 2021 06:48:00 -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 6DE98C061574 for ; Tue, 14 Dec 2021 03:48:00 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id c6-20020a05600c0ac600b0033c3aedd30aso13533816wmr.5 for ; Tue, 14 Dec 2021 03:48: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=4caTpC3+u83vTS7zkUHAtzr+4yoHPBYES+caC91RmF4=; b=hlOeNdh6eznDJlILO2ce2SZ0ZHd0hRov3vTCiz3VGXQt7RMWHcE60iXh+sHzs0GVjE 3WzKvoy8UzDTPjll2ezCpD2ZCPvcYQmGXBhdQ1lv3ZicC4DAm1W26fSX+dirzR80m2k9 g6niIV4R3Ly5YuqWmz8YXWqQPuKWOnrrbQXGlPMtJrfwSRaElm5EBMs+HzXuvAUwTz/0 3ZXUjG9arwdgrNEBLdKG/gLFvm68VraAh4BVQoKc0jfzn2yMpGiys8+YmiOMKeJgETBw TRw2PzDF1CwppiF+ouByAKOBuTLJSmKImj+2DtQT/z0t73Ta+q8fPGqzfwQrQptL08k7 PBWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=4caTpC3+u83vTS7zkUHAtzr+4yoHPBYES+caC91RmF4=; b=ywaByp+FTtKyKxWRYiE/4LT6sGEZNE2vzxM4MovBaPzWNarxn3nc7hIVlFd2m+ReH5 6PDFmTdNy93sYuuz9fOdsagbyNAeHdyVOim3inis8BAs1Gprh+0Ct7Q1YAIEDQQIMvN7 MMFEYME2Ua3g8Y1wSrDARUC+GMZ9kNBNqbSDlLVUdnQtT4gx2FoiK5BM7ku+i574ZuIB QfJetj4s7aVmxQYhYjN7G1PAJMz1oGzpUbV4XosmdyIul9KgNumoax3K/gAjYEtsbi8i Fx92Kd+zUsxxvtb9dAdZMT4i1m/cQ1S422Y2B+pjpCJKKHEMuOi2jFpu9lIxLS7WCOB/ IHKw== X-Gm-Message-State: AOAM530lAeXqFDbuTpqVxeCqOk/8/CwYFV1+bWAgY6wESHcOudbRJSwD FiadQI0O87Pik9MeZucQF/FKulebIIo= X-Google-Smtp-Source: ABdhPJwwuy4H/4+63e79xKZR82wZtDFpoikyMzb0lHi7Kaw52Ige0/cErbvtQjwuC+XRSfH3QKl6pA== X-Received: by 2002:a05:600c:4f14:: with SMTP id l20mr45154201wmq.164.1639482478937; Tue, 14 Dec 2021 03:47:58 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m9sm1864171wmq.1.2021.12.14.03.47.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:47:58 -0800 (PST) Message-Id: <1c1a3ff92bd24f7c588ce480983cecb3508a0add.1639482477.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:46 +0000 Subject: [PATCH v4 01/11] reftable: fix OOB stack write in print functions Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Signed-off-by: Han-Wen Nienhuys --- reftable/record.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 6a5dac32dc6..8536bd03aa9 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -254,7 +254,7 @@ static void hex_format(char *dest, uint8_t *src, int hash_size) void reftable_ref_record_print(struct reftable_ref_record *ref, uint32_t hash_id) { - char hex[2 * GIT_SHA256_RAWSZ + 1] = { 0 }; /* BUG */ + char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */ printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index); switch (ref->value_type) { case REFTABLE_REF_SYMREF: @@ -586,7 +586,7 @@ static struct reftable_record_vtable reftable_obj_record_vtable = { void reftable_log_record_print(struct reftable_log_record *log, uint32_t hash_id) { - char hex[GIT_SHA256_RAWSZ + 1] = { 0 }; + char hex[GIT_MAX_HEXSZ + 1] = { 0 }; switch (log->value_type) { case REFTABLE_LOG_DELETION: From patchwork Tue Dec 14 11:47:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675861 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 3738DC433FE for ; Tue, 14 Dec 2021 11:48:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233734AbhLNLsK (ORCPT ); Tue, 14 Dec 2021 06:48:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233699AbhLNLsB (ORCPT ); Tue, 14 Dec 2021 06:48:01 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 374CDC061574 for ; Tue, 14 Dec 2021 03:48:01 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id 77-20020a1c0450000000b0033123de3425so16098672wme.0 for ; Tue, 14 Dec 2021 03:48: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=5WQciBJg+b+a7i06YFpIhdeArC43KJcMStyqTq1E0/E=; b=m92/UolKfX69IfWzNCjK0FEoG3pM6/USzOEmIwArs0DaLwXaXqxjB7AXHDX2x4XN1b OGQ+St7w2a865tetpAI76S48x82MVWFhsug5GjE7S1j9SAodYyOsV1t7FJR8ZMCIFu/X cmd5BaBoLymO3REnHOYo34xm+Z4TkmJK0xLu6VpycKUbVcsfowdRxRQmebnKeOVFZ2rZ aZMUOFDPA6z4N+aw+LKAcYFyVFkw8nGvSh81fX3D75wBxU9m51bIpluQdNoAtBGP8NeH j3edkBBtbAkYDLRdRq3yFkVKMAm1wUap0PDYnNb4N8KtG98a30Gu7LxqlMsgO3D5Zo9u THJA== 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=5WQciBJg+b+a7i06YFpIhdeArC43KJcMStyqTq1E0/E=; b=Wm5HN/XKk+D/S5Gl5ilWWZiyRLRSvbRvZSHKL+zBc6/IdXkGmjqa2eosafq2AXonHl cDsBa46teWhSEYJoGQR7YeXAcHH8lXaZuVuqhO8rFptKaGsDzJoZFWLBEEAAd7LkoEPC OY2xc5JNfPQyCbVvKdBLtfRUfefYN/uLaBLL5XOy4ROVLRFp9BRBsNZRD6bKjZu1khEH as5W4IVR/+IIY+uG8S9xYcEvvIs7rD/qg5CrgmtTAZxaepPItttzzwrAKn7jcJOgxFxp UA1G8Iny/hQIlFTVUk5ZAwUjchf/ebcQv/xPVfzC3Fav4VSDfOjVY+Vn5fEtB3PKh9hj p9bw== X-Gm-Message-State: AOAM530CZpFMFYuUQesUFyvejm7Tqk7aVAlfKCsTaeS7GH79Nai/GW25 SB2dtLrjMFVES1IvPiQlgmkGT6EdGp4= X-Google-Smtp-Source: ABdhPJyPlu32O34kHgw/sy15OAf+6z+Jy664otxpiEMBoSTGw9EP8y23NKFyffrs30UwhcciUFbctQ== X-Received: by 2002:a05:600c:214f:: with SMTP id v15mr6375411wml.194.1639482479612; Tue, 14 Dec 2021 03:47:59 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w7sm13327936wru.51.2021.12.14.03.47.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:47:59 -0800 (PST) Message-Id: <923293b79b4a450df1b6577a0e6a696496ce46db.1639482477.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:47 +0000 Subject: [PATCH v4 02/11] 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 , 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. This problem was discovered by a Coverity scan. Signed-off-by: Han-Wen Nienhuys --- reftable/block.c | 26 +++++++++------ reftable/readwrite_test.c | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 855e3f5c947..6c8e8705205 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -188,13 +188,16 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, uint32_t full_block_size = table_block_size; uint8_t typ = block->data[header_off]; uint32_t sz = get_be24(block->data + header_off + 1); - + int err = 0; uint16_t restart_count = 0; uint32_t restart_start = 0; uint8_t *restart_bytes = NULL; + uint8_t *uncompressed = NULL; - if (!reftable_is_block_type(typ)) - return REFTABLE_FORMAT_ERROR; + if (!reftable_is_block_type(typ)) { + err = REFTABLE_FORMAT_ERROR; + goto done; + } if (typ == BLOCK_TYPE_LOG) { int block_header_skip = 4 + header_off; @@ -203,7 +206,7 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, uLongf src_len = block->len - block_header_skip; /* Log blocks specify the *uncompressed* size in their header. */ - uint8_t *uncompressed = reftable_malloc(sz); + uncompressed = reftable_malloc(sz); /* Copy over the block header verbatim. It's not compressed. */ memcpy(uncompressed, block->data, block_header_skip); @@ -212,16 +215,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 +257,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/readwrite_test.c b/reftable/readwrite_test.c index 5f6bcc2f775..6e88182a83a 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -254,6 +254,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; @@ -633,6 +698,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 Tue Dec 14 11:47:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675865 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 34F9DC433EF for ; Tue, 14 Dec 2021 11:48:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233744AbhLNLsP (ORCPT ); Tue, 14 Dec 2021 06:48:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233703AbhLNLsD (ORCPT ); Tue, 14 Dec 2021 06:48:03 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6C17C06173F for ; Tue, 14 Dec 2021 03:48:01 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id i22so2117445wrb.13 for ; Tue, 14 Dec 2021 03:48: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=zeOacPG8foepzoe93AsPL3Dhk+VXAN8S9blbgW2/nvM=; b=IuXZmHH44Lek607rDpdfaQyhHvuq/lqbJnocc12KGN9frAbqfMVgwl1csLCgIhAUz/ WTm7Zzs/fHmYRk3IlX9QEA+92M0u8MfxhV48WHLCnBdJYMeqTHJDImRpmUktHZ8QPqDw dKZFPKDODBiRIeOiS0B+ReU4urn2yhHcm+yKhpUkTtyRi8g4uv7b4M1188xvBexwMt5V c8qWfcybKzC6TMpmVNeJqMbzsvqipMj7nLOMXHyi9ia70VdRDJ6XFKI0f92zz2HOZ2ah sVSTRCGODsH1j5rYIn63nJINBbSWJ8mNLG1Qwz9gvP6aqj/jLmD0nyE7oN6smcwe9HTl Kdhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=zeOacPG8foepzoe93AsPL3Dhk+VXAN8S9blbgW2/nvM=; b=kY7iJQx+6fj6eIVsNTXhjT6UyJz6a8NmhhbfghwXBN6pKkCjaRwiIzIvmE7Y6Og9Io lGX2Jis9A9ath/2sHyi+bdetqXwEiofbfo6NcHzHgcr5g1xl2DovL0imKI9xMQ5BS9zW iAAcEbN+pk5lVO+ArxOmr1KHEKs91xCAjzpFDOa+BrAZIlTYEfuyWsHKclavZPt6ZD89 6+LnPg/o9mweoEuwmf7d8Jxs7l7xQC6mvyrcMmeWbTbzxVOSPm7PYegJTjHtSDhByABw 3XpQ4eXcnKeCpBl/PU9RTfyrCLEKupt29SBnU6oNOOIADpvHXk7R4yA8I8ck3LSGgzeq torQ== X-Gm-Message-State: AOAM532BDwTEaRe3Dzme4FzRLdDxgs1Ft0lrTf06VuhrX1uC7pjrzWL/ Rj30NUDjOrDDeTonQ3UHt6sR2z4hveA= X-Google-Smtp-Source: ABdhPJzincGeIkeC3DfxbSOZF9Ak6i6ujJUw9U9FzuC4tbm9/9pQbikcyKUSN8Biy5kONGrf5bUNWg== X-Received: by 2002:a5d:64af:: with SMTP id m15mr5310914wrp.267.1639482480254; Tue, 14 Dec 2021 03:48:00 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y6sm15187289wrh.18.2021.12.14.03.47.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:47:59 -0800 (PST) Message-Id: <7a914f77756bc7c9ec5338584f955cb5930ea4e4.1639482477.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:48 +0000 Subject: [PATCH v4 03/11] reftable: fix resource leak blocksource.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This would be triggered in the unlikely event of fstat() failing on an opened file. Signed-off-by: Han-Wen Nienhuys --- reftable/blocksource.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/reftable/blocksource.c b/reftable/blocksource.c index 0044eecd9aa..2605371c28d 100644 --- a/reftable/blocksource.c +++ b/reftable/blocksource.c @@ -134,8 +134,10 @@ int reftable_block_source_from_file(struct reftable_block_source *bs, } err = fstat(fd, &st); - if (err < 0) - return -1; + if (err < 0) { + close(fd); + return REFTABLE_IO_ERROR; + } p = reftable_calloc(sizeof(struct file_block_source)); p->size = st.st_size; From patchwork Tue Dec 14 11:47:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675863 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 83F51C433EF for ; Tue, 14 Dec 2021 11:48:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233738AbhLNLsL (ORCPT ); Tue, 14 Dec 2021 06:48:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233704AbhLNLsD (ORCPT ); Tue, 14 Dec 2021 06:48:03 -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 79507C061748 for ; Tue, 14 Dec 2021 03:48:02 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id t9so31918450wrx.7 for ; Tue, 14 Dec 2021 03:48:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=6v0+JuvgL+rkV6+ZUY0aCiH0eSlBA7tjh6rJWXdXHEw=; b=c73frBoM2eIMIdBZaswTZRnKstwVRV+qUekdeKep3ktKsC+X6ahTKGxUtplttNYPvg zkGzs5tbk+bzA9Iy01Nr+yfHOVQ1gErBj/1WdNLBeql1qDSy4Y2mtipxHBWReorK1ohz A/H2C55FObhTqk5I10JaFThp7XkKo/gl7PFoIhPe4A1viA1CS24mgApEhYoPg8+/mzNo lGk39GTKovfrYSlQefcvubEpmP4guY8ul+0AB7QF+KUzlRqiEEOQOEgmcFlkEnfrTttz M/nXfgtUOnDpUe3/a1qatqQGmHyOLOVhRusZ1F/3kkE0Y3EMfhx0RTXifQljtnhCJ/um K7Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=6v0+JuvgL+rkV6+ZUY0aCiH0eSlBA7tjh6rJWXdXHEw=; b=vssh6ccy5jqtpRoLCaWzGYkdJDFtHHJo7XUQFhPkGRZ5Hrl1bLfcu7KD2++jgjQH2t x1+pTz4ITn6PxUS5ZS8TZ5Fr1AGWvfmjvOPX4g4+bopHJOoGmRe8qZ6k/NUho4IYZ912 GULJgDo8uO6NghPuYpfKVy2COf0KhClGMooqBDP4PmJFp0AM4kkN+M1EVhHiAb+USYdt +xZuRC0mgZU1CXMdOrqauqv3ZQ480ChpohJBKhN+NwHiWE5UfRUBcRiGBGKC5yc2Q/gH aU0bbcq2xlu+yHqaVD8374P6dk+aMF00ymCEYwF/UXGJ9stK6o6zlfWEwa8VI6UKcD2Z io5Q== X-Gm-Message-State: AOAM531zSH+MJipo2UaIDNwUL36yDiLbrOb/fZPG/f7i/jXporoO+E8z WvX2eiwOaQ8DcX/dRxTzKCmZPHicBlI= X-Google-Smtp-Source: ABdhPJyRvdfCpBl7uSkMAzDcqly8vwLMnZNLw/DJxTyWnX9PYAb1LDOZj5Krr9E0FtUDVKfl8OAS2w== X-Received: by 2002:a5d:4575:: with SMTP id a21mr5369685wrc.193.1639482481001; Tue, 14 Dec 2021 03:48:01 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id 8sm8387719wrb.49.2021.12.14.03.48.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:48:00 -0800 (PST) Message-Id: <51b4a17a2e1f867c47d0b00f72721ef6e21fab04.1639482477.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:49 +0000 Subject: [PATCH v4 04/11] reftable: check reftable_stack_auto_compact() return value Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Fixes a problem detected by Coverity. Signed-off-by: Han-Wen Nienhuys --- reftable/stack_test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/reftable/stack_test.c b/reftable/stack_test.c index eb0b7228b0c..d628420e63a 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -814,6 +814,7 @@ static void test_reftable_stack_auto_compaction(void) EXPECT_ERR(err); err = reftable_stack_auto_compact(st); + EXPECT_ERR(err); EXPECT(i < 3 || st->merged->stack_len < 2 * fastlog2(i)); } From patchwork Tue Dec 14 11:47:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675867 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 BB2F5C433FE for ; Tue, 14 Dec 2021 11:48:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233722AbhLNLsR (ORCPT ); Tue, 14 Dec 2021 06:48:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233707AbhLNLsD (ORCPT ); Tue, 14 Dec 2021 06:48:03 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FC9CC061751 for ; Tue, 14 Dec 2021 03:48:03 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id y196so14020118wmc.3 for ; Tue, 14 Dec 2021 03:48:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=J9jVqwMPaNdDSNjujdVEAD8serufDJ5UG7qKldS2NTo=; b=Lm9CEm1FvyDP16mfEwyD0C8AEtR9ap79TeU0pnqU32SW0k+r22c7f9R+JyITW3nFYZ +btK8/gHadkYHuMbwLx8+C7E5C7GfKYcWiLppTnJGyLyd48YV2+9UxfYvMSM/ZHahIGU d5+9m4Fm+6YcUX0C7Wgklu9xPZYW/xAVC6XGOQt0aF+DNa0/P/LPq8Z4RZ0mM5MamX6O nyiQ5tiwzHxqDNR+ekUVnaTo/lpggsy18B2MyB933XrX1BK7gt9pFW4kzQ0RwDVCNTGi U0k23JRLnS3DS2HOnGb38KR/l5srxZCJ9Sg3cyu9V/VVoFcAaxRcpGOG1m/epeR5QwtX dFXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=J9jVqwMPaNdDSNjujdVEAD8serufDJ5UG7qKldS2NTo=; b=zXMvRE38L3e2SS6u3fWqoBSDClncQjYSvl5coVSdHHLTWhq94Xfq/K2W0cwEWo68L7 qCJ3yZQLCW4mxEXu4y/SL7etyBwuuADm7KfA1wHKb+uBUsOzARnPX+Tt6wY7w6cgwfkD lGcPW+zl30Hvun3/JnKgapO+ZlZ1XdYAB+y3q3gWiBZhXdpaKjL4qfbjQLxmjH+6TH9k BXUXasgu3yU0iKxi1uumpXMFvMw0M1YiroNgr05ZyxT9ve+0Hlj7dSOxdWyVTzSacJDD nIXmRAeuCk18jszjUHvVZps/V4NOgN6/Qhorduycw/HuKE6R2GBSWWXg9caCzVTuyZL5 MGZw== X-Gm-Message-State: AOAM53202JWC+nzrwruu9qBZpkayxZCigmlqQ2BPkN8bPVtxjm28MrQD 5doJDIb7j5zvQC/AxtUk5lyzV/YvSvU= X-Google-Smtp-Source: ABdhPJwMzsVaISmyYImb06h+m44qOx4059OpI+aKJBEgnDQiOQVDqOAQ8CKBOsB/JI/z9bCXfEUUEQ== X-Received: by 2002:a1c:4e04:: with SMTP id g4mr45286136wmh.15.1639482481629; Tue, 14 Dec 2021 03:48:01 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t127sm2132768wma.9.2021.12.14.03.48.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:48:01 -0800 (PST) Message-Id: <43989afcb5a533c892739cf3d95f18b6d447e0e3.1639482477.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:50 +0000 Subject: [PATCH v4 05/11] reftable: ignore remove() return value in stack_test.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys If the cleanup fails, there is nothing we can do. Signed-off-by: Han-Wen Nienhuys --- reftable/stack_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reftable/stack_test.c b/reftable/stack_test.c index d628420e63a..4b7292945c3 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -89,7 +89,7 @@ static void test_read_file(void) EXPECT(0 == strcmp(want[i], names[i])); } free_names(names); - remove(fn); + (void) remove(fn); } static void test_parse_names(void) From patchwork Tue Dec 14 11:47:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675869 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 4C92BC433F5 for ; Tue, 14 Dec 2021 11:48:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233737AbhLNLsS (ORCPT ); Tue, 14 Dec 2021 06:48:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233714AbhLNLsE (ORCPT ); Tue, 14 Dec 2021 06:48:04 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02792C061574 for ; Tue, 14 Dec 2021 03:48:04 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id y196so14020148wmc.3 for ; Tue, 14 Dec 2021 03:48:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=HOTpe9FNENotMYcACPCrU+33ZDetlyOQNtsGAWihsVI=; b=eCBQdLRx6Stfhrh5bZub6RjrrWnYoOuSuNaeBW1z7EnV1NkSmDCZkcGpTNf+NXM0Je hMrwp0YtYIcjVEg1N+BUbY3e1t3ii1in5NCRza8RZSGYZpYHvZAGAhEVgL+a6/tTT1YT y3yAADs2VJdDM+gyZnSPcwygdiMqjAjDfH/tGEQS8CbIwmPwoQliwOwsOyt1B/EvUvln 1B1JaqJtg9IVlrxOcZ09HMlwuJVJh2VDV0M7nibnmDu1roX4M61A1sh77Hr6In7xtuH4 7P72L3jqYC5NTP3VRlfakqQRbOPEZWyOyUgnnb65NPQE7PYLffDVAl44c099YncWJOJK RzGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=HOTpe9FNENotMYcACPCrU+33ZDetlyOQNtsGAWihsVI=; b=7ekgmBxpiA45NceP0mTWhI+ur5h2+p/+rjOGS8SgXlGu+N+9h5/qmNOZcz8DaWVpSR w6nCU1cFsb4GJf1cRqlAGoVshOLupCaCxA3zLMp8bj6OAbcaFCsQP40THLFiINkL9j4V sbdSEhmT+BU+SrsBW0SWJwW5RNV/Jz48Aq+kKiDWS6NFsdIz7j/5uUXECfS7wLvCqmnx /uSN4OjKvy6T2+ciLMtsg9oPM3Sx+dgWym2N6/ElhPOx017djuLSYnXoEhfx4eFnL46m mHMiH8oeBtyrZGPSzgEdNVys2b8Hnp8YkNH2prjPh3WdH/oPR12fFREWq5sDe01s5cYa rOHQ== X-Gm-Message-State: AOAM5306QOtUnTcjcG5mcFVLhWVzjjf8BvLKRzODzusRoJuu2s/Oxhaf kOL6n6rS+5Cq+6nI9X0ZNc0Uc8Q5JIY= X-Google-Smtp-Source: ABdhPJy+9KPcUm9PIJpcLRkVjhQF9PJKFlrKbztuuGJUtJtCk38zpfpXFuOYy66PFxjcNFvT/h13rw== X-Received: by 2002:a05:600c:8a7:: with SMTP id l39mr3073797wmp.138.1639482482426; Tue, 14 Dec 2021 03:48:02 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w8sm11157161wre.106.2021.12.14.03.48.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:48:01 -0800 (PST) Message-Id: <1c7f15d811c4a73f1fd7b24de52fcd1869126982.1639482477.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:51 +0000 Subject: [PATCH v4 06/11] 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 , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This would trigger in the unlikely event that we are compacting, and the next available file handle is 0. Signed-off-by: Han-Wen Nienhuys --- reftable/stack.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index df5021ebf08..10dfd370e8e 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -877,7 +877,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, struct strbuf new_table_path = STRBUF_INIT; int err = 0; int have_lock = 0; - int lock_file_fd = 0; + int lock_file_fd = -1; int compact_count = last - first + 1; char **listp = NULL; char **delete_on_success = @@ -911,7 +911,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, } /* Don't want to write to the lock for now. */ close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; have_lock = 1; err = stack_uptodate(st); @@ -932,7 +932,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, sublock_file_fd = open(subtab_lock.buf, O_EXCL | O_CREAT | O_WRONLY, 0644); - if (sublock_file_fd > 0) { + if (sublock_file_fd >= 0) { close(sublock_file_fd); } else if (sublock_file_fd < 0) { if (errno == EEXIST) { @@ -1013,7 +1013,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, goto done; } err = close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; if (err < 0) { err = REFTABLE_IO_ERROR; unlink(new_table_path.buf); @@ -1050,9 +1050,9 @@ done: listp++; } free_names(subtable_locks); - if (lock_file_fd > 0) { + if (lock_file_fd >= 0) { close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; } if (have_lock) { unlink(lock_file_name.buf); From patchwork Tue Dec 14 11:47:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675871 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 9C441C433EF for ; Tue, 14 Dec 2021 11:48:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233717AbhLNLsV (ORCPT ); Tue, 14 Dec 2021 06:48:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231233AbhLNLsF (ORCPT ); Tue, 14 Dec 2021 06:48:05 -0500 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91598C06173F for ; Tue, 14 Dec 2021 03:48:04 -0800 (PST) Received: by mail-wr1-x42b.google.com with SMTP id o13so31857054wrs.12 for ; Tue, 14 Dec 2021 03:48:04 -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=mj3roB/KNfN5aSaawhZhUchjzd3HSZTsombt9lwgcBE=; b=TAKvQglnmb6e4IKXg+2EzZAkdj/M0HiAXsl3XyKuDpxCPbmjLhS0k0T7TN9KNnEMyR YqPW7YZ/VIztPLORi7VVId3Sj3+pSdt4vytvVsRCQ4t0ZwD1kOLZ4bssyUtFqccUj0EF eTJxLcmR0yv+JwGBAPk/Rw8ejmE6684JzE9EYZCryDHdYmBm4Mg5dm8uaoqRDtV/qpu/ cn5UDFi2jCKdeKRTZ4aCgliZ27zIlhOpduYyMJjZfptkrexiV3n/iGTUPnmBADyrdMh6 pBc3EUoVbvGIt3rLEzEkrPBWWJUKjOXCsV3Df/q44izMHcLcFmY2niI0kS7YaFSAYpzL I/Qw== 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=mj3roB/KNfN5aSaawhZhUchjzd3HSZTsombt9lwgcBE=; b=wwyqoe/AQxWIdTSxQ0Ggf6lrZx3yq3JsiL8PxN4Mh1ScoPyMZobhaDS87SN4C2KI9H f8CqQsEHgR5i3zqLMCgXDsZ8LMSrqpVZp3C3n0d2bJo3jqJ1GGRQi1rcPqTmBA4mg2sk JlKn1b/PHXAjiV6GRGrU4a7NjKykCt2lnfWESG3WZxD7aPJ09lQe3u0e07AZYdjN3DNv X520dcKZa6ne0zV+Szz1W9CP6WLrGsD3CTprTqMejYzNVBvV8V6a396YlnWhNbS8GFay Zj9dRn7kd1hg3i6zm/DVFQvCB6CMo65IIY0FJoeYZmG0xyyUcZLirTH4MrJtRuhsulm9 Tfrg== X-Gm-Message-State: AOAM5328QTzlfoLnq/BID4lCxEtAb+71DBQcksK5NUX+VNlOzbg8+mg/ VVL3mYJdJnpRurCV1pNXRCUDcUWsjJ0= X-Google-Smtp-Source: ABdhPJx+TWFjBAzCdAmUdJySzq8anELw+6Kq69NqV8YbdRIphiOWpSFrhFvuHs5OodRF5ytF5vP5mQ== X-Received: by 2002:adf:fc0d:: with SMTP id i13mr4890137wrr.699.1639482483111; Tue, 14 Dec 2021 03:48:03 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id e7sm17448118wrg.31.2021.12.14.03.48.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:48:02 -0800 (PST) Message-Id: <47ba5ddceb8cc864cdc3006f7718e00f6ea2c7fc.1639482477.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:52 +0000 Subject: [PATCH v4 07/11] 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 , 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 006709a645a..733509606a9 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -641,6 +641,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 3ca721e9f64..9fd24fa93ce 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 Tue Dec 14 11:47:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675873 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 6F57EC433F5 for ; Tue, 14 Dec 2021 11:48:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233696AbhLNLsW (ORCPT ); Tue, 14 Dec 2021 06:48:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233729AbhLNLsG (ORCPT ); Tue, 14 Dec 2021 06:48:06 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 429CEC0613F8 for ; Tue, 14 Dec 2021 03:48:05 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id y196so14020186wmc.3 for ; Tue, 14 Dec 2021 03:48:05 -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=7hrVqwDEsCy4jvGx2ygf2ufkTVUbjR+/cx/GyTSeOVg=; b=b/jrt20dUAWuvvXQFf1zfZyTXbfZzuJ0Wh+fWLJLz6R6TjOoFsiT5UR5FFk52msd3y ULxY4L2bigvdolUDbZQ9XiUIMm7MvvH9CMEG6ALcmhvNwk+O1mLKKvYR9GnL420d5VOh 8e9jwKiUy8C8shBZoQMho6iAFvbMbhM8CAjEb3Y1FWQmpn9otLrMmvIDm6NVV/QnQ+Er Hn3qWPrr//dP11oZ9IG4grRZVFTXRDckMe1zjf4C04M/aTA6hh6MVuHb0xTm2wi6BJRS beIpkMhGjNj67jZR+MOUTuqNr6hGYPHgDsNQHcnHx4PTDESJG0UUfEOrHwptFIlvyibf m+Ww== 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=7hrVqwDEsCy4jvGx2ygf2ufkTVUbjR+/cx/GyTSeOVg=; b=WQwTT2mm4LbTwu9N9n1698EGjcBC82TAsAdIwUiyETVJQgpaYOaKsLu6OdQE3ypdUN Cav6w5A2dz3tMaHaIkCMxdiNtkJR4BoBeI/v4fQct3LhdBkR6n9iI1IAHg26k5k+U3aR c87LmYb0hC0L4Yvos7Ivm5xxRHPQmGO8zbZ82oYPkLv32NP/QJHk5YyHNlnnLdTRpfze 71PHZ5272gX0Bi2YYvp9eKtLRLqRbXZxT8eLT2vANaxkJvc1o3g4fgLzM0TdFIO5J/eq WX/AUQybVgtWRjUfjSPGXDjrXYlbxvIfj7dxjvqIayFmjTBx0h9YcykbqjJMsvfCqIXy nyRw== X-Gm-Message-State: AOAM5321jyg+AmLVp71fdlT8fHLBYkXUDDC6nBoS1WlJD5CmsJ0186kP 3qsjvJhvWdmKRcJMzoixP/6DBVWQx/k= X-Google-Smtp-Source: ABdhPJzOueV2pnWeUZbXwxL7IYD1/yTcaIMqbT5FuJ7ElezulT5OoadMIALZU6XgkxY9MygHS9ICPg== X-Received: by 2002:a7b:cd93:: with SMTP id y19mr6286829wmj.190.1639482483763; Tue, 14 Dec 2021 03:48:03 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p12sm13618140wro.33.2021.12.14.03.48.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:48:03 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:53 +0000 Subject: [PATCH v4 08/11] 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 , 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 Tue Dec 14 11:47:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675875 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 6FAB0C433F5 for ; Tue, 14 Dec 2021 11:48:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233756AbhLNLsY (ORCPT ); Tue, 14 Dec 2021 06:48:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233730AbhLNLsG (ORCPT ); Tue, 14 Dec 2021 06:48:06 -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 C51DCC061574 for ; Tue, 14 Dec 2021 03:48:05 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id a9so31906778wrr.8 for ; Tue, 14 Dec 2021 03:48:05 -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=S929WtbLycx8tthlebgwee0eZnsSzF55QNgcBtMV46g=; b=EoDThHChSYvPMgX79SPENeAkflTOVNi5l7NBiGxXem6P+WCcSt8G0VrEVII0O+RDs0 iGSHoKCxfNCLATRwUJf58786cPFPT9MF83A/zZaPX5bug3UNKi+aIumguWI2PZLklUe6 1uXKMBAcRx1TT92vKsLoKW+i0etN5EpEiGcY6I/FWB42Qdipakcq7yWmFpJzdfKfYcbV b/vmyoTokjJEe3puadQrt4wGAeZwKY38araH6vfzOacAYIRkJgiOLesAGAd/Kv2QGaBi xmfY2bzW6LYa9KXwRJrFTMnUW3MYbpyfYphzD/Jqy+I2IoGoZvQ2H7o23J2V0u909Os3 7vjw== 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=S929WtbLycx8tthlebgwee0eZnsSzF55QNgcBtMV46g=; b=vsmDcjYKolTbCkWD+ioI+vNJiE2MNq5FYYfciL588UiIzIrHpJvquYfbmkg6mcwNVo AkEWBMEvjvPszPHTQR9u+Hpy3bRyiyQbzcBOpXrewR9mpEAetRlI6V6jAifKSf8v8coV xg67NI6SC6kDxr/m0d6Yc5ZL9UHzvrntCuMjMeIpQ9p/zp8cB4nHgA2hh5vBc1eFKkQJ fwskOXWD+yg2c6xNacvGyB8bl8eY66J2qzOBWHNtqYuih2mH5Bbe+uJFoALtc7gpAWRo Kr00KYdGUYukLZwVlPi4QAxQn4bHqA0hwOWytlk6wC3V3YoflwHZm5C8FnNKxc1FF/GY sNFg== X-Gm-Message-State: AOAM533XQbcRrkEemxNObl5YOSMvc7WunowtsKRN5dKHbA8QGs2qOd5c lFE1Ta5/35HFbMVdz+PdXsbYyI6mg+M= X-Google-Smtp-Source: ABdhPJxmp9OrgTQZyNZAgMB0K/rq5qhOZAC3rdtA6YNtxvviHOy6Pjgcy0Y3ai1gIdVYGcObJGRBYA== X-Received: by 2002:a5d:5251:: with SMTP id k17mr5499773wrc.482.1639482484356; Tue, 14 Dec 2021 03:48:04 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w17sm14037345wrp.79.2021.12.14.03.48.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:48:04 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:54 +0000 Subject: [PATCH v4 09/11] reftable: drop stray printf in readwrite_test Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 6e88182a83a..9b89a5da103 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -662,7 +662,6 @@ static void test_write_key_order(void) err = reftable_writer_add_ref(w, &refs[0]); EXPECT_ERR(err); err = reftable_writer_add_ref(w, &refs[1]); - printf("%d\n", err); EXPECT(err == REFTABLE_API_ERROR); reftable_writer_close(w); reftable_writer_free(w); From patchwork Tue Dec 14 11:47:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675877 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 39928C433EF for ; Tue, 14 Dec 2021 11:48:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233760AbhLNLs1 (ORCPT ); Tue, 14 Dec 2021 06:48:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233751AbhLNLsP (ORCPT ); Tue, 14 Dec 2021 06:48:15 -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 77763C0617A1 for ; Tue, 14 Dec 2021 03:48:06 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id d9so31953435wrw.4 for ; Tue, 14 Dec 2021 03:48:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=IY/CPJQ0Q+sLVf7jURIMds3wahY4HaQuVDcXfMmDpdE=; b=eim8Y6qiA18pYhLHwfQn3lVUcDx5a9AmkdPdE0Bnjh0yZIjtgc8ErGEQMeIfSNNcuY 50aKvkEB3ZuGU55D8tqaS7SnCBqo9ktjobpHrVP1EmwoAyc/oA0xvL0YMgNnYRsnrdF4 XqpJbjoxbwI2z86S7hFt/awdSCB2e9AWKAt+DpS36xp3EaCc0benWkfFlOMFmLH8MRN8 IEVXcsPxRDOGJwpiUx26nwpXJNYqXGFql3HqkAfU3fh2qDGGirz35LY/OhQgXUyfmBAD fD7xjMt8eY2/h8E0LdOPikpC4Lb5oYky/XndJcn7cWivk+nfUFFzUnZkbiAz1nnXdUSF GF+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=IY/CPJQ0Q+sLVf7jURIMds3wahY4HaQuVDcXfMmDpdE=; b=FzdtriX9jR38t2dSvt1ApWH+opJ/aW4A4/3P9dRq7oPmMzu6USRSqNdWgCYcdAeAqP 0H+ZH6tP0pJDu7bVNR7iLtR+PG/IdF47onOuoM1z7Pwb5mN0F6i3EWuUVEjUxtvZagGL Rvgb4pH5+o0jjnIo1dgrEe8WQgCp1dPLl7zmHsh6BWv7Ua+JrbWikNByxLh73Ls5D8Gm Dw7WDMzYfwtq+oNaUntQYCc8UmsdV7FKwNCjHe4cqBM7VUXw/0DmdGEd830dfRHndG6B cg8+Orb7VKjaSTGCgho5NKhFZ1glr7Q/b9Q2AHTCxIYm7waSpCVOHHVCWUUjd/BoGT/c N6ww== X-Gm-Message-State: AOAM531GI2HAfa6VVFEkyzvfaEL2EjCF8dG1DjeGa5YVYaQ53z+nL/eD uqxjmFG8AjA4yxDJ7890ma/wFnUBE2Q= X-Google-Smtp-Source: ABdhPJz8mhI1+Tx4qDQtHoikkBrtzm3dlgC1yFoyR2Prz44OpWfbKSfCIZbIut5tQF6DzZqOwDwK7Q== X-Received: by 2002:a05:6000:1809:: with SMTP id m9mr5147579wrh.678.1639482485010; Tue, 14 Dec 2021 03:48:05 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n1sm1947179wmq.6.2021.12.14.03.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:48:04 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:55 +0000 Subject: [PATCH v4 10/11] reftable: handle null refnames in reftable_ref_record_equal Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Spotted by Coverity. Signed-off-by: Han-Wen Nienhuys --- reftable/record.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 8536bd03aa9..8bbcbff1e69 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -1154,9 +1154,11 @@ int reftable_ref_record_equal(struct reftable_ref_record *a, struct reftable_ref_record *b, int hash_size) { assert(hash_size > 0); - if (!(0 == strcmp(a->refname, b->refname) && - a->update_index == b->update_index && - a->value_type == b->value_type)) + if (!null_streq(a->refname, b->refname)) + return 0; + + if (a->update_index != b->update_index || + a->value_type != b->value_type) return 0; switch (a->value_type) { From patchwork Tue Dec 14 11:47:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12675879 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 1764BC433FE for ; Tue, 14 Dec 2021 11:48:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233691AbhLNLs3 (ORCPT ); Tue, 14 Dec 2021 06:48:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233753AbhLNLsQ (ORCPT ); Tue, 14 Dec 2021 06:48:16 -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 DD25FC061372 for ; Tue, 14 Dec 2021 03:48:07 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id j3so31964598wrp.1 for ; Tue, 14 Dec 2021 03:48:07 -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=iuqBYGcFfZCu5WZBgO0eRvTvNNGVWCYTKVCN9GpPMqs=; b=Wokg9CU+w8CTyWbC9Sa65M4Xb3JjQ7SkfxRln5Ihawt5/yxmxd2uon7R0sCjCbPMSq Of+Bnult6AT+hV4wKmgybrtGLZ8unRmHMvjDcXpOPJPGZh97/CezWDnIBYepbIJeo9H4 vaYyN+vrJZCTqjhY/l+99jBKcXexCte/1YdOx5seT2hUpmF9EgcWWEwAs5dvpXi5mW4p fNHXKp4R3oCPFer0SA9/Jv4Fw2TG9DV5uGcq6J5OXMYG1cdLiBMe7KtL4XZL3d54+hv4 icK8bUUHC+yVdBuKEFmEK2g73xmX4+2ZSpTsfksIyfUEGSJ2uVRyhRRwwf8QUMxlEZh0 qC6Q== 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=iuqBYGcFfZCu5WZBgO0eRvTvNNGVWCYTKVCN9GpPMqs=; b=wfxSSxKttZmZKXsfY6aLz24NTULBcVkTUs4qpA31By/D5S9K6qsNm5GkKcPctIBoL4 bWsNDmyyY7A84hKUGe+f/LlQ3qtspILgnHxfyLYJpBECJpxQT2Z198S8AvFbEqtblH6f WcBYc5LcPhqLkX6EekM+JLdCKyMfpVgVZVFJK7870giuUZ8yEgUC8f47f/vdl2m/OonI 09Bfm7AE6Wq1h3numdJkmrmGmIX089lqKZukawFbuwqBkheoQ7Zcs3RSXPVgdQbftoFv f772yHo4K1cbApA44KDGh7KZ0+zwVU2FbShIuMpwVBd2jlwI5CxEqSCS8Kpqj1qXfFwX wlTA== X-Gm-Message-State: AOAM5313aZg4285C4saIW2F6VLWlgYsv5srHDYvFNia2qqNOoWFldMsc WcKaku4yNotrTeNNu9vBt/WNyxVMykc= X-Google-Smtp-Source: ABdhPJwj5yYEsmM9omNm8rS75FtpErqBhAKyCE5qHjGsLD0Md6oxv81SrnYX7I7VZbK+6pWGkQ1oSQ== X-Received: by 2002:adf:f489:: with SMTP id l9mr5188217wro.268.1639482485716; Tue, 14 Dec 2021 03:48:05 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m3sm13708974wrv.95.2021.12.14.03.48.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Dec 2021 03:48:05 -0800 (PST) Message-Id: <0ffc1cf0e429510d81ad91c6d052e0bd38264f28.1639482477.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 14 Dec 2021 11:47:56 +0000 Subject: [PATCH v4 11/11] reftable: make reftable_record a tagged union Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 | 35 ++++---- reftable/iter.c | 4 +- reftable/merged.c | 33 ++++---- reftable/pq.c | 3 +- reftable/pq_test.c | 31 +++---- reftable/reader.c | 93 +++++++++++---------- reftable/record.c | 173 ++++++++++++++++----------------------- reftable/record.h | 42 ++++------ reftable/record_test.c | 181 +++++++++++++++++++++-------------------- reftable/writer.c | 40 +++++---- 12 files changed, 317 insertions(+), 342 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 6c8e8705205..2170748c5e9 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -421,7 +421,7 @@ int block_reader_seek(struct block_reader *br, struct block_iter *it, done: strbuf_release(&key); strbuf_release(&next.last_key); - reftable_record_destroy(&rec); + reftable_record_release(&rec); return err; } diff --git a/reftable/block_test.c b/reftable/block_test.c index 4b3ea262dcb..fa2ee092ec0 100644 --- a/reftable/block_test.c +++ b/reftable/block_test.c @@ -26,8 +26,9 @@ static void test_block_read_write(void) struct block_writer bw = { .last_key = STRBUF_INIT, }; - struct reftable_ref_record ref = { NULL }; - struct reftable_record rec = { NULL }; + struct reftable_record rec = { + .type = BLOCK_TYPE_REF, + }; int i = 0; int n; struct block_reader br = { 0 }; @@ -40,7 +41,6 @@ static void test_block_read_write(void) block.source = malloc_block_source(); block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size, header_off, hash_size(GIT_SHA1_FORMAT_ID)); - reftable_record_from_ref(&rec, &ref); for (i = 0; i < N; i++) { char name[100]; @@ -48,14 +48,14 @@ static void test_block_read_write(void) snprintf(name, sizeof(name), "branch%02d", i); memset(hash, i, sizeof(hash)); - ref.refname = name; - ref.value_type = REFTABLE_REF_VAL1; - ref.value.val1 = hash; + rec.u.ref.refname = name; + rec.u.ref.value_type = REFTABLE_REF_VAL1; + rec.u.ref.value.val1 = hash; names[i] = xstrdup(name); n = block_writer_add(&bw, &rec); - ref.refname = NULL; - ref.value_type = REFTABLE_REF_DELETION; + rec.u.ref.refname = NULL; + rec.u.ref.value_type = REFTABLE_REF_DELETION; EXPECT(n == 0); } @@ -74,7 +74,7 @@ static void test_block_read_write(void) if (r > 0) { break; } - EXPECT_STREQ(names[j], ref.refname); + EXPECT_STREQ(names[j], rec.u.ref.refname); j++; } @@ -92,7 +92,7 @@ static void test_block_read_write(void) n = block_iter_next(&it, &rec); EXPECT(n == 0); - EXPECT_STREQ(names[i], ref.refname); + EXPECT_STREQ(names[i], rec.u.ref.refname); want.len--; n = block_reader_seek(&br, &it, &want); @@ -100,7 +100,7 @@ static void test_block_read_write(void) n = block_iter_next(&it, &rec); EXPECT(n == 0); - EXPECT_STREQ(names[10 * (i / 10)], ref.refname); + EXPECT_STREQ(names[10 * (i / 10)], rec.u.ref.refname); block_iter_close(&it); } diff --git a/reftable/generic.c b/reftable/generic.c index 7a8a738d860..4446b8ed360 100644 --- a/reftable/generic.c +++ b/reftable/generic.c @@ -7,6 +7,7 @@ https://developers.google.com/open-source/licenses/bsd */ #include "basics.h" +#include "constants.h" #include "record.h" #include "generic.h" #include "reftable-iterator.h" @@ -15,23 +16,21 @@ https://developers.google.com/open-source/licenses/bsd int reftable_table_seek_ref(struct reftable_table *tab, struct reftable_iterator *it, const char *name) { - struct reftable_ref_record ref = { - .refname = (char *)name, - }; - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, &ref); + struct reftable_record rec = { .type = BLOCK_TYPE_REF, + .u.ref = { + .refname = (char *)name, + } }; return tab->ops->seek_record(tab->table_arg, it, &rec); } int reftable_table_seek_log(struct reftable_table *tab, struct reftable_iterator *it, const char *name) { - struct reftable_log_record log = { - .refname = (char *)name, - .update_index = ~((uint64_t)0), - }; - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, &log); + struct reftable_record rec = { .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = (char *)name, + .update_index = ~((uint64_t)0), + } }; return tab->ops->seek_record(tab->table_arg, it, &rec); } @@ -129,17 +128,19 @@ void reftable_iterator_destroy(struct reftable_iterator *it) int reftable_iterator_next_ref(struct reftable_iterator *it, struct reftable_ref_record *ref) { - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, ref); - return iterator_next(it, &rec); + struct reftable_record rec = { .type = BLOCK_TYPE_REF }; + int err = iterator_next(it, &rec); + *ref = rec.u.ref; + return err; } int reftable_iterator_next_log(struct reftable_iterator *it, struct reftable_log_record *log) { - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, log); - return iterator_next(it, &rec); + struct reftable_record rec = { .type = BLOCK_TYPE_LOG }; + int err = iterator_next(it, &rec); + *log = rec.u.log; + return err; } int iterator_next(struct reftable_iterator *it, struct reftable_record *rec) diff --git a/reftable/iter.c b/reftable/iter.c index 93d04f735b8..a8d174c0406 100644 --- a/reftable/iter.c +++ b/reftable/iter.c @@ -32,7 +32,7 @@ static int filtering_ref_iterator_next(void *iter_arg, struct reftable_record *rec) { struct filtering_ref_iterator *fri = iter_arg; - struct reftable_ref_record *ref = rec->data; + struct reftable_ref_record *ref = &rec->u.ref; int err = 0; while (1) { err = reftable_iterator_next_ref(&fri->it, ref); @@ -127,7 +127,7 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it) static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec) { struct indexed_table_ref_iter *it = p; - struct reftable_ref_record *ref = rec->data; + struct reftable_ref_record *ref = &rec->u.ref; while (1) { int err = block_iter_next(&it->cur, rec); diff --git a/reftable/merged.c b/reftable/merged.c index e5b53da6db3..a7c0df91ff0 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,12 @@ int reftable_merged_table_seek_ref(struct reftable_merged_table *mt, struct reftable_iterator *it, const char *name) { - struct reftable_ref_record ref = { - .refname = (char *)name, + struct reftable_record rec = { + .type = BLOCK_TYPE_REF, + .u.ref = { + .refname = (char *)name, + }, }; - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, &ref); return merged_table_seek_record(mt, it, &rec); } @@ -302,12 +302,13 @@ int reftable_merged_table_seek_log_at(struct reftable_merged_table *mt, struct reftable_iterator *it, const char *name, uint64_t update_index) { - struct reftable_log_record log = { - .refname = (char *)name, - .update_index = update_index, + struct reftable_record rec = { + .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = (char *)name, + .update_index = update_index, + } }; - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, &log); return merged_table_seek_record(mt, it, &rec); } diff --git a/reftable/pq.c b/reftable/pq.c index efc474017a2..96ca6dd37b3 100644 --- a/reftable/pq.c +++ b/reftable/pq.c @@ -74,6 +74,7 @@ struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq) void merged_iter_pqueue_add(struct merged_iter_pqueue *pq, struct pq_entry e) { int i = 0; + if (pq->len == pq->cap) { pq->cap = 2 * pq->cap + 1; pq->heap = reftable_realloc(pq->heap, @@ -98,7 +99,7 @@ void merged_iter_pqueue_release(struct merged_iter_pqueue *pq) { int i = 0; for (i = 0; i < pq->len; i++) { - reftable_record_destroy(&pq->heap[i].rec); + reftable_record_release(&pq->heap[i].rec); } FREE_AND_NULL(pq->heap); pq->len = pq->cap = 0; diff --git a/reftable/pq_test.c b/reftable/pq_test.c index c9bb05e37b7..e0f5c584ada 100644 --- a/reftable/pq_test.c +++ b/reftable/pq_test.c @@ -31,7 +31,7 @@ static void test_pq(void) int N = ARRAY_SIZE(names) - 1; struct merged_iter_pqueue pq = { NULL }; - const char *last = NULL; + char *last = NULL; int i = 0; for (i = 0; i < N; i++) { @@ -42,12 +42,14 @@ static void test_pq(void) i = 1; do { - struct reftable_record rec = - reftable_new_record(BLOCK_TYPE_REF); - struct pq_entry e = { 0 }; - - reftable_record_as_ref(&rec)->refname = names[i]; - e.rec = rec; + struct pq_entry e = { + .rec = { + .type = BLOCK_TYPE_REF, + .u.ref = { + .refname = names[i], + } + } + }; merged_iter_pqueue_add(&pq, e); merged_iter_pqueue_check(pq); i = (i * 7) % N; @@ -55,19 +57,18 @@ static void test_pq(void) while (!merged_iter_pqueue_is_empty(pq)) { struct pq_entry e = merged_iter_pqueue_remove(&pq); - struct reftable_ref_record *ref = - reftable_record_as_ref(&e.rec); - + struct reftable_record *rec = &e.rec; merged_iter_pqueue_check(pq); + EXPECT(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 733509606a9..0790f654366 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; @@ -437,8 +436,7 @@ static int reader_start(struct reftable_reader *r, struct table_iter *ti, static int reader_seek_linear(struct reftable_reader *r, struct table_iter *ti, struct reftable_record *want) { - struct reftable_record rec = - reftable_new_record(reftable_record_type(want)); + struct reftable_record rec = reftable_new_record(reftable_record_type(want)); struct strbuf want_key = STRBUF_INIT; struct strbuf got_key = STRBUF_INIT; struct table_iter next = TABLE_ITER_INIT; @@ -475,7 +473,7 @@ static int reader_seek_linear(struct reftable_reader *r, struct table_iter *ti, done: block_iter_close(&next.bi); - reftable_record_destroy(&rec); + reftable_record_release(&rec); strbuf_release(&want_key); strbuf_release(&got_key); return err; @@ -485,34 +483,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; @@ -540,8 +540,8 @@ static int reader_seek_indexed(struct reftable_reader *r, done: block_iter_close(&next.bi); table_iter_close(&index_iter); - reftable_record_release(&want_index_rec); - reftable_record_release(&index_result_rec); + reftable_record_release(&want_index); + reftable_record_release(&index_result); return err; } @@ -549,8 +549,7 @@ static int reader_seek_internal(struct reftable_reader *r, struct reftable_iterator *it, struct reftable_record *rec) { - struct reftable_reader_offsets *offs = - reader_offsets_for(r, reftable_record_type(rec)); + struct reftable_reader_offsets *offs = reader_offsets_for(r, reftable_record_type(rec)); uint64_t idx = offs->index_offset; struct table_iter ti = TABLE_ITER_INIT; int err = 0; @@ -590,11 +589,12 @@ static int reader_seek(struct reftable_reader *r, struct reftable_iterator *it, int reftable_reader_seek_ref(struct reftable_reader *r, struct reftable_iterator *it, const char *name) { - struct reftable_ref_record ref = { - .refname = (char *)name, + struct reftable_record rec = { + .type = BLOCK_TYPE_REF, + .u.ref = { + .refname = (char *)name, + }, }; - struct reftable_record rec = { NULL }; - reftable_record_from_ref(&rec, &ref); return reader_seek(r, it, &rec); } @@ -602,12 +602,13 @@ int reftable_reader_seek_log_at(struct reftable_reader *r, struct reftable_iterator *it, const char *name, uint64_t update_index) { - struct reftable_log_record log = { - .refname = (char *)name, - .update_index = update_index, + struct reftable_record rec = { + .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = (char *)name, + .update_index = update_index, + } }; - struct reftable_record rec = { NULL }; - reftable_record_from_log(&rec, &log); return reader_seek(r, it, &rec); } @@ -651,31 +652,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; @@ -683,15 +686,15 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r, } err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id), - got.offsets, got.offset_len); + got.u.obj.offsets, got.u.obj.offset_len); if (err < 0) goto done; - got.offsets = NULL; + got.u.obj.offsets = NULL; iterator_from_indexed_table_ref_iter(it, itr); done: reftable_iterator_destroy(&oit); - reftable_record_release(&got_rec); + reftable_record_release(&got); return err; } diff --git a/reftable/record.c b/reftable/record.c index 8bbcbff1e69..f766ee20ace 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -15,6 +15,10 @@ https://developers.google.com/open-source/licenses/bsd #include "reftable-error.h" #include "basics.h" +static struct reftable_record_vtable * +reftable_record_vtable(struct reftable_record *rec); +static void *reftable_record_data(struct reftable_record *rec); + int get_var_int(uint64_t *dest, struct string_view *in) { int ptr = 0; @@ -926,58 +930,6 @@ static struct reftable_record_vtable reftable_log_record_vtable = { .is_deletion = &reftable_log_record_is_deletion_void, }; -struct reftable_record reftable_new_record(uint8_t typ) -{ - struct reftable_record rec = { NULL }; - switch (typ) { - case BLOCK_TYPE_REF: { - struct reftable_ref_record *r = - reftable_calloc(sizeof(struct reftable_ref_record)); - reftable_record_from_ref(&rec, r); - return rec; - } - - case BLOCK_TYPE_OBJ: { - struct reftable_obj_record *r = - reftable_calloc(sizeof(struct reftable_obj_record)); - reftable_record_from_obj(&rec, r); - return rec; - } - case BLOCK_TYPE_LOG: { - struct reftable_log_record *r = - reftable_calloc(sizeof(struct reftable_log_record)); - reftable_record_from_log(&rec, r); - return rec; - } - case BLOCK_TYPE_INDEX: { - struct reftable_index_record empty = { .last_key = - STRBUF_INIT }; - struct reftable_index_record *r = - reftable_calloc(sizeof(struct reftable_index_record)); - *r = empty; - reftable_record_from_index(&rec, r); - return rec; - } - } - abort(); - return rec; -} - -/* clear out the record, yielding the reftable_record data that was - * encapsulated. */ -static void *reftable_record_yield(struct reftable_record *rec) -{ - void *p = rec->data; - rec->data = NULL; - return p; -} - -void reftable_record_destroy(struct reftable_record *rec) -{ - reftable_record_release(rec); - reftable_free(reftable_record_yield(rec)); -} - static void reftable_index_record_key(const void *r, struct strbuf *dest) { const struct reftable_index_record *rec = r; @@ -1055,91 +1007,52 @@ 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); -} - -void reftable_record_from_ref(struct reftable_record *rec, - struct reftable_ref_record *ref_rec) -{ - assert(!rec->ops); - rec->data = ref_rec; - rec->ops = &reftable_ref_record_vtable; -} - -void reftable_record_from_obj(struct reftable_record *rec, - struct reftable_obj_record *obj_rec) -{ - assert(!rec->ops); - rec->data = obj_rec; - rec->ops = &reftable_obj_record_vtable; -} - -void reftable_record_from_index(struct reftable_record *rec, - struct reftable_index_record *index_rec) -{ - assert(!rec->ops); - rec->data = index_rec; - rec->ops = &reftable_index_record_vtable; -} - -void reftable_record_from_log(struct reftable_record *rec, - struct reftable_log_record *log_rec) -{ - assert(!rec->ops); - rec->data = log_rec; - rec->ops = &reftable_log_record_vtable; -} - -struct reftable_ref_record *reftable_record_as_ref(struct reftable_record *rec) -{ - assert(reftable_record_type(rec) == BLOCK_TYPE_REF); - return rec->data; -} - -struct reftable_log_record *reftable_record_as_log(struct reftable_record *rec) -{ - assert(reftable_record_type(rec) == BLOCK_TYPE_LOG); - return rec->data; + return reftable_record_vtable(rec)->is_deletion( + reftable_record_data(rec)); } static int hash_equal(uint8_t *a, uint8_t *b, int hash_size) @@ -1212,3 +1125,53 @@ void string_view_consume(struct string_view *s, int n) s->buf += n; s->len -= n; } + +static void *reftable_record_data(struct reftable_record *rec) +{ + switch (rec->type) { + case BLOCK_TYPE_REF: + return &rec->u.ref; + case BLOCK_TYPE_LOG: + return &rec->u.log; + case BLOCK_TYPE_INDEX: + return &rec->u.idx; + case BLOCK_TYPE_OBJ: + return &rec->u.obj; + } + abort(); +} + +static struct reftable_record_vtable * +reftable_record_vtable(struct reftable_record *rec) +{ + switch (rec->type) { + case BLOCK_TYPE_REF: + return &reftable_ref_record_vtable; + case BLOCK_TYPE_LOG: + return &reftable_log_record_vtable; + case BLOCK_TYPE_INDEX: + return &reftable_index_record_vtable; + case BLOCK_TYPE_OBJ: + return &reftable_obj_record_vtable; + } + abort(); +} + +struct reftable_record reftable_new_record(uint8_t typ) +{ + struct reftable_record clean_idx = { + .type = BLOCK_TYPE_INDEX, + .u.idx = { + .last_key = STRBUF_INIT, + }, + }; + struct reftable_record clean = { + .type = typ, + }; + + if (typ == BLOCK_TYPE_INDEX) { + return clean_idx; + } + + return clean; +} diff --git a/reftable/record.h b/reftable/record.h index 498e8c50bf4..407cdaba018 100644 --- a/reftable/record.h +++ b/reftable/record.h @@ -60,16 +60,10 @@ struct reftable_record_vtable { int (*is_deletion)(const void *rec); }; -/* record is a generic wrapper for different types of records. */ -struct reftable_record { - void *data; - struct reftable_record_vtable *ops; -}; - /* returns true for recognized block types. Block start with the block type. */ int reftable_is_block_type(uint8_t typ); -/* creates a malloced record of the given type. Dispose with record_destroy */ +/* 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 @@ -97,6 +91,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 */ void reftable_record_key(struct reftable_record *rec, struct strbuf *dest); @@ -111,25 +121,9 @@ int reftable_record_decode(struct reftable_record *rec, struct strbuf key, int hash_size); int reftable_record_is_deletion(struct reftable_record *rec); -/* zeroes out the embedded record */ +/* frees and zeroes out the embedded record */ void reftable_record_release(struct reftable_record *rec); -/* clear and deallocate embedded record, and zero `rec`. */ -void reftable_record_destroy(struct reftable_record *rec); - -/* initialize generic records from concrete records. The generic record should - * be zeroed out. */ -void reftable_record_from_obj(struct reftable_record *rec, - struct reftable_obj_record *objrec); -void reftable_record_from_index(struct reftable_record *rec, - struct reftable_index_record *idxrec); -void reftable_record_from_ref(struct reftable_record *rec, - struct reftable_ref_record *refrec); -void reftable_record_from_log(struct reftable_record *rec, - struct reftable_log_record *logrec); -struct reftable_ref_record *reftable_record_as_ref(struct reftable_record *ref); -struct reftable_log_record *reftable_record_as_log(struct reftable_record *ref); - /* for qsort. */ int reftable_ref_record_compare_name(const void *a, const void *b); diff --git a/reftable/record_test.c b/reftable/record_test.c index f4ad7cace41..96f65035c1b 100644 --- a/reftable/record_test.c +++ b/reftable/record_test.c @@ -16,24 +16,22 @@ static void test_copy(struct reftable_record *rec) { - struct reftable_record copy = - reftable_new_record(reftable_record_type(rec)); + struct reftable_record copy = reftable_new_record(reftable_record_type(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(©)) { + switch (copy.type) { case BLOCK_TYPE_REF: - EXPECT(reftable_ref_record_equal(reftable_record_as_ref(©), - reftable_record_as_ref(rec), + EXPECT(reftable_ref_record_equal(©.u.ref, &rec->u.ref, GIT_SHA1_RAWSZ)); break; case BLOCK_TYPE_LOG: - EXPECT(reftable_log_record_equal(reftable_record_as_log(©), - reftable_record_as_log(rec), + EXPECT(reftable_log_record_equal(©.u.log, &rec->u.log, GIT_SHA1_RAWSZ)); break; } - reftable_record_destroy(©); + reftable_record_release(©); } static void test_varint_roundtrip(void) @@ -106,61 +104,60 @@ static void test_reftable_ref_record_roundtrip(void) int i = 0; for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) { - struct reftable_ref_record in = { NULL }; - struct reftable_ref_record out = { NULL }; - struct reftable_record rec_out = { NULL }; + struct reftable_record in = { + .type = BLOCK_TYPE_REF, + }; + struct reftable_record out = { + .type = BLOCK_TYPE_REF + }; struct strbuf key = STRBUF_INIT; - struct reftable_record rec = { NULL }; uint8_t buffer[1024] = { 0 }; struct string_view dest = { .buf = buffer, .len = sizeof(buffer), }; - int n, m; - in.value_type = i; + in.u.ref.value_type = i; switch (i) { case REFTABLE_REF_DELETION: break; case REFTABLE_REF_VAL1: - in.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ); - set_hash(in.value.val1, 1); + in.u.ref.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ); + set_hash(in.u.ref.value.val1, 1); break; case REFTABLE_REF_VAL2: - in.value.val2.value = reftable_malloc(GIT_SHA1_RAWSZ); - set_hash(in.value.val2.value, 1); - in.value.val2.target_value = + in.u.ref.value.val2.value = + reftable_malloc(GIT_SHA1_RAWSZ); + set_hash(in.u.ref.value.val2.value, 1); + in.u.ref.value.val2.target_value = reftable_malloc(GIT_SHA1_RAWSZ); - set_hash(in.value.val2.target_value, 2); + set_hash(in.u.ref.value.val2.target_value, 2); break; case REFTABLE_REF_SYMREF: - in.value.symref = xstrdup("target"); + in.u.ref.value.symref = xstrdup("target"); break; } - in.refname = xstrdup("refs/heads/master"); + in.u.ref.refname = xstrdup("refs/heads/master"); - reftable_record_from_ref(&rec, &in); - test_copy(&rec); + test_copy(&in); - EXPECT(reftable_record_val_type(&rec) == i); + EXPECT(reftable_record_val_type(&in) == i); - reftable_record_key(&rec, &key); - n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); + reftable_record_key(&in, &key); + n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); EXPECT(n > 0); /* decode into a non-zero reftable_record to test for leaks. */ - - reftable_record_from_ref(&rec_out, &out); - m = reftable_record_decode(&rec_out, key, i, dest, - GIT_SHA1_RAWSZ); + m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ); EXPECT(n == m); - EXPECT(reftable_ref_record_equal(&in, &out, GIT_SHA1_RAWSZ)); - reftable_record_release(&rec_out); + EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref, + GIT_SHA1_RAWSZ)); + reftable_record_release(&in); strbuf_release(&key); - reftable_ref_record_release(&in); + reftable_record_release(&out); } } @@ -213,7 +210,9 @@ static void test_reftable_log_record_roundtrip(void) set_test_hash(in[0].value.update.new_hash, 1); set_test_hash(in[0].value.update.old_hash, 2); for (i = 0; i < ARRAY_SIZE(in); i++) { - struct reftable_record rec = { NULL }; + struct reftable_record rec = { + .type = BLOCK_TYPE_LOG + }; struct strbuf key = STRBUF_INIT; uint8_t buffer[1024] = { 0 }; struct string_view dest = { @@ -221,23 +220,25 @@ static void test_reftable_log_record_roundtrip(void) .len = sizeof(buffer), }; /* populate out, to check for leaks. */ - struct reftable_log_record out = { - .refname = xstrdup("old name"), - .value_type = REFTABLE_LOG_UPDATE, - .value = { - .update = { - .new_hash = reftable_calloc(GIT_SHA1_RAWSZ), - .old_hash = reftable_calloc(GIT_SHA1_RAWSZ), - .name = xstrdup("old name"), - .email = xstrdup("old@email"), - .message = xstrdup("old message"), + struct reftable_record out = { + .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = xstrdup("old name"), + .value_type = REFTABLE_LOG_UPDATE, + .value = { + .update = { + .new_hash = reftable_calloc(GIT_SHA1_RAWSZ), + .old_hash = reftable_calloc(GIT_SHA1_RAWSZ), + .name = xstrdup("old name"), + .email = xstrdup("old@email"), + .message = xstrdup("old message"), + }, }, }, }; - struct reftable_record rec_out = { NULL }; int n, m, valtype; - reftable_record_from_log(&rec, &in[i]); + rec.u.log = in[i]; test_copy(&rec); @@ -245,16 +246,16 @@ static void test_reftable_log_record_roundtrip(void) n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); EXPECT(n >= 0); - reftable_record_from_log(&rec_out, &out); valtype = reftable_record_val_type(&rec); - m = reftable_record_decode(&rec_out, key, valtype, dest, + m = reftable_record_decode(&out, key, valtype, dest, GIT_SHA1_RAWSZ); EXPECT(n == m); - EXPECT(reftable_log_record_equal(&in[i], &out, GIT_SHA1_RAWSZ)); + EXPECT(reftable_log_record_equal(&in[i], &out.u.log, + GIT_SHA1_RAWSZ)); reftable_log_record_release(&in[i]); strbuf_release(&key); - reftable_record_release(&rec_out); + reftable_record_release(&out); } } @@ -322,47 +323,52 @@ static void test_reftable_obj_record_roundtrip(void) } }; int i = 0; for (i = 0; i < ARRAY_SIZE(recs); i++) { - struct reftable_obj_record in = recs[i]; uint8_t buffer[1024] = { 0 }; struct string_view dest = { .buf = buffer, .len = sizeof(buffer), }; - struct reftable_record rec = { NULL }; + struct reftable_record in = { + .type = BLOCK_TYPE_OBJ, + .u.obj = recs[i], + }; struct strbuf key = STRBUF_INIT; - struct reftable_obj_record out = { NULL }; - struct reftable_record rec_out = { NULL }; + struct reftable_record out = { + .type = BLOCK_TYPE_OBJ + }; int n, m; uint8_t extra; - reftable_record_from_obj(&rec, &in); - test_copy(&rec); - reftable_record_key(&rec, &key); - n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); + test_copy(&in); + reftable_record_key(&in, &key); + n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); EXPECT(n > 0); - extra = reftable_record_val_type(&rec); - reftable_record_from_obj(&rec_out, &out); - m = reftable_record_decode(&rec_out, key, extra, dest, + extra = reftable_record_val_type(&in); + m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ); EXPECT(n == m); - EXPECT(in.hash_prefix_len == out.hash_prefix_len); - EXPECT(in.offset_len == out.offset_len); - - EXPECT(!memcmp(in.hash_prefix, out.hash_prefix, - in.hash_prefix_len)); - EXPECT(0 == memcmp(in.offsets, out.offsets, - sizeof(uint64_t) * in.offset_len)); + EXPECT(in.u.obj.hash_prefix_len == out.u.obj.hash_prefix_len); + EXPECT(in.u.obj.offset_len == out.u.obj.offset_len); + if (in.u.obj.hash_prefix_len) + EXPECT(!memcmp(in.u.obj.hash_prefix, out.u.obj.hash_prefix, + in.u.obj.hash_prefix_len)); + if (in.u.obj.offset_len) + EXPECT(!memcmp(in.u.obj.offsets, out.u.obj.offsets, + sizeof(uint64_t) * in.u.obj.offset_len)); strbuf_release(&key); - reftable_record_release(&rec_out); + reftable_record_release(&out); } } static void test_reftable_index_record_roundtrip(void) { - struct reftable_index_record in = { - .offset = 42, - .last_key = STRBUF_INIT, + struct reftable_record in = { + .type = BLOCK_TYPE_INDEX, + .u.idx = { + .offset = 42, + .last_key = STRBUF_INIT, + }, }; uint8_t buffer[1024] = { 0 }; struct string_view dest = { @@ -370,31 +376,30 @@ static void test_reftable_index_record_roundtrip(void) .len = sizeof(buffer), }; struct strbuf key = STRBUF_INIT; - struct reftable_record rec = { NULL }; - struct reftable_index_record out = { .last_key = STRBUF_INIT }; - struct reftable_record out_rec = { NULL }; + struct reftable_record out = { + .type = BLOCK_TYPE_INDEX, + .u.idx = { .last_key = STRBUF_INIT }, + }; int n, m; uint8_t extra; - strbuf_addstr(&in.last_key, "refs/heads/master"); - reftable_record_from_index(&rec, &in); - reftable_record_key(&rec, &key); - test_copy(&rec); + strbuf_addstr(&in.u.idx.last_key, "refs/heads/master"); + reftable_record_key(&in, &key); + test_copy(&in); - EXPECT(0 == strbuf_cmp(&key, &in.last_key)); - n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); + EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key)); + n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); EXPECT(n > 0); - extra = reftable_record_val_type(&rec); - reftable_record_from_index(&out_rec, &out); - m = reftable_record_decode(&out_rec, key, extra, dest, GIT_SHA1_RAWSZ); + extra = reftable_record_val_type(&in); + m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ); EXPECT(m == n); - EXPECT(in.offset == out.offset); + EXPECT(in.u.idx.offset == out.u.idx.offset); - reftable_record_release(&out_rec); + reftable_record_release(&out); strbuf_release(&key); - strbuf_release(&in.last_key); + strbuf_release(&in.u.idx.last_key); } int record_test_main(int argc, const char *argv[]) diff --git a/reftable/writer.c b/reftable/writer.c index 9fd24fa93ce..51066c36a61 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -253,8 +253,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) @@ -263,8 +265,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) @@ -303,7 +304,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); @@ -313,8 +317,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); } @@ -395,8 +397,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; } @@ -464,17 +468,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; @@ -487,7 +492,8 @@ static void write_object_record(void *void_arg, void *key) arg->err = block_writer_add(arg->w->block_writer, &rec); if (arg->err == 0) goto done; - obj_rec.offset_len = 0; + + rec.u.obj.offset_len = 0; arg->err = block_writer_add(arg->w->block_writer, &rec); /* Should be able to write into a fresh block. */