From patchwork Thu Jan 20 15:12:00 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: 12718754 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 3CEEAC433FE for ; Thu, 20 Jan 2022 15:12:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346176AbiATPMT (ORCPT ); Thu, 20 Jan 2022 10:12:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233375AbiATPMS (ORCPT ); Thu, 20 Jan 2022 10:12:18 -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 4AC6FC061574 for ; Thu, 20 Jan 2022 07:12:18 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id q141-20020a1ca793000000b00347b48dfb53so14599637wme.0 for ; Thu, 20 Jan 2022 07:12:18 -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=ih9NAlm1rMb0Vzhh4c4q0PUQamAlV3ZmTzG7KjxsaA0ExXF8oiiug4aYKqFteMgW2r YVEngFAqltY2XO1qflfkMnEkmj+z8IMJjxc6XxzavrNQYF8ws0spTBkrM76Va+r0hOFO qmuaJgC2KcRA7Q9ido3ggC2Tmug5F7GasiZUvkcQbXQ6Y1bx/4nuDzjeLzyJ4pjDrouF G/Sr8aaoJ43qwsoXqttm2FTcWlSdifMgH+UV5jnoJJ1lbEiNuy0hJS8AT6eQBPUrWm1u jx7/bKWFqIYwe05wwRfce7a7f/E47WTmFA619bjdPJyhVE//J28SI+4eLvgSvPYCXaW1 Tfrw== 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=schZHj5ExFUESP08EF2GT9BEn5UH6TeXxZTzXa+GKnEhoOToBl3Cg809YjFEZOx8QC z7+1mKEB9uhlNYLRHdjUwyGuCQvO4wR7I4wnECX02d4QsEPhFVBRQWCyAjQfUgImgoge KRvfvD4v9ySPfdJg+Yv1IqO41piVXpFYnJcql17lXtDTCp/oESY9ynUJGCcnHpRzgLwF Wgy05gVZBKRqNfIepZckm8iCLgWKw3RAPJdcX/zBGDl0+H7SxE+E9CT1cAYdu7B7wk3G n06on8F3zT/jXVaISvs0g1eO9g/28rkeqUgp4dDTKTquxyPCs7zlKWj6eTc/vil2Cf+t QFQA== X-Gm-Message-State: AOAM5303sMJ7mUc7+rPPwthoR/Ux1YPuTmdFJBXs29WFi3E7MmgohMhN 7ubC6xFt0UBGpevUQzKMSu0YhBl4AJY= X-Google-Smtp-Source: ABdhPJzK6kwfaRb/jPCRS1J9i6PAXQ8aZhvSn26tAvSsh4NDB1ykUJe+thiy1l3p3tYBqDy096a2bA== X-Received: by 2002:a5d:6d8b:: with SMTP id l11mr24842072wrs.234.1642691536752; Thu, 20 Jan 2022 07:12:16 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t5sm2933428wrw.92.2022.01.20.07.12.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:16 -0800 (PST) Message-Id: <9218bd59b2570ab08f3a2e5c0b590656d00482c4.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:00 +0000 Subject: [PATCH v6 01/15] 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 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 Thu Jan 20 15:12:01 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: 12718755 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 91086C433F5 for ; Thu, 20 Jan 2022 15:12:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376498AbiATPMV (ORCPT ); Thu, 20 Jan 2022 10:12:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346179AbiATPMT (ORCPT ); Thu, 20 Jan 2022 10:12:19 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37823C061574 for ; Thu, 20 Jan 2022 07:12:19 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id w26so12860826wmi.0 for ; Thu, 20 Jan 2022 07:12:19 -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=MAFv42Wdf02HqJQ1K4AFLDNAvLgG2Dg41xMO3uHwiLc=; b=Gn6/pt0+Lq1kihtiOl+S6gpQxkBBA0tg+JgAncP2TVwBe6w8dF1lwT/JtiI7ILJuyo vK+GQBAA6ftfYy7KF71RzsBPWgyCiP/xYy57t3UJF3/m7dzLw+Yx0fK5mV4x4brRaSLy aesiqWPHnx3c243dsjI+/Ams7ZE66Q2dpiAoDLsANLXHjlLkbT72wuo1k1qILBdktep4 Y2L5i5W+xAHczNDbybe6ntDxXhnwzBBHYVVRv8kPQUshr6vrdjX3J1+d4YmZYCi3clw1 dzebXFnRrVYOi956pxU77I+IUetCQw9R9X4aHQv/dr6UkA7gg4ItIRjFPD59XpXkoHAO 9DDA== 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=MAFv42Wdf02HqJQ1K4AFLDNAvLgG2Dg41xMO3uHwiLc=; b=CJ8PFFCmy8LsRtsX0EDh7tG4Hs0ESAAt2FHIrVJhyjk0HWX5whUjfaQMfK8oSo1nHT B1h3BN/UIDxc/XyUnQ6BDMIg9RCP2dojNqksAKlmJzy5I0E2ioOiSJWkM/kYIDoofzFe kHAFmx4SXW8pClfh7aXr/e+TVdo86IwouJylRlhSS9O84mjVjcT4yVeu6tuLdjwVlSFE FV8IEI8VjOE8SVclSev5i0rn5Mr6WwJ1OL5ye6ykPjkRMp71QxkditUTwyi3tUJUB890 XLzLUZYAd1yyPBqEschU417m1A5OHYaEEt1BJxxtt/D93P6kTQ2nIp+slnrJWNSEQIIU ELSg== X-Gm-Message-State: AOAM533zzf4iRKBoByV7NJLe+H29RiZ4CYkYTm9Yr/veWQpvDOddffzr 8j61KQNmlf5oqsKIodXy8zzL/s2XNkE= X-Google-Smtp-Source: ABdhPJz9t/QJ3/zTE0nCYgaAiwZYh4474iasnH/uSVjYA6J7IOlHA3uqOavFleNW1bzRo0HFzLwKXA== X-Received: by 2002:a05:600c:4e8e:: with SMTP id f14mr9539418wmq.175.1642691537529; Thu, 20 Jan 2022 07:12:17 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o26sm970896wro.9.2022.01.20.07.12.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:17 -0800 (PST) Message-Id: <315ce62e710e6e80dc8a4d959e5532b5f9142669.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:01 +0000 Subject: [PATCH v6 02/15] 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 | 26 +++++++++------ reftable/reader.c | 23 ++++++++------ reftable/readwrite_test.c | 66 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 18 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/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 Thu Jan 20 15:12:02 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: 12718756 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 8D0C5C433FE for ; Thu, 20 Jan 2022 15:12:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376508AbiATPMX (ORCPT ); Thu, 20 Jan 2022 10:12:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346200AbiATPMU (ORCPT ); Thu, 20 Jan 2022 10:12:20 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2FABC06161C for ; Thu, 20 Jan 2022 07:12:19 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id v123so12782862wme.2 for ; Thu, 20 Jan 2022 07:12:19 -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=cIHspsBwTzCzVE5M4aqRkyRLT2fI2Q3mrpJLD/2gtvPkH5oAdKu6HdzmQxJNSRRl1b QbMRgYhQznpGgsY5wJHvJ8PSbloOj2K+0CNedg3h+uXTh498WzD0Qw7jd8snwprRMKy2 rg3PHXPRFehaktZQEOPVVa+FogAE3BvQcWEEwTVEQb46eSr1dyAIw9lw6V7iuEOiuou5 Mi9cJqpUF8AbSSI5jmN/Vqtl8zbAlIqxR12c4tFlI48H9HEKBSbJOCBlHtMJ4dtlHPL5 cIDpQm+IgsjQyXAhtDnZlmue1aOk2hfoyUBZAvLSchVZZhgO9o0FyGPDPAwaxF6fp7/2 AYJA== 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=4gwFXm9oxJKAfoLEkj0x3z7JG0g9Mc5zPt6gG4l8rfLbAAzI8LzAPdtvSQYqM5NX/2 SO+TH6rua7Br2U+VLq+6qky5dgVP6ahdkRwxRqIsZdXiY5KA2GTRVfKtZnlIPIfnPArS 3XikmW+qerWQtolF9r4+ay0yqkw36IZPm7yGOmRlau3qJF0ut9n7t/xkK9X1J+FZ97OP R5dPEE0cLzU1L2kV5Q2VEZi5Vi05NL5P8R/wTH0tBrkbYZbWBdLOlgxsWF8N7cD3E6UN 3tKKyRbTFb2T1/WOCPk4CA9/aOWIP4I1VNOMAcVhsV1SYabfGr3WAcsRubLjAWesAdsm CQWA== X-Gm-Message-State: AOAM531nnyHgYq5WZalCxd1iJxv7sFtEjEI18nohhwNfQmSQdql5xmym nO6WLd7oHG/LoS+bhUI1+QBN9sv6hpE= X-Google-Smtp-Source: ABdhPJyv1vKnB8b9mBdnuKDqq2aVtslkAEZw4fdTvfqfXe54a5VbrXKIBejGWVPFvnGEliJnssyfpQ== X-Received: by 2002:a7b:c042:: with SMTP id u2mr9503989wmc.18.1642691538288; Thu, 20 Jan 2022 07:12:18 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n14sm3368598wri.101.2022.01.20.07.12.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:17 -0800 (PST) Message-Id: <7cf65e141cfb6de7668a06a8d66cc6e254fac691.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:02 +0000 Subject: [PATCH v6 03/15] 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 Thu Jan 20 15:12:03 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: 12718757 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 493F8C433EF for ; Thu, 20 Jan 2022 15:12:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376510AbiATPMZ (ORCPT ); Thu, 20 Jan 2022 10:12:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376488AbiATPMV (ORCPT ); Thu, 20 Jan 2022 10:12:21 -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 9B81EC061574 for ; Thu, 20 Jan 2022 07:12:20 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id r7-20020a1c4407000000b0034e043aaac7so286687wma.5 for ; Thu, 20 Jan 2022 07:12:20 -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=GydEKk/lw83MzlrywQVkOlfzU6wNV+DIBi8JuH4hUEI=; b=FIMT4tbd2i1Ga/sqi7Duk5CuM5JSpTePFDIXkKRsGAeM5Cl1iK2t0ecmYjFHzReFEJ BoWL8wkSoRV8nuPwFMOPfwzazrgEVIHOQfSaFFApayMSgzjGn6YpRQObUhA3loF6/cEJ qNkwZJbvj6OSvStXsiS7LE9+uOxRA9l1qzaWf0dXWl3lP64tMvLmghICnfBNr9EaDgXM PZcDLslFvPqu7cYKS4ey8lSjNkadej7/YRiIiRHWqFvw5SZZBrPqNTlh4Bl9iweOaGIv TBm+4z9UqISPotP0iyKoswX1OlbPsG0nvYlMvxw/fkg4HzefgsYOP0NAj0oqBPqb1dTt /E+A== 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=GydEKk/lw83MzlrywQVkOlfzU6wNV+DIBi8JuH4hUEI=; b=KGVUxxNjTg1FSvUcGqAINvg3XDftU4FQGLaNJr1GDaGa78NM0vYMmEGUpNkyyLiEnJ +/fodhJjEEc7O68iSGO7gQ0Pn+AubtjYv35sTQnqdy74VBHLHPlGF2MLne2Wj1EBzoTe Do8iGm8rgNxt2Sv37f0cAwIRAPEn8SrpbcPDWo3hYCFEFXE0zjc/rDKrzn0+ZgmmL2wt mWtO6PfB5A505z/rKypbGeqoEUdP63qAbYoJQ/lYQVcBfPrJFQvrwYfo1FC/s3doFY9A lGVxbtVK5Iubr8LJyFG7BYgqKHWBYBlj/vcS3kJ8lZMS50bZbwEgr01hLFe2qLNJfhun BHaA== X-Gm-Message-State: AOAM5324iqmg/9T2ylGLMe6+QXi/RiuS5AD1ixEcD3zO4HhOIY6NAcTY PYC9je9a4l1sxHggxloA4JuzVPZsZyU= X-Google-Smtp-Source: ABdhPJwkrpVm0n6J4R0VME5/JnU9Qwji2HkJ0LIabERh977rxnwNVczarX1hEXLScFvAQY8Fosuq+g== X-Received: by 2002:a1c:a7c4:: with SMTP id q187mr7541791wme.35.1642691539049; Thu, 20 Jan 2022 07:12:19 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n32sm8286644wms.8.2022.01.20.07.12.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:18 -0800 (PST) Message-Id: <8740ba92981c77f85b855e69c237fdb7449cca58.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:03 +0000 Subject: [PATCH v6 04/15] 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 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 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 Thu Jan 20 15:12:04 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: 12718759 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 25899C433F5 for ; Thu, 20 Jan 2022 15:12:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376521AbiATPM2 (ORCPT ); Thu, 20 Jan 2022 10:12:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376365AbiATPMW (ORCPT ); Thu, 20 Jan 2022 10:12:22 -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 3E306C061401 for ; Thu, 20 Jan 2022 07:12:21 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id e9-20020a05600c4e4900b0034d23cae3f0so14486545wmq.2 for ; Thu, 20 Jan 2022 07:12:21 -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=1RBfs0p86hdynJHyBF5Adh0gBV25zTb1bkuk97sM/+w=; b=KfDGQgF5TLEyV8bBChACaSDcdE3ca+PPaBBpM6crL4GOJeQuvNqU/XlZe2/Pqyy1FF EcKsjjLSobNNeLk+zJYmSgjBwoEkkHDIJQfpI0brHDDKcUgdpb86FCrGP2zosbB2E92E kAF8/ftSDm2HFbkaMB5j5Q7E3z26s66bqW+bVrl7ePInYD/3dgjThSZfEakW9FJUqINN pMXqjPoNZoxGzGY3yP/mtF5obY3/A4CZ62rWRgARKwL33taI/DwLSCd7cPcNkC7oLXZp p6uEdtuTZqdGfGH/7KkkzF4ljT0Z55ELegW5RsWLSWncUXXHBg/sIWDKTFqyrEpO1TPk nfyw== 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=1RBfs0p86hdynJHyBF5Adh0gBV25zTb1bkuk97sM/+w=; b=4uUWIc+vC8Y0hk8EOxWYpPP332uy5PzYzB0qiiC1g9bdJlhvVTZlAGUSKh0jbpJ12k XM0pE5Gs51gey/3TO21K2Z4yBuJuJc5v0tDtuYI2S9rQ0Gf82RlYqjWdDkYTP5pWgJ5I G0HgzSTgQs8/pr0ubI6k8C/+8vWMdqq0Dg0w0rUDIC5oYjv+0iaSpwSYVXtij++8oMoV tNxKPqUb5hXIf88TWH3ooAebvCP2iN++Ozovx0wMP/xznrjTsYqkvDRrBeQxll4vvEKK W4svhy+120hEzPNF+u418pVp4XC1BzfYVKzD1mj2cNXqM10uHI6x2DJgMqIrbNlVUahr oUNw== X-Gm-Message-State: AOAM533FIrTwotkWTyJAfEFZKl3m1+EAKEarXYWsg/VH4c4sZyzqI8ul SOt71cze6nqGEFobPcBFudBqMV17/CA= X-Google-Smtp-Source: ABdhPJy10YMADMDQAkaLeFz7XEHGkLuIIjtNpAQjS6RF+WTcjq0ZZDz9eGEgihVk4PtgOoAU03rWNQ== X-Received: by 2002:a7b:cc12:: with SMTP id f18mr9515006wmh.42.1642691539771; Thu, 20 Jan 2022 07:12:19 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p16sm2649773wmq.20.2022.01.20.07.12.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:19 -0800 (PST) Message-Id: <08be6d90a4890b63fe9b0885af0df084c9ca81fd.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:04 +0000 Subject: [PATCH v6 05/15] 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..19fe4e20085 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); + (void) remove(fn); } static void test_parse_names(void) From patchwork Thu Jan 20 15:12:05 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: 12718758 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 3A77DC433EF for ; Thu, 20 Jan 2022 15:12:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376515AbiATPM0 (ORCPT ); Thu, 20 Jan 2022 10:12:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376509AbiATPMY (ORCPT ); Thu, 20 Jan 2022 10:12:24 -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 014B8C061748 for ; Thu, 20 Jan 2022 07:12:22 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id v123so12783115wme.2 for ; Thu, 20 Jan 2022 07:12:21 -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=i6kfUc6nxgzHKqB4LDRUfuZVULCh/jE8z1l8Vkn5LcvbxTyrSNhAK8MNRWDFwz7Lgw xfLumpUKAv2LEM/4FG55oGdePsjT9aLoK2rSVmXggzU43plm1lwTp28EbIFOjX0dJsfj hPcfOqIl3rfg5LZEYpk+jd9UiRxjs+tpIr2SbawjfkCuO7DYu4RMm/fOXmSb0No0t9XO wZAAibpaE/m3ItfB+kUcryFZMnfIi6NPFszqFoRbfWe13VTOAh7pKd98WR5Isdyf43hd 405/OnDOoyzxxDNAys5H2oaxv9OoviIjlql2zJ4hYSifN/4w8n6YQSbPhPepdEcWi2vk c5oQ== 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=YAvQtDQ8RjNOdHnsEqW89YbpPW2zkpCp5jrQIVlzqBkCK+YYmNqp5qISVVX5vGIScV bGvwx7Bd5lNSuElvN1HNO5RYGIZCofbI6EPSONIbSxpshCqpYBA6P2kTUXZH0g8mmqBq qolojiLts+Vfvglw5sOwfbe2q+YCqLHHfMHyCr/dhm6ruQ1qI7xKhI/hGN3MCSTnNsJg eSol8+BCN2SUEFcpaKEhlL+NmgfxgqE2V3dWvO8sLFOkNLzmCmWJRfYUZ5SNIrjAgOdt F7NiW7BQyr48AM+4RWvvkREQs4Z52acQETb8u+f0+Y8PShLCSH2ENLpEeMT1V2Frna3a XEdQ== X-Gm-Message-State: AOAM531wxZjWk0HhzAuYcIFsvKFr5yN23ijzKFdMBH8cIC+Z4wzLZnWE T5gO2NigCtPONhvxCSBppiinDiwgvK4= X-Google-Smtp-Source: ABdhPJwRkFgVRBf12KEhAqVpR9q7KglFx2VZsKWiE068+E21qXrvY83tfpILYexQZIJYVchBCBF6xQ== X-Received: by 2002:adf:e196:: with SMTP id az22mr33910301wrb.637.1642691540503; Thu, 20 Jan 2022 07:12:20 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r7sm6375140wma.39.2022.01.20.07.12.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:20 -0800 (PST) Message-Id: <630f67bdb519251815f878fba73b07dd0ae75320.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:05 +0000 Subject: [PATCH v6 06/15] 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 Thu Jan 20 15:12:06 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: 12718760 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 DE075C433FE for ; Thu, 20 Jan 2022 15:12:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376526AbiATPM3 (ORCPT ); Thu, 20 Jan 2022 10:12:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376512AbiATPMY (ORCPT ); Thu, 20 Jan 2022 10:12:24 -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 BEA55C061751 for ; Thu, 20 Jan 2022 07:12:22 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id i187-20020a1c3bc4000000b0034d2ed1be2aso21195966wma.1 for ; Thu, 20 Jan 2022 07:12:22 -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=HZ4Qa7Lrji45Xlmaq70NEery4vKKRRFPiYCkEINxFPMutnaWCNReMhMoMkFarFuH5M ijHb63O9vECwVqN1uoe4JSJCHQpS1owvgdLdh8kICSezT5607YJ95hrqfrfZ7gmXdHGO Ebriomb4YYnmlV3eR14TDrsNuKJUE4RntDy125wKr2Ra/ehzYh7nhknuvalkYdmHmjEV YI9bBa9639+sad9oEVcZKFY4cWHnJ/ByPZ+eKkSNgKXs+1KBr14YBmn088Q7/9Su/SSK oSLC30cJg7AedutNUUvi+1a3RfIEvPYjYVNiF56JF/Y7vS3CiTbu25q+Tpu4hNKAHs2R oMUg== 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=or5gVuM6uOAB/TixbyOme+dp252iLJLKlq2qBgRE3WSaIgKjMp1yW81SWE8dcf6hyE CdckBTPgTORgMIrwquEoiqkfQdRbUavUNUPQz9iO1Rktji6W6eObFVUVm+iTiD2A2v9w +3/fhvdVVG05K5/AJtE9BgwNl64fwzonN6tlID/7QTr05KUaPhq54rF3dnWb36TfuonL yMSoV1CrogelhssVkSCihff+dpSHTUlNBCf5pejWzfzUNaWgNutpKr/XKzEpAfZTCDhs Mwq5vpnTw7pY1s4HGD3yBaql6pSKE745cvclE5dxVDRY5sgllvmIBLaTjNhYmxQO63AJ VBhA== X-Gm-Message-State: AOAM531kBHI7dJigosOqTSFYWDLkkxP6LFHOGnGocghsirpdTuMVRJio BnjvELzl8oafYmGBKvJtywlgeAKfvQc= X-Google-Smtp-Source: ABdhPJzWJR1W1EKD2Y75Z2OjIk+5CKwwZGcIXUe3zaxlyW8O9Wbn7gipL+6qlI5psBizJy3zjOSHHw== X-Received: by 2002:a1c:1dd0:: with SMTP id d199mr9356460wmd.121.1642691541225; Thu, 20 Jan 2022 07:12:21 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r15sm2782230wmq.3.2022.01.20.07.12.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:20 -0800 (PST) Message-Id: In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:06 +0000 Subject: [PATCH v6 07/15] 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 Thu Jan 20 15:12:07 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: 12718763 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 C5B7BC433EF for ; Thu, 20 Jan 2022 15:12:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376524AbiATPMe (ORCPT ); Thu, 20 Jan 2022 10:12:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376514AbiATPMY (ORCPT ); Thu, 20 Jan 2022 10:12:24 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94C18C061753 for ; Thu, 20 Jan 2022 07:12:23 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id l12-20020a7bc34c000000b003467c58cbdfso22639740wmj.2 for ; Thu, 20 Jan 2022 07:12:23 -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=qyV/9O4Znm4gOArqo6ts0AYtWYyIVLX2JDCEDy33fDt3rghy0b2xvkWpAHpXdp+pNQ jPuZt3md/hARNAZaGMmhVt+XciaMXQclkGBr3sZY2RFn5wbZJaBeBdk6ERdk8qyV3ZhF mKR/Y+Mmo4ZXvh0srDsw7liKXvO9XaIUr0WMf1yOipMBXHXTKBFS0UdOy6fztunTPK92 ikzevtAcWH+VIBuBgoq8GpJW3e8p3qrPMfouc06FmOvzKuQ5t9v3NUt6Nyba9QBNKfkZ fMaibLeqwVJTVfAOFQOYDYGADoCwIF3zT7g2XRSu2IBQD6m7hR9t8iHnhj9DLlrwR6H7 xQ1w== 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=ERtUToBTsf+X2IRZKa/LhfdumdUMhXPW5kHOrQ0cS0CB1bqnQ/9OPkt0FSGiiAGCgD q7EpMyXwDIZ3OGqTEbjb0+QOWYfpYyhQOpFYYpPPAvfzLdpkmRYxUkbA1qHg8W2Fg69V Vs3oQChrPKiqAzUFZ3lBFzlwbVZYQsjYoiAQ+ADs8ZxFfN4FoQK3tdOOwUQtFeaq903W mjvq6ZDocW+QkISAylaOCBMJbsHXkwBGNjgYpSUmW1UVvv12hEvNVh2GIz5A08eEjJgP j3vmDXLU8Tn6HDILyUhx7saN37nbrm5qVH63t+Gex+7l9iZ9S9Hox4VF6H8Nxp0GAStE p+og== X-Gm-Message-State: AOAM532bT6weeKjWr0XYlKTpdSgAZZyNXr/aUO+e9g4A/fOovuSIVt+x ZNmkxiP5AHv56VZLty5v8JIDfkYbNyM= X-Google-Smtp-Source: ABdhPJxsaTXdtvMC+0xKn95nbK8JVenqFUcssAOHyL1mbRfQrf4sRrALrUQmlO0lu4uVihZK3E+pJA== X-Received: by 2002:a5d:4944:: with SMTP id r4mr6610908wrs.550.1642691542030; Thu, 20 Jan 2022 07:12:22 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n15sm4140992wrf.79.2022.01.20.07.12.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:21 -0800 (PST) Message-Id: <6f4f24c17ad91b28b22deff2530039bcfde2cbe9.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:07 +0000 Subject: [PATCH v6 08/15] 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 Thu Jan 20 15:12:08 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: 12718761 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 C3351C433EF for ; Thu, 20 Jan 2022 15:12:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376542AbiATPMb (ORCPT ); Thu, 20 Jan 2022 10:12:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233375AbiATPMY (ORCPT ); Thu, 20 Jan 2022 10:12:24 -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 4D875C061759 for ; Thu, 20 Jan 2022 07:12:24 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id i187-20020a1c3bc4000000b0034d2ed1be2aso21196158wma.1 for ; Thu, 20 Jan 2022 07:12:24 -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=DRxjwg+yq/gkc7QeQ2Evwutq50I+Ny4TmImaRpa1aDoxwx0AOJu44Gv37i2AfHtPOo /KuBqNPQE2gTmDo2qg/LeQ7iAaQKjvGVGvAXbp4efhm/OjKH5ih6g5v/XMemMYdsFlsj Jpxw7Fj72cpzgqyJx7B4jBakdMdyUm3KC6Nadgl/B5I9ITTvKjyPmuYPTfGteSn68pv7 XDFprZ4F/9FWLlgKzWcWXiW/Fm3hLxgydlVsE4pXacL4wyto18siM4nOIpqY8vzD7V4D sb9S3qWdPSnrVhKjP25vruauj6uuKtAgNCoN1Cuk4/gtR61Y6O9Lm4ZRRTbNbB7F8H4s k7Ng== 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=obOMN/PbBsAH70424EpD5x4z2ueKGv7CkKbHPmz94RNOaeJXKtHByeUfymz3S9TmjC GMp+E+LhQqufUD9eikviYzXqVQi7RKdSM2yUGlv9MhV0Tpr788UMlyUyqUFKz0MDy9wY kWScce4EzqD0pP+mO0DFdxyECfsDBhlq05jtK/C28X1FhnyQgxkDyQJo6sXqFP2vas32 xCQgx0oILojcdSru8cGwZZFWZS0Zu4DPHSiZJWIfqFyno8R+wwKlGPc7tr4uqJf2darb MAD2zaWM4MvmK1t1Um5im35J+RcNFkWaTf0QONr7kYSuaXY3lGg+s6Dp6SLIY2K7afth 4RyQ== X-Gm-Message-State: AOAM530NlUoquYvFgu18Shm2ndM43hAi6qkDHPeye/x405uVutp6XKqs KYyJ0Z11GGWwc1HWzHuKwSiUxt6W7yw= X-Google-Smtp-Source: ABdhPJwgznvatoQAPxWUxgXLglJfXY5rhwHrM7hMDVyxjlKcXLzB0Q1UFXkgx9LI9EveNi1iXlD1rQ== X-Received: by 2002:a5d:5887:: with SMTP id n7mr23810634wrf.499.1642691542823; Thu, 20 Jan 2022 07:12:22 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id h8sm8801634wmq.26.2022.01.20.07.12.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:22 -0800 (PST) Message-Id: <0abaab1d7c47dfcea4490459897dd100d2be39a5.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:08 +0000 Subject: [PATCH v6 09/15] 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 Thu Jan 20 15:12:09 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: 12718762 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 60483C433FE for ; Thu, 20 Jan 2022 15:12:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376532AbiATPMb (ORCPT ); Thu, 20 Jan 2022 10:12:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376511AbiATPMZ (ORCPT ); Thu, 20 Jan 2022 10:12:25 -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 1AA86C061746 for ; Thu, 20 Jan 2022 07:12:25 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id r7-20020a1c4407000000b0034e043aaac7so287242wma.5 for ; Thu, 20 Jan 2022 07:12:25 -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=iN8tcffPW7Vx2ZhdJzn9MVtkdOCqVT7lFBrsFId2Xs0GKA2ipaz1tPcoaDMtiouCoD 5XIUvFTsNxNh810Lpao8BUQdSaWwkbwnKK0c1kmpqwVSsKBqkL4JK44HW+Jrt+Zf6g9A 7HJilFXtjAsksx2ts9qD2NZChq04kowK1d2RxtHefKl5+TgdbbRQqIRR+3q7hy493/QZ uWDQwxXaiadSMHEuqMeuYYQwrx2LynZGX0smPvhqllHh5hvoyh5GSRiMlbsSh/Hgwk6s eG6cLd57YRdzfxfdYQwLFc3n/OUj62m5lSfMmN3PdhxgcqileUIu5fYwgmmf9iWm5EiW cEpw== 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=Gi283BcvUreyiU88to/nBHq+a8UOtVaPNpM0nYfOL9PDuRkb0oMihC8iNFKRnofyjY xUySw+15aR6C+ObUUZbhNWhX2x3Gt48t/LUSH4fEyBM/Ap5V1LHNVEk9eBRmyNqs1v1q bl9u/06nhRc0WL2aOjoktHswC7dhE0WWdbz0UbodbwQkupfjGipvgUJ7OUyiKJIB8W2k KcBkkW96r9DsBo6t+D7y6qSb4wRo/1YWQUoZcsGBX1fH6J5EFwMzf7BCgZ5+pWFLmd5z j8U6efU8eDgFI/SGddLQhDWKziL6N9RzmAm5dnU46dSPzEkm2F+YicpU/GQh1viWGZMu ky/g== X-Gm-Message-State: AOAM531o36TlqNXoPCGQKOipvSvgLVbPMNGoo1IT0iayS1ArPgbRM2MU Fa4v8R4bXPZgb5qcmQvzEpm3eGBCIe8= X-Google-Smtp-Source: ABdhPJyZDr0d7wnARwyEawWbs2N9Wb5tRy6ar3j/2+m+JNkJbia0hlktd290TywQ/55CPCsQ1SqekA== X-Received: by 2002:adf:a34c:: with SMTP id d12mr12084694wrb.80.1642691543599; Thu, 20 Jan 2022 07:12:23 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id 16sm2502843wmj.12.2022.01.20.07.12.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:23 -0800 (PST) Message-Id: <004b7a736bf5ed847bad6518a5a9434c6176f98a.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:09 +0000 Subject: [PATCH v6 10/15] 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 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 Thu Jan 20 15:12:10 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: 12718764 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 88AF2C433FE for ; Thu, 20 Jan 2022 15:12:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376523AbiATPMg (ORCPT ); Thu, 20 Jan 2022 10:12:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376488AbiATPM0 (ORCPT ); Thu, 20 Jan 2022 10:12:26 -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 D52B8C06161C for ; Thu, 20 Jan 2022 07:12:25 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id r7-20020a1c4407000000b0034e043aaac7so287314wma.5 for ; Thu, 20 Jan 2022 07:12:25 -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=7yeGXzd+HLokH3mIqMgC6QPxufo4Z99YCUN5bOC8N9I=; b=d+nG9FZUDJe5q5FM3IzacJxO7ka9XH7uEDE+oE0X6roPrFFR3+PZ6yQy4wsnisuH6Y uO8fLE71GmH9Yd3eTqZBEIGhmvmFpHaYRVFLLhDF+A2eHQn7gqdvSGCzd3IfHRyMfw3B RKg2h7c2TagS3OIBxrzZGmd2Whq39sIeSTi6viIcVTeXJopvmdDg4Zneo8Y87+Yit1j6 R4FnnmwnrPvpIWFrcwphxccSfo7XnwHE6swr1o4tONK6arYOjz7yX4YQjaoKqCcB7cdC erJS0GFu3GO77lY6+HGmdhJ1W2KJsbcLSOhz0Ej6XvDSsxtFWG0uO5lwI/0Rd52sBcnT ddjw== 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=7yeGXzd+HLokH3mIqMgC6QPxufo4Z99YCUN5bOC8N9I=; b=tjLu9FvR3emjUBbZ8edfyoYhPIqr8SON2Gqz+q7vmRTwUmJeePcKLnLJ+vxxdSFBhJ Vxn4tNc9w+/XhE4xKo9O5Ffw9lChL6Dlo9wvzUo0vJklEPd41DWTw4opJ5f+0LKLN4ir CTO34V1AGicw3oLjkSj9uHMMXC7nDE7tQodLcIR2I2FtlRzpTagMwVtXFQtdYaq9MwiR NyUebcGocdZqx3LXWUe6vWLUax47hP1xKGLSpU8Pp3pxyaugw2BjyqW7ruhCO+YDNZVn edr3BgnfUzQu4vSlbF5jCOzsaasQ0cjMf4Gay5KOpdroM//OUvUNd3H6HxO3MZQUeAm5 NzHw== X-Gm-Message-State: AOAM532BrWqkAAsZob3pf8nAdPLEAC1eEoP512Zjzh7//MmbNlraAZF2 3+wassQ3hogRwmf4w12c0WjKUYaEYGQ= X-Google-Smtp-Source: ABdhPJyjYI+I12ykxtrejUjftDSHbw5tBwsZol1heh4k8oMV4cRLlA6eNoRlK6GjEtBwHKHGu7bzwg== X-Received: by 2002:a7b:cb83:: with SMTP id m3mr9458406wmi.150.1642691544334; Thu, 20 Jan 2022 07:12:24 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n5sm7922447wmc.47.2022.01.20.07.12.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:23 -0800 (PST) Message-Id: <951efcebcd2097ec3af71703290c9e8c293bef05.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:10 +0000 Subject: [PATCH v6 11/15] 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 8bbcbff1e69..f7c77c51539 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] = { 0 }; /* BUG */ @@ -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 Thu Jan 20 15:12:11 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: 12718765 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 D6120C433EF for ; Thu, 20 Jan 2022 15:12:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376561AbiATPMh (ORCPT ); Thu, 20 Jan 2022 10:12:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346200AbiATPM1 (ORCPT ); Thu, 20 Jan 2022 10:12:27 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A76BDC06173F for ; Thu, 20 Jan 2022 07:12:26 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id o1-20020a1c4d01000000b0034d95625e1fso8127962wmh.4 for ; Thu, 20 Jan 2022 07:12:26 -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=ifvX/FZZWEtWmoSJwuxEfFvFGzduSB4zEMVZ/LX8/Lk=; b=AQiu6wbZg3aOPnNBszIaA0qMeQa9tYTvnUzGE7uRcIDy+TUIt1AmWjSroSH85GZ1aV 0ihUnMH+kleIud9kCXFcd0Zkde7cQS7QRRZaCft6ZJQQKH5HtlQeRCprbT5v4K82Szhe 78RZAarcKOnaTtfkRslv1qBjO4We/IGCaFWU9Rm6RD/LiUQYxhnhM1aPNWlYlV8UUveo L4sXo8KD28cG/l8/3LsatMcagpgpjRuVAkVu9I69wb8+TYthDGT5DrDy8PaJ02p7z8zL yLC8wpn83cFsqq2e2/qllhXuhF376+i4bvUbE+DPc3ptbXKxky/8gWz/+OU+pfBRA4oP Ak8g== 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=ifvX/FZZWEtWmoSJwuxEfFvFGzduSB4zEMVZ/LX8/Lk=; b=6xqgB71LIHJDayxTSVNIaDmvK9R4jgRY/INvJiI1uPlnjsxDyBp4CBwaVcKHhf6SeB DrUn3eR6Gg1izr2YWKsErAKW37GoKxlKk0cUZ60EYJYyEzWnu6pBAbzNnhjnkuiiMpVD sy2DKkXtagZluXSbLJ4dFTfDbUB322G9BhsjNXYwN/cgAi2o4zwm4dwhbbGe8Z5ji2b+ rACpR2hNlChWKlkMzwf4UHAyd9+yvb0D4kCwuSjZNobwvzgIQipYfQN9YVwy+TqS+yCw 7fEx237SqxqN1L+RBttunqlEYG/rdesFVRisshNI1R3TS22Zne2Qlq82TZXeVSr4xBO3 MabA== X-Gm-Message-State: AOAM532ISd8L6/Cwg76yCQa6Lkn/DFBMzCDgKYZvif1EI0nQQ2vKTSJu xPZcyiMhyeuAs4pN7BIrid1LruVdKXA= X-Google-Smtp-Source: ABdhPJy572aN8Q+9KmHLSEJXW3q5NiVsYIXNNB/4ESw7xo9fEAB7cz7zITFnZbSIIgGtVa62ZumXNw== X-Received: by 2002:adf:fa81:: with SMTP id h1mr34652685wrr.19.1642691545153; Thu, 20 Jan 2022 07:12:25 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p17sm2870246wrf.111.2022.01.20.07.12.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:24 -0800 (PST) Message-Id: <6385e449ba714e3ce41f10276fc60e9757519492.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:11 +0000 Subject: [PATCH v6 12/15] 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 | 57 +++++++++++++++++++++++++++++++++++++++++- reftable/record.h | 5 +++- reftable/record_test.c | 23 +++-------------- 3 files changed, 63 insertions(+), 22 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index f7c77c51539..2a9e41a992e 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -430,6 +430,15 @@ 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 +448,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 +582,25 @@ 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 (ra->hash_prefix_len && + memcmp(ra->hash_prefix, rb->hash_prefix, ra->hash_prefix_len)) + return 0; + if (ra->offset_len && + 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 +609,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 +911,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 +962,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 +1081,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 +1098,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 +1146,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 Thu Jan 20 15:12:12 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: 12718767 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 48E1FC433EF for ; Thu, 20 Jan 2022 15:12:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376548AbiATPMj (ORCPT ); Thu, 20 Jan 2022 10:12:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376535AbiATPMb (ORCPT ); Thu, 20 Jan 2022 10:12:31 -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 659DDC061749 for ; Thu, 20 Jan 2022 07:12:27 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id r7-20020a1c4407000000b0034e043aaac7so287493wma.5 for ; Thu, 20 Jan 2022 07:12:27 -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=fDCQRPmbctiJ9S8YcosDCrYYLBkTbCalMV/n2/UyMLqjim7/HXKd3OktMclD23UBCa Jk3gmB8LcrWA09tg1I/4xNu0ZnU+mq/zFG0pPvLHsAskFO0tWNjpcP4T/OSZ4gJO0CGR JhFxCCm5E249BAznY/f6CvpQXBLJD7hByc53Hma4QF+yDHvCZAJZR+5apDSjB3ndww05 j5xjPL/uhbKc9HlWeOwsDEGow4mUo+yHjmmuFA61DotFqxRWhUcuEBuxlPusX0fxeTHA 732dMov9nKP9GEdQnlSlvJJpNqVzqcotMZ4lMWEWxuxaezmdDttau7ka70PvLwWFQtsN hn9Q== 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=wtAv0EErVCwq0AVWb6b9Lq5Swtq4EdhzZBMOZ549Zs/2jsf3KA44hBfKN0z656VXAt G2MYwWWh6y1f8YRTWqndn2S3khqP4pfovlJ/Nmc131dQxEHFWXtpV1pYPtMsiByqeCQi 2iJCazI84OtD4MUv1Q0A3LMjzKaibLJFor2hBF1yuQV6HZWSJmGD+Z/wtG7pRBs1D0eD JAXHqdSDoshpHQvoI9mQLfcaMwDcqcw4uGaqhgxrSXNsfu5u/rHOaAUUcU4rnAZeAs5S jSQHvhO20NeGOUKSKzqG/+dXyi2hONKiUgLuw+5a3Lf0M1xZKlY46k+b04xdiCQ7URGv wPOg== X-Gm-Message-State: AOAM532+rpWQo20+5XvbMzjWxeuX7e7i1cb9JhqnDlBpENQhCiq6SS97 XsOJ3p2gWkW7v7qsaABB4gn24f4bldA= X-Google-Smtp-Source: ABdhPJzF5g7Xe0ddzedCGx7fTrl1Kvf4HO/7iyJTaL1ywjBY4neQxvdn78Mjen0P2CotcBo8gjGfmw== X-Received: by 2002:a05:600c:2216:: with SMTP id z22mr9543590wml.119.1642691545910; Thu, 20 Jan 2022 07:12:25 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l13sm3271706wrs.109.2022.01.20.07.12.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:25 -0800 (PST) Message-Id: <69c4a55488209f96ae605047ecd1709678518fa5.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:12 +0000 Subject: [PATCH v6 13/15] 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 Thu Jan 20 15:12:13 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: 12718768 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 CE13AC433F5 for ; Thu, 20 Jan 2022 15:12:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376519AbiATPMk (ORCPT ); Thu, 20 Jan 2022 10:12:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376541AbiATPMb (ORCPT ); Thu, 20 Jan 2022 10:12:31 -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 0D13DC061755 for ; Thu, 20 Jan 2022 07:12:29 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id c66so12734480wma.5 for ; Thu, 20 Jan 2022 07:12:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=dVcn/vt7EuSCHbr009hYn3daNggRX9ySp16twunuqXk=; b=K9JyDilWHN8InXM37oaPO+AVqS1AjpS1e4WzvUgibft4TvfCZCTXgRz9wEzlj6Vq74 yEqLKEQr1NDqSGkHdP7HgCzDw5/aHWsH6JvIXZg+/FYtvp38SuuW+lyyLaZdHcRcqN5z WX951nUraUj7fWcJygPMqLmiF4bOg4egvJzy0ZuX2GhOHHr4WqM4rzzkc4FtgCNhIznX yTD9ZiUXuYAv401FbFePuGhm/YbbvH4dQ7uQWL0U9RGmOMlEDLNgVyHwPqaBzJky3NgX rBq7X64N39QJqrI/4H1bAoOc6PuLbaaFuwKeMkCXrnpVXF+XQAe4uBFhlM2ZGkC+eCWm /QoQ== 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=dVcn/vt7EuSCHbr009hYn3daNggRX9ySp16twunuqXk=; b=Xkp39I8gCXLE3Keq+YlJ/apIMRZeeAqX6UpWSM/oHhH/xv1sWQCFYY6vVk9TNiEs84 Gvj1B01yKcGWWjotyNnw3QLkekUSPI7BTjBlFgfHhy0Drsg9NHRRgMBvYKv5am0eTyOS KoBBQBGpt/hLNofs+ILEvgk6XGCfMFB8287YuXDX8RlgCplIWH42btNqY6UNLqZQDG0Z Iiu6N3JAi7tqMWfpFZR+w55mf+x3p+7aqIaKkj4cXByBateHqYU5Yg92Sd7cxmaMqfWI 8+eTogtzFNakDE4ICwHsYp20EflGei4ohOtjR7MNlRFAmx0VMdMa9NTZKuEoA5kp1W9e D6dw== X-Gm-Message-State: AOAM532WxlcfTYTc6hdh/FBYSvZ7Ew+BzlO9BjH5aAqzyPapDcc8JHt9 CaSrHNz438BuEKZqhI7qv5AZle8ZFqM= X-Google-Smtp-Source: ABdhPJxxHqZGo4GyS0Z9MVAZoJ6B4Ol+NQF/6ZxuV9XfjJOCNv9yN+lSZ7oIXSPPYm2m5T9mBLU8SQ== X-Received: by 2002:a05:600c:3482:: with SMTP id a2mr9490821wmq.152.1642691546901; Thu, 20 Jan 2022 07:12:26 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u14sm2935159wrm.58.2022.01.20.07.12.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:26 -0800 (PST) Message-Id: <54bcac325bd542aa81328e5616b2b7573d43adac.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:13 +0000 Subject: [PATCH v6 14/15] 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 | 41 ++++---- reftable/iter.c | 4 +- reftable/merged.c | 33 ++++--- reftable/pq.c | 3 +- reftable/pq_test.c | 27 +++--- reftable/reader.c | 87 +++++++++-------- reftable/record.c | 212 +++++++++++++++++++---------------------- reftable/record.h | 42 ++++---- reftable/record_test.c | 157 +++++++++++++++--------------- reftable/writer.c | 41 ++++---- 12 files changed, 334 insertions(+), 337 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..b27d152e89a 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,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..2a6efa110d5 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,11 @@ 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 = { NULL }; - reftable_record_from_log(&rec, &log); + struct reftable_record rec = { .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = (char *)name, + .update_index = update_index, + } }; 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..7de5e886f35 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,10 @@ 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 +53,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..00906e7a2de 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,35 @@ 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 +545,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 +595,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); } @@ -607,12 +608,11 @@ 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 = { NULL }; - reftable_record_from_log(&rec, &log); + struct reftable_record rec = { .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = (char *)name, + .update_index = update_index, + } }; return reader_seek(r, it, &rec); } @@ -656,31 +656,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 +690,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 2a9e41a992e..a8cee628942 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; @@ -475,12 +479,14 @@ 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); - memcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len); + obj->hash_prefix = reftable_malloc(src->hash_prefix_len); + obj->hash_prefix_len = src->hash_prefix_len; + if (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) @@ -965,58 +971,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; @@ -1103,98 +1057,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) @@ -1267,3 +1183,75 @@ 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 = { + .type = typ, + }; + + /* 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 (typ) { + 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..c6fdd1925a9 100644 --- a/reftable/record_test.c +++ b/reftable/record_test.c @@ -16,13 +16,16 @@ 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 typ; + + typ = reftable_record_type(rec); + copy = reftable_new_record(typ); 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,61 +98,58 @@ 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.value.val2.target_value, 2); + set_hash(in.u.ref.value.val2.value, 1); + in.u.ref.value.val2.target_value = + reftable_malloc(GIT_SHA1_RAWSZ); + 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 +202,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 +210,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 +236,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 +313,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 +357,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..944c2329ab5 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); @@ -316,8 +320,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 +400,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 +471,17 @@ 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 = { NULL }; + 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, + } }; 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 +494,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. */ From patchwork Thu Jan 20 15:12:14 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: 12718766 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 67ABFC433F5 for ; Thu, 20 Jan 2022 15:12:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376567AbiATPMi (ORCPT ); Thu, 20 Jan 2022 10:12:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376538AbiATPMb (ORCPT ); Thu, 20 Jan 2022 10:12:31 -0500 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F6B7C061756 for ; Thu, 20 Jan 2022 07:12:29 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id bg19-20020a05600c3c9300b0034565e837b6so7234177wmb.1 for ; Thu, 20 Jan 2022 07:12:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=EQ7LuISxS+OlxHRMui8s5gta6TAMzHzfTV3N4opZx10=; b=jx0xj6Egeb1hwz7Z9Hb9cocIJlQalN0akv9grnwD/6whpSjDEuMWm6ZDRi7v3oQe4n icQNntxOBi6vM6bmOS7vsyGqClE7phy2Yl+e+b1H0vMKtSlmFZWz35WSSMvccNstIRHA l33EoFR+3Q6urj41nT/cRfOm1aIeiQMz8AWSqiGrQuXU+izJAWYAJeOD+18icNw9FkvG snRMjP7WJIWeHLiplQCHUZYkXdJF6u0yWhF2wqazNsm3VEQyKXmUCANqI0EQ5ncKymkW gKzmCD8z61hh1E1DBf1ftmUUVlaMz+v81KfY2ybqlJsbrm6Dw6qH2IbL+vLg3fTWIi+j ftyA== 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=EQ7LuISxS+OlxHRMui8s5gta6TAMzHzfTV3N4opZx10=; b=NnmkzR0ZtiTRlw2dqL7YmSaibrDanMce5GIzsW6ScGhx4yVJnHZZa1mZCGzPiqh2Kt yrrmnbbgqlXQ/Id86y0OKcNqVIPMBjpiDyXC1ThKPBU9AGLYujAA9KnftBBw37JtitqH Is7jTlNsodScZPQcWxlAyOOoQCAkiuGVtVWxudgJrJ/qu9BZkFuYlyR+eyh2bFNi7il4 PYm/mNDYNivi6EPUSDZX1TAL+I/xm47ssjtWh3eaP9nBguVkyEg3LhhDKxVp7vuGsSNI 4mSrJ4+UE/qLlG/WjnI767cYVuFNzGlnaqA4yv0OIiRR8xNjGh9v7lnZ0uK7rsbpBzrK wk6Q== X-Gm-Message-State: AOAM533cskjzbfuYRmhh5tNaVGsu4tWBEdH44Gni5s6dByMhhEJJySHA d0nzzb8Xfrd4ZxLExk9menUyB7YMDH4= X-Google-Smtp-Source: ABdhPJxMo0Ja1Ifi8c6LRAWvffPpsrt6Q4dIop4sQ/R5fgV0tBxyuwCJ2SX+7mAFaLOpfaN+r+jSaQ== X-Received: by 2002:a05:6000:15c9:: with SMTP id y9mr32613823wry.689.1642691547669; Thu, 20 Jan 2022 07:12:27 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d2sm2905957wru.40.2022.01.20.07.12.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 07:12:27 -0800 (PST) Message-Id: <82f140cab5c30b59de534d773a5d00343e0868f9.1642691534.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 20 Jan 2022 15:12:14 +0000 Subject: [PATCH v6 15/15] 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 | 85 +++++++++++++++++++++++++++++++++++------- reftable/record.h | 4 ++ reftable/record_test.c | 21 ++++++++++- 3 files changed, 95 insertions(+), 15 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index a8cee628942..fbaa1fbef56 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -255,8 +255,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] = { 0 }; /* BUG */ printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index); @@ -265,14 +265,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: @@ -282,6 +282,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); @@ -443,6 +448,12 @@ 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, @@ -453,6 +464,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) @@ -471,6 +483,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) { @@ -617,31 +644,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, + 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 = @@ -959,6 +996,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, @@ -968,7 +1010,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) @@ -1043,6 +1086,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, @@ -1053,6 +1103,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) @@ -1255,3 +1306,9 @@ struct reftable_record reftable_new_record(uint8_t typ) } 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 c6fdd1925a9..f91ea5e8830 100644 --- a/reftable/record_test.c +++ b/reftable/record_test.c @@ -25,6 +25,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(©); } @@ -176,7 +180,8 @@ 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, @@ -197,10 +202,24 @@ static void test_reftable_log_record_roundtrip(void) .refname = xstrdup("refs/heads/master"), .update_index = 22, .value_type = REFTABLE_LOG_DELETION, + }, + { + .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), + /* rest of fields left empty. */ + }, + }, } }; 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;