From patchwork Wed Dec 22 18:56:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697123 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 E2921C433F5 for ; Wed, 22 Dec 2021 18:56:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344947AbhLVS4k (ORCPT ); Wed, 22 Dec 2021 13:56:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344925AbhLVS4j (ORCPT ); Wed, 22 Dec 2021 13:56:39 -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 575D2C06173F for ; Wed, 22 Dec 2021 10:56:39 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id e5so1815949wmq.1 for ; Wed, 22 Dec 2021 10:56:39 -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=lCux+Z1kzOm6YqFTPAy10jS1g4vd6/lZlHiy7y6MBc/KuNcOyrvlZ6ruqEFzCDAqT9 VSA+pC0sNnVD1u6JyP0NZNK5NUmoDZ4qqrhTToCkXD7xu12Ew/qghffvLsUvaCTrem0h v0COfvWG7rmcn7+83Twxc4grpJQ/Qb2sbV9vpAxUGmTxcVe6mNRc0WuSR72ktPyWva7X eVdN6qt9gnZ5fovz23g57hDVvw35C0yskv27PuLMYI7IKAYFyOPKduTtrMqYsCeeyc+O /KwyHDAr07P2avNZxnw65Eprw0pxTp2Bh5EE3uigMazEgNnWYoyVaeLb7bvGS2fIExvZ B37A== 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=Vm0kUjOMZWnT2yBLIv4uZ8UrCe0nUWTZqT4XhkaCjUQELUV+z7j9wUMQFG2fDP+gF2 uGMH7sbm16xghYySe+WJrXFYWn5ezPTca+eJSOUdWZ4jh5ylCu6wyuk2TMcJaEEhIusL 6lGr80vL6Ynq4Nl1H+WjN7bMttO+PsXKExnigMTsnBAKzf0xIsahQLZAAk7jHOZvgwCc bLRJJUujHca3L3Fsiz+w2UjwN1rkywBqAzzgN0aiUvLE+FMippNK/JRTIF9V0OBvZeLI VV4mTNvyxU2ybMbI+Y5fAoASpSX47kYyGQ0xFZjS3mLPbjerIjt3HyVqlc+FBxbsttX5 VZlA== X-Gm-Message-State: AOAM533QNsKo+qONgQrys5foHQlGMmS5RJ05+lu/XR1t+c7bw7vucRL8 Rrkmshc7va/URZry0taHGs6x1jc/WYg= X-Google-Smtp-Source: ABdhPJyoaBU921E/udNconIqyWPSa05rok4Y8oelZ6s7gDM+Zc+yOmqKEIsPGZBu4jmsNL2uv0G+zw== X-Received: by 2002:a7b:cb51:: with SMTP id v17mr1794412wmj.185.1640199397817; Wed, 22 Dec 2021 10:56:37 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w25sm5804672wmk.20.2021.12.22.10.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:37 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:21 +0000 Subject: [PATCH v5 01/16] reftable: fix OOB stack write in print functions Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Signed-off-by: Han-Wen Nienhuys --- reftable/record.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 6a5dac32dc6..8536bd03aa9 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -254,7 +254,7 @@ static void hex_format(char *dest, uint8_t *src, int hash_size) void reftable_ref_record_print(struct reftable_ref_record *ref, uint32_t hash_id) { - char hex[2 * GIT_SHA256_RAWSZ + 1] = { 0 }; /* BUG */ + char hex[GIT_MAX_HEXSZ + 1] = { 0 }; /* BUG */ printf("ref{%s(%" PRIu64 ") ", ref->refname, ref->update_index); switch (ref->value_type) { case REFTABLE_REF_SYMREF: @@ -586,7 +586,7 @@ static struct reftable_record_vtable reftable_obj_record_vtable = { void reftable_log_record_print(struct reftable_log_record *log, uint32_t hash_id) { - char hex[GIT_SHA256_RAWSZ + 1] = { 0 }; + char hex[GIT_MAX_HEXSZ + 1] = { 0 }; switch (log->value_type) { case REFTABLE_LOG_DELETION: From patchwork Wed Dec 22 18:56:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697125 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 2790DC433F5 for ; Wed, 22 Dec 2021 18:56:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344955AbhLVS4m (ORCPT ); Wed, 22 Dec 2021 13:56:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344946AbhLVS4k (ORCPT ); Wed, 22 Dec 2021 13:56:40 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 072D5C061574 for ; Wed, 22 Dec 2021 10:56:40 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id e5so6780427wrc.5 for ; Wed, 22 Dec 2021 10:56:39 -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=q2z1Id3icuATpEhRt5U6kI2GaIS74Y55B7P88uwdxC4=; b=Cd/geLOOuNz6+QnVCmr3lpvFOjKHbq8jonMW/gNEiU+7VhFsM+AdFCqiGgqGLrjEBQ VF20PvYRYCvt3je14I8qxFF1+mbxe3JAGCHEfiMsDCj+J+tVyWZiMyi2S5L9iRy2NSUa +jm+GGmBc3rr/0ViZKg1l28DYxxk0d17jn6+NqaaLTZLiRofXbKhckcyv8YOwHX/eGhe nUfUygol9sCSTXIBqD45NP0z65ZYPHGF2sPM9Ia7h2bWexGgs7i6JzhzCQlrz3UphMcp e/wykkoxFCYiFg0cVQ/J4h3L2+PRflbVbTeTvoJfknOD65nHgJlB/BbsC8tOn2owGnw2 3LVQ== 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=q2z1Id3icuATpEhRt5U6kI2GaIS74Y55B7P88uwdxC4=; b=VmajBk6Bku57eIOOALyHEdwmFdo/uShILMVC4DcM3uAEnUwTZ04M88zj5kqf98+j0d 9mkdKfdaptdSKE44BlJ+sBcAGBY7FsE7Vk+GeqvdqJQIYwzZxf4V1mlSpED60ZDCEgO7 sWHMRhbLvWpA9ShS+soPMj8S7xVtmjO7fdzWOg62UDIJTDhZCvHI00XI32kSRSI1Q/aH 0C8RZ1WhW8lKZoLAcoql14zdw0EXhzakUPA+WkQcXkvZ6QeAgUhr9LwXRk9ZWp1AjMt2 l9T4wsrUdJi88COknrpMtBNAE/zxGsm7tOxYDCub8MOlhL2dqnnF+r2QcULLE8TOmMZS RIzQ== X-Gm-Message-State: AOAM531J/rGDHRxfDrxdf9lURX54JR1lau2pAC8YBWO75Fv7xxbWIcGe g25XsxLHHi21DtStCTaW0lwkpd4eRc8= X-Google-Smtp-Source: ABdhPJzYCLa1A2j7cQnggyPA+WO95iw7KiZEy2fokxKdDs1au69EuzFoJ2tUVae7lIdOSDQiH7/GJg== X-Received: by 2002:a5d:44d2:: with SMTP id z18mr2920249wrr.23.1640199398466; Wed, 22 Dec 2021 10:56:38 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p15sm2398240wmj.46.2021.12.22.10.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:38 -0800 (PST) Message-Id: <9ab631a3b29addaa54415139e7f60a79a19a6edb.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:22 +0000 Subject: [PATCH v5 02/16] reftable: fix resource leak in block.c error path Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 | 24 ++++++++------ reftable/readwrite_test.c | 66 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 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..0d16b098f5e 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -290,28 +290,34 @@ 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: + if (err) + 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 5f6bcc2f775..6e88182a83a 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -254,6 +254,71 @@ static void test_log_write_read(void) reader_close(&rd); } +static void test_log_zlib_corruption(void) +{ + struct reftable_write_options opts = { + .block_size = 256, + }; + struct reftable_iterator it = { 0 }; + struct reftable_reader rd = { 0 }; + struct reftable_block_source source = { 0 }; + struct strbuf buf = STRBUF_INIT; + struct reftable_writer *w = + reftable_new_writer(&strbuf_add_void, &buf, &opts); + const struct reftable_stats *stats = NULL; + uint8_t hash1[GIT_SHA1_RAWSZ] = { 1 }; + uint8_t hash2[GIT_SHA1_RAWSZ] = { 2 }; + char message[100] = { 0 }; + int err, i, n; + + struct reftable_log_record log = { + .refname = "refname", + .value_type = REFTABLE_LOG_UPDATE, + .value = { + .update = { + .new_hash = hash1, + .old_hash = hash2, + .name = "My Name", + .email = "myname@invalid", + .message = message, + }, + }, + }; + + for (i = 0; i < sizeof(message)-1; i++) + message[i] = (uint8_t)(rand() % 64 + ' '); + + reftable_writer_set_limits(w, 1, 1); + + err = reftable_writer_add_log(w, &log); + EXPECT_ERR(err); + + n = reftable_writer_close(w); + EXPECT(n == 0); + + stats = writer_stats(w); + EXPECT(stats->log_stats.blocks > 0); + reftable_writer_free(w); + w = NULL; + + /* corrupt the data. */ + buf.buf[50] ^= 0x99; + + block_source_from_strbuf(&source, &buf); + + err = init_reader(&rd, &source, "file.log"); + EXPECT_ERR(err); + + err = reftable_reader_seek_log(&rd, &it, "refname"); + EXPECT(err == REFTABLE_ZLIB_ERROR); + + reftable_iterator_destroy(&it); + + /* cleanup. */ + strbuf_release(&buf); + reader_close(&rd); +} + static void test_table_read_write_sequential(void) { char **names; @@ -633,6 +698,7 @@ static void test_corrupt_table(void) int readwrite_test_main(int argc, const char *argv[]) { + RUN_TEST(test_log_zlib_corruption); RUN_TEST(test_corrupt_table); RUN_TEST(test_corrupt_table_empty); RUN_TEST(test_log_write_read); From patchwork Wed Dec 22 18:56:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697126 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 AA810C433FE for ; Wed, 22 Dec 2021 18:56:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344967AbhLVS4p (ORCPT ); Wed, 22 Dec 2021 13:56:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344943AbhLVS4l (ORCPT ); Wed, 22 Dec 2021 13:56:41 -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 CC50FC061574 for ; Wed, 22 Dec 2021 10:56:40 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id g132so2225343wmg.2 for ; Wed, 22 Dec 2021 10:56:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=zeOacPG8foepzoe93AsPL3Dhk+VXAN8S9blbgW2/nvM=; b=XUFZwh/YyEx+ltkjDdG9nRFaq8aKNpCbiUEI8SEfHh5oYtB++5D8BWygKuo5FfrnSz 3XiCQSOc5yBSrLQGopqMyRdVa1PBI3Li2+sA2r59Fq49Gq02AcVU+lfF6zzY7pC9vFxQ +JubB4Eaao6kuOoqbO2dLXdIiQ2qkc0YZPLH/Mj1SRChiQEewwYI9gthFBe9bskAcuKa SS0fIPFUvwP3fnrnOpdQrgubZIamXeJrnsTnNwE2O73F6WVHUrcMvTLo9ucf7Mj27hIF wOA0Ju7IGMEJ7UUO7E/I4UpnwN+qo5cE+zQ5xoRdGijFcEOrCSIdV8mLc0wFN+Ui/eNC XuLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=zeOacPG8foepzoe93AsPL3Dhk+VXAN8S9blbgW2/nvM=; b=jC5Kp4ravNRaQsybfepiHFQM85XuPWPCd5XZkQkqnEmcVKKwtg2SHDOlrJ/06GdqDh HuzHiHLJl7VbxZo6LNhPogeYACYuBiFFUeVSa3U3K11dO3LuLIs9Yel1WmKIQ3Xrix4Q OR9UYew3NBAFjerXELGVBihNFJSBFrFOWYRXTaH5PvCepyp5BCbM6FmRAo9I4RJLVGQY 6n4/Gq5jt/dhNqVaYpeel4DKMpXbFS/adJs3P46kXZfFaRl/oZSF0or1bbftBVAGMK17 irxpvEt4K7InkoaZFFI9T/p2BTE87kxWWpY/r0vHMQuGo4ses6z8zeYLOJMabesTBRJy BMZA== X-Gm-Message-State: AOAM5303jCVpy8ZNIDv2c3ZX2PxCuTmOc5ypVW/GwEARvIuyELqG971f G+JY1GylSmMg3bW/L+VuBSU5AXADcLo= X-Google-Smtp-Source: ABdhPJxopQ8987cCMk0JSnO0x9X9OoO2++Z8pJsjSuYZMVqsFC3kolnCO3+FZ4ksR6FQCJ1WDBT+hQ== X-Received: by 2002:a05:600c:4f48:: with SMTP id m8mr1868766wmq.158.1640199399316; Wed, 22 Dec 2021 10:56:39 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id h204sm2748329wmh.33.2021.12.22.10.56.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:38 -0800 (PST) Message-Id: <8301000e83b2f9758087bb741aeac0e6238ad343.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:23 +0000 Subject: [PATCH v5 03/16] reftable: fix resource leak blocksource.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This would be triggered in the unlikely event of fstat() failing on an opened file. Signed-off-by: Han-Wen Nienhuys --- reftable/blocksource.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/reftable/blocksource.c b/reftable/blocksource.c index 0044eecd9aa..2605371c28d 100644 --- a/reftable/blocksource.c +++ b/reftable/blocksource.c @@ -134,8 +134,10 @@ int reftable_block_source_from_file(struct reftable_block_source *bs, } err = fstat(fd, &st); - if (err < 0) - return -1; + if (err < 0) { + close(fd); + return REFTABLE_IO_ERROR; + } p = reftable_calloc(sizeof(struct file_block_source)); p->size = st.st_size; From patchwork Wed Dec 22 18:56:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697127 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 60CAAC433EF for ; Wed, 22 Dec 2021 18:56:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344972AbhLVS4p (ORCPT ); Wed, 22 Dec 2021 13:56:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344951AbhLVS4l (ORCPT ); Wed, 22 Dec 2021 13:56:41 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ED41C06173F for ; Wed, 22 Dec 2021 10:56:41 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id v11so6733716wrw.10 for ; Wed, 22 Dec 2021 10:56:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=6v0+JuvgL+rkV6+ZUY0aCiH0eSlBA7tjh6rJWXdXHEw=; b=o7THXJk0bGUQ7vXKQPrp/bTH3Wn/bWknF01Vl9ctpJtg8NPtHk6mD6XpXqGV3CpdFy sPUXizPa4HT5fggAc/HhNOWp5gNa1I0oUoRcta1cll8wz8fWM4dURHQjWpfIZEm7ndDk 3PW7o2jh1k5p6U48Bu6+c8bc3L0iOdMXC8b2asrHl+HiLGIm9VMZDDlGNe5+nq4B5mrU OH8cYhI2isowXBtnYeG3cyUVwm//kFl3H9dztdscfnbnoH+hIemionx7E+H86E8WCYte ye/ofxvDa1m3qjbe2XRPR+XKjwWGct+Fn5KFOg3gUOw5PJJz6ZOHUMsZz4VlXwS9gTBs SFHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=6v0+JuvgL+rkV6+ZUY0aCiH0eSlBA7tjh6rJWXdXHEw=; b=WyG1hpoF9x84kfZln6VPtYlEI0D38UyXxfLVqN6cY4TWXhvBx7zSRg2VzhyrpQa1LT Cjr38lRViuONeflm8IZANJcDH7p7lyQgmDl56KBe9JtDi/inSRB082Xf6gpQaM//jNkS xtE5Lm/rPuwn3Fo/5OcfvVUDhtuuLzYzi7QFOQKDOcWsUykEhqSIUZ3f4I5kM3zea7zd 5M7vxUENVp2vKPqQLvDuT/sQ/cYYA1CJNC9NJxr9JTVO5rIJGDAQxSji0s7vQU1Wn3IE fhSyH99AB6kBSZIsettHH2kO6FkomtVhOF5D0R8KHwfjsx7Ail542n70H33HLNET31I4 EBjw== X-Gm-Message-State: AOAM530zfOWvONKhqzzyUYzCobgFqt8ki9KBofcd1+vcfPn080O7aimt qkCN29wT5Qd4x6AjajZ8KpyEDdCRh3I= X-Google-Smtp-Source: ABdhPJwVYlYdN7sCX0IVHlDFfXLLIKmTcmwY+LPwWbtj4R/VHvQY5HP34PZbwnxaVgrqXwHXvFxOIg== X-Received: by 2002:a5d:460d:: with SMTP id t13mr3107018wrq.44.1640199399893; Wed, 22 Dec 2021 10:56:39 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u20sm4476336wml.45.2021.12.22.10.56.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:39 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:24 +0000 Subject: [PATCH v5 04/16] reftable: check reftable_stack_auto_compact() return value Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Fixes a problem detected by Coverity. Signed-off-by: Han-Wen Nienhuys --- reftable/stack_test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/reftable/stack_test.c b/reftable/stack_test.c index eb0b7228b0c..d628420e63a 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -814,6 +814,7 @@ static void test_reftable_stack_auto_compaction(void) EXPECT_ERR(err); err = reftable_stack_auto_compact(st); + EXPECT_ERR(err); EXPECT(i < 3 || st->merged->stack_len < 2 * fastlog2(i)); } From patchwork Wed Dec 22 18:56:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697128 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 75330C433F5 for ; Wed, 22 Dec 2021 18:56:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344965AbhLVS4q (ORCPT ); Wed, 22 Dec 2021 13:56:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344954AbhLVS4m (ORCPT ); Wed, 22 Dec 2021 13:56:42 -0500 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16BBEC061401 for ; Wed, 22 Dec 2021 10:56:42 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id p1-20020a1c7401000000b00345c2d068bdso1843124wmc.3 for ; Wed, 22 Dec 2021 10:56:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=J9jVqwMPaNdDSNjujdVEAD8serufDJ5UG7qKldS2NTo=; b=dogplk6Pse60y+ZAKHh8ph6DXETtF0jwW5j1UHqA/2ISjoL+aa+B1BJfTj6friIvtV v0Ar7r/ruF9rpxgPHvgXgxG7IKRFcXT+8ozPKZ0m3vs3G5fwGT0VtBnYZhetT57OWbfn RNP37jSrFey/4UKgtGJyFuzPEN+qpJuqH6fl1ee8xO4g8iR4Biuid5RVHQsemFWkjqW/ ldCP+TZvhsjIXTqr07slMX8bc6GabiedFIHjGf5vWj18AvCnwo1bulq5jKMkHtL0ZZR7 dEUMGvGVrFsSfE2GRtHZvnDPRXPOCIeFdLtojqdPqB7h2rr4g1/Hahg4W4e1EtHHvORB b3Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=J9jVqwMPaNdDSNjujdVEAD8serufDJ5UG7qKldS2NTo=; b=PL81oZE7toYaQ/LgDMEvQA6iBO3P9VNwJzO6SirOo+FOTQ+kva/w2pKUjxAs/3gjwf 92RsoiX8J+50hOv/y+gBxuZEfWnS7gkFBRBdzmQOaVo+a781/D4ZzvQgswLpjRKNgclW uNU0N+Qm4QHZYQVn60Vrl89BIVLsLe39C4bfCqWrTWl+eXwZbrlo8GIZzyrjCS5hyf2q qD8YKvtHfGhil6XnGwaFzeeHHT4JywpfWhCG4mBp3VJJhtiAM9wEPy9rCe6iEORWm3vQ F4EwsqovA9c4CwNwtR6Brfv0wmEVirF80OuBh+sVImAYHSQJo/nwvTbEn1JkUQWc2uFZ sm6A== X-Gm-Message-State: AOAM5311jGvtwOH2oNFMb6wwJOzSvNcLyKZXKIPl47frRS32lABBkM9V 7Tmso+pIF+XfSbJbOWVEXD50zGnxk5U= X-Google-Smtp-Source: ABdhPJyGvWht0Fp+XPyQcQsfapoJejCSZqwRYYZlKWb+C603V6/+JBXZXUu8FKAmLehOysd28TaYpA== X-Received: by 2002:a05:600c:3783:: with SMTP id o3mr1831409wmr.78.1640199400527; Wed, 22 Dec 2021 10:56:40 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b10sm2689603wrg.19.2021.12.22.10.56.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:40 -0800 (PST) Message-Id: <7551bcdd9175d0e7a0bdc60ad0fed7310976e02d.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:25 +0000 Subject: [PATCH v5 05/16] reftable: ignore remove() return value in stack_test.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys If the cleanup fails, there is nothing we can do. Signed-off-by: Han-Wen Nienhuys --- reftable/stack_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reftable/stack_test.c b/reftable/stack_test.c index d628420e63a..4b7292945c3 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -89,7 +89,7 @@ static void test_read_file(void) EXPECT(0 == strcmp(want[i], names[i])); } free_names(names); - remove(fn); + (void) remove(fn); } static void test_parse_names(void) From patchwork Wed Dec 22 18:56:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697129 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 BA743C433EF for ; Wed, 22 Dec 2021 18:56:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344973AbhLVS4r (ORCPT ); Wed, 22 Dec 2021 13:56:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344956AbhLVS4n (ORCPT ); Wed, 22 Dec 2021 13:56:43 -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 9E452C061746 for ; Wed, 22 Dec 2021 10:56:42 -0800 (PST) Received: by mail-wm1-x335.google.com with SMTP id o19-20020a1c7513000000b0033a93202467so2025221wmc.2 for ; Wed, 22 Dec 2021 10:56:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=HOTpe9FNENotMYcACPCrU+33ZDetlyOQNtsGAWihsVI=; b=VHh+PrdOWOitvscP3p3PMgSnQKG/MIAsePygCMSQ9pOvEV0oWYFfXrE/PhH24IHQqS RcnPPa8UyzmA0BxfiMpWBbTpaVoxVYCxy3uvyF5i8+CMZz8nVjxvdtk9Dc4iQptdxvX3 FwJD8M4Jq5JY3V3rNuTK88b7Hc5IB3L2vA2zRQ88Z7r8S5gb/aYARVHV/vWf9P8AoN/V hBTR4pdLD9mOwZfYLKPm/r/h8d74eYm6MgrSDo1FpwXvuPA2GshPOPeV03pEcWwod7Dy CqnmudwAOYlr6NzjsnueRSjxUv2IfiOeRw4og8Dz7l6GkBQYD2zfQNFoHHw1gxGrwG7P ADTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=HOTpe9FNENotMYcACPCrU+33ZDetlyOQNtsGAWihsVI=; b=hsj2GKZayY/R1+IfYYsi/IouFyVr9c0eFoTosEsNmudylMibgrv056/oFH/Cv/Fjct Bma/GMnXIOARno/1QuD73W1DPsvgRXpedL5NsVEL299wxHwSKrL6O7FQtlMF32/MMBIV JmlURehqg8RD7gQvpX+3AiPeOMpX/VvPV1EvSh2MSJnBoAqsPAt3Jtd1/kIGi6dNujeq hVjUTgC5gjhi+RQHy93FPIrakHzCDLSyimqQNbROJs70bT6EjR3/v6Di89+jALlvDTgd l1yaOZ9RDCWV3lLdCr5U4dtaggygkFVEx0MOIqOdNKi+SnMRSzmz5YHSFtPg+T/CC90f TxpA== X-Gm-Message-State: AOAM533nznZmT4KGvBD9Xwq+JbWxe8R4PWJvkmvWyB9TVoANwBEVxTA2 Hl8pBUvktVbhGNLxRMP4Pxvbe5BjRv0= X-Google-Smtp-Source: ABdhPJxoeSVJbUPyn7WiwQkE2DQUuPJs1Sk8E7ux84aeEItDzSoz44KmyvIs+TIgdZPEejm/uCJQUQ== X-Received: by 2002:a1c:e909:: with SMTP id q9mr1768854wmc.184.1640199401132; Wed, 22 Dec 2021 10:56:41 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u20sm4476388wml.45.2021.12.22.10.56.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:40 -0800 (PST) Message-Id: <700a4e247e7e1deb19358960bbb75ebbde88ddbd.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:26 +0000 Subject: [PATCH v5 06/16] reftable: fix resource warning Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This would trigger in the unlikely event that we are compacting, and the next available file handle is 0. Signed-off-by: Han-Wen Nienhuys --- reftable/stack.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index df5021ebf08..10dfd370e8e 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -877,7 +877,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, struct strbuf new_table_path = STRBUF_INIT; int err = 0; int have_lock = 0; - int lock_file_fd = 0; + int lock_file_fd = -1; int compact_count = last - first + 1; char **listp = NULL; char **delete_on_success = @@ -911,7 +911,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, } /* Don't want to write to the lock for now. */ close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; have_lock = 1; err = stack_uptodate(st); @@ -932,7 +932,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, sublock_file_fd = open(subtab_lock.buf, O_EXCL | O_CREAT | O_WRONLY, 0644); - if (sublock_file_fd > 0) { + if (sublock_file_fd >= 0) { close(sublock_file_fd); } else if (sublock_file_fd < 0) { if (errno == EEXIST) { @@ -1013,7 +1013,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last, goto done; } err = close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; if (err < 0) { err = REFTABLE_IO_ERROR; unlink(new_table_path.buf); @@ -1050,9 +1050,9 @@ done: listp++; } free_names(subtable_locks); - if (lock_file_fd > 0) { + if (lock_file_fd >= 0) { close(lock_file_fd); - lock_file_fd = 0; + lock_file_fd = -1; } if (have_lock) { unlink(lock_file_name.buf); From patchwork Wed Dec 22 18:56:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697131 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 D90C9C433FE for ; Wed, 22 Dec 2021 18:56:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345000AbhLVS4z (ORCPT ); Wed, 22 Dec 2021 13:56:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344946AbhLVS4n (ORCPT ); Wed, 22 Dec 2021 13:56:43 -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 5356FC061747 for ; Wed, 22 Dec 2021 10:56:43 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id g7-20020a7bc4c7000000b00345c4bb365aso1626591wmk.4 for ; Wed, 22 Dec 2021 10:56:43 -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=+ktIBxUaPIKKFrC2R2WjpGto/EjpmGRCAsIvpDAcqcQ=; b=G8IJhxr4MkNQLQJmSEyPdrTlwuxdX8M5ZLXzmn540tZ1/st0rA6amIhIb10DblI7uz 6t4w2aQ0oM5xFfz5MiTAGMYVScmZnH7irQ6SiXO6QOw25tEr/Adg1M8WrOmOL5MDARKE JkNsh8smapsfPUmgkxyiZo41kD5fx/VmGcXTpFZpGTluT0a+A2NYuvo64u5BDRkBbcZR zeIkV3U88YpvusQ2DQ9dfEWQflR0zfnBx/upuqvw6lXvS0PUenocEf0JdI9JD8TpPHcF K0NhtmK1uuxsMDK+eW50kqPfH0PVqEzFz25ncozORYUxkr1EW5eCR8cdTy+K11CixP5q lKnQ== 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=+ktIBxUaPIKKFrC2R2WjpGto/EjpmGRCAsIvpDAcqcQ=; b=tIOFR7sxVX7BR1NFmkDuI+qGsCuQ3XgcynqBhuCiRBsC4TbkabdUUOiTnaaNNZIM5k ph3EvU+yYaQhfeh8AqeePxdGY6HL4T4HxhPk1OmVERZADfi+0bfuoxxk8a9JTUXoU7uu 2TnL2r8yVTpwFQy3U6xBWySeHeJZudjq/Rt4MwBiA9HZpS0WhWVVJwtlNkID7lrkES2D afTlQltOw8phyzxJE3thwiNI8T0yZ8nfdbunQSGSD1Ms4ECA+NWh6S9bCLLq2ikYNIoi ZWtxuFczxwxgwW2dPKZXBm8GdvqEc2M5IoGDpBnjorYGao8TJJcHNArznANgYr9P4Rvb 1FEw== X-Gm-Message-State: AOAM530vxKIcpMwyIDC7nu3mvtm0pnu3rrV7TGar+2+4QtHzW4usE3db 02y9OmcZP0462ZT+SAVSjW0DzojyY2E= X-Google-Smtp-Source: ABdhPJxySPZht14xh6zDRCirA5HBUCATk68XU4xv9+7v40ftv6F7Gdc3CQZGhn0duaqN1e2099kvIg== X-Received: by 2002:a1c:9acf:: with SMTP id c198mr1818456wme.117.1640199401759; Wed, 22 Dec 2021 10:56:41 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id z6sm3206062wmp.9.2021.12.22.10.56.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:41 -0800 (PST) Message-Id: <30016fee4c85c0a803017070d28b3ccf2111ee91.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:27 +0000 Subject: [PATCH v5 07/16] reftable: all xxx_free() functions accept NULL arguments Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 0d16b098f5e..c7a32331c76 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -647,6 +647,8 @@ int reftable_new_reader(struct reftable_reader **p, void reftable_reader_free(struct reftable_reader *r) { + if (!r) + return; reader_close(r); reftable_free(r); } diff --git a/reftable/writer.c b/reftable/writer.c index 3ca721e9f64..9fd24fa93ce 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -150,6 +150,8 @@ void reftable_writer_set_limits(struct reftable_writer *w, uint64_t min, void reftable_writer_free(struct reftable_writer *w) { + if (!w) + return; reftable_free(w->block); reftable_free(w); } From patchwork Wed Dec 22 18:56:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697130 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 D399FC433EF for ; Wed, 22 Dec 2021 18:56:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344990AbhLVS4w (ORCPT ); Wed, 22 Dec 2021 13:56:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344960AbhLVS4o (ORCPT ); Wed, 22 Dec 2021 13:56:44 -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 F0EA2C061756 for ; Wed, 22 Dec 2021 10:56:43 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id f134-20020a1c1f8c000000b00345c05bc12dso2020567wmf.3 for ; Wed, 22 Dec 2021 10:56:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=7hrVqwDEsCy4jvGx2ygf2ufkTVUbjR+/cx/GyTSeOVg=; b=PooCSs4+UhraBHChon8vgD80jAgb27qqJI6/jon/r6xuB/OXAylmOKdFCGDZeTn7MP 2SUOjgHQH5PewZqjCNETVq98E3s1VoAfRFLP0/+n/GQXO1pj6Nv+G/Z6k4H1T8OY4q31 RhKnIPYxUvi6GGwTzOm1o/ue6I4WeYmjOnzsSfSWYX0HCOrGDqauYRCpQSULtqOCJ6oQ 1JJBYEUnsYfEVNnzCRgSq1rkbXnw4LMAjq6Mo+RF541MxwMqQ7HKwJ5lWrHZ/APpr/zv 4l/3J96HNKrK917caeyqwO+G2OOXlZ6bIKaP2mbqpfVzh9mLt9ZmERz8lGXYtCExcZH5 nVAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=7hrVqwDEsCy4jvGx2ygf2ufkTVUbjR+/cx/GyTSeOVg=; b=h8r29F7uINRlXutxBHvomHtNzZe+NqYSdg+f+fgraA/jOHGT2HYongFvTZWv14PQ/J Q1KOkpk6ezcn3p07Y0q8rZNHE9fXECCY9+ZwxG4YY+KKh78ytZOyU3L2hBnHpxL83TH0 fGGiKcqnoHtJnTOCHnAPoJvnILsOwYeQS975e9Z9bDXh1/G/vj21vHzkyK2zuObyWDP9 oPTPXjYUrxSeVK7zAVLSFFCGLp98SJQ3SIijOvQO5g+X1gKdNmo0+DOMjSLRkfjKEUls hg8xx3tZrVsRYfqs3IQ+vLnotvYrnL40i7SeQBzz/5WewmMKjw/mJWxMTs2MJ6wiyu2f 5ZsA== X-Gm-Message-State: AOAM530eHiwkjXJxIqZscQMaK3GXVMEIJNJHeV5KfCbf2kYrEnmaBJOX u3JIRDfD5kQzP97j82AdElOuDyBWbfM= X-Google-Smtp-Source: ABdhPJwMEc6iibsI/Ez8FKx6bY/nwTUrvw4TNJHHYrHALDOIWzS7lpAPr8m215BfHaNt4MZ8hYCyfQ== X-Received: by 2002:a05:600c:4e91:: with SMTP id f17mr1808165wmq.195.1640199402402; Wed, 22 Dec 2021 10:56:42 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y13sm3092564wrd.89.2021.12.22.10.56.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:42 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:28 +0000 Subject: [PATCH v5 08/16] reftable: order unittests by complexity Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 Wed Dec 22 18:56:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697132 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 DC4D0C433F5 for ; Wed, 22 Dec 2021 18:56:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344991AbhLVS44 (ORCPT ); Wed, 22 Dec 2021 13:56:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344966AbhLVS4p (ORCPT ); Wed, 22 Dec 2021 13:56:45 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9597EC06173F for ; Wed, 22 Dec 2021 10:56:44 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id f134-20020a1c1f8c000000b00345c05bc12dso2020592wmf.3 for ; Wed, 22 Dec 2021 10:56:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=S929WtbLycx8tthlebgwee0eZnsSzF55QNgcBtMV46g=; b=Dtj26eDLjkRVNKQjBPVG4jhMmfMkttqoQRbB37DyZPysw9WyrYXXYbMU+dUpEV6RAo Y6sWCYxH5kEBGOeXysIUOlcdDKZKS3uqsx1EsbwHN8mYDi0MoR4Qw9h7VEu/9adbB3MU sqwMofMC82DorEQnmawctQMedHKvscPibxJoa/HDWwaQGpEoYOkuB4LgHizMtVrBI2T+ rr9HQQrOfqx5FcMMOg2iQbCCmRXCn2pIK58QCajIJsXNZGdc8tdX6zU/L+tCXPKHLEq1 7ySdxPk6c6lUDiqKUxpxtjq1x1KdaDB248JeozKCuvCJO7m5I4OApJFpsWcqHgiO2t3T Joew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=S929WtbLycx8tthlebgwee0eZnsSzF55QNgcBtMV46g=; b=upVxemxdrXQ+WC/O6s6nP/3G9MrYSbF2HOzFoDN5a9bOOfJ5xGn6BR4j/Rre7OrMYK ZdDhIIrBQJN+DlMXCDXdpEjNhixSxY+SZPTAtwLY6H7GJZA7G8RcXgydF34hL6V4U03O DKQuolkI7kVfvyQHGdDQNlfLtMowZiu3l4SmXhfz6a+gWKNBR8s2eiTzMd1eV88Zey/C HyXdNSPVfcnzOQM+3rSd2UPCdrA3ImpLw+Mlg6CTVzo5IOsG5f4ZzUyLFNGjBI8XJdr0 PsPztIDNdou6tuvXML8xv+fIyLJ80feo+aodBI4THfAhEDwDq27x4KjsDO74vT3QunNN rXZw== X-Gm-Message-State: AOAM533x80s8Dv5DcKUjOB/tL/VOnC/G7LK98q3dbbjcPatjWDrrtELZ rxGwSjcNKv/7jC38cMsis9EzS32zezg= X-Google-Smtp-Source: ABdhPJyh+2bNQIHnlswLX+o7j64k4lTJM0w8yp454XreYnJ6qWUZcyr+r6RYtQP+uKFXra9yP++Uvw== X-Received: by 2002:a1c:1903:: with SMTP id 3mr1797073wmz.89.1640199403104; Wed, 22 Dec 2021 10:56:43 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d2sm2587519wra.61.2021.12.22.10.56.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:42 -0800 (PST) Message-Id: <425e12d3667754a152b32a75cad435aad6a06148.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:29 +0000 Subject: [PATCH v5 09/16] reftable: drop stray printf in readwrite_test Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Signed-off-by: Han-Wen Nienhuys --- reftable/readwrite_test.c | 1 - 1 file changed, 1 deletion(-) diff --git a/reftable/readwrite_test.c b/reftable/readwrite_test.c index 6e88182a83a..9b89a5da103 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -662,7 +662,6 @@ static void test_write_key_order(void) err = reftable_writer_add_ref(w, &refs[0]); EXPECT_ERR(err); err = reftable_writer_add_ref(w, &refs[1]); - printf("%d\n", err); EXPECT(err == REFTABLE_API_ERROR); reftable_writer_close(w); reftable_writer_free(w); From patchwork Wed Dec 22 18:56:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697133 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 7B893C433EF for ; Wed, 22 Dec 2021 18:56:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344985AbhLVS44 (ORCPT ); Wed, 22 Dec 2021 13:56:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344971AbhLVS4p (ORCPT ); Wed, 22 Dec 2021 13:56:45 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43B14C061746 for ; Wed, 22 Dec 2021 10:56:45 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id j18so6814207wrd.2 for ; Wed, 22 Dec 2021 10:56:45 -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=kavocR3fv8eVSjDl+KDN0Mg9YMhYqjlMaOhkncK/LU4Tbl/Z8G9tYwO7w2B6vp0PHW XCQX68C/Pdu2sfrWXYfVm6GGWiTq2k/902TRWb3yepj9iNG5MnJ2ej0PxWgC/V5jPqy3 x8pot8KXJDpA9o5cP3He3wbWeiI713d15i/C4ZKUt21+henLwRLTJAmNyWvKPV3s9lbR 9uFsHsdpoCeUrQ3LuEseiKyB9XYGLzl6NPaGRhGj/RUqXx8GQ5BpQGsTJKGLhCqg/8Qt FRAAnazp0x7c19BnOS6WfHyPYtUdtmvbRMlDw94lICS5fjjamg9L40HiVgq4kQlkOdnQ 7cNA== 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=RyAQ+Fh4O+LT3thLi4gDAIs01OK7OyBMEvr5U3LEJsYQUXEVxt55e4E25X7SXgjk2F 0qrt3Fh076iK8UYJzge/AQorCacYLWjUOGyIKgEuvKITnL5byv5i3nXJiYu8CQqkPSUh A6JutXR7HivKqEHOehaM3SccMll3CONLCw6TQjg4AZ0VSpdI+Nc28T8YLB6jGibFj4IX bpNuceHH4G1DTR+a1SqvQRtSVOICq8y/UU6KKQ8w/PQM8RMBQ0Ew1NzbS15n2j7M5gpy QoNePNYkR1mu7eTczZjstvJUd2VKOcDhLGIDmHONatAAU/ct0euHUJ5m+Ryeh+IPR8bO eZ0w== X-Gm-Message-State: AOAM533K/hTq6hEHCPd4dSfsbm92+vDEbETI8LJadPct2NkakHM+EYsZ RYtenBYk47E5hfrxe6xabTmj53o8ZF0= X-Google-Smtp-Source: ABdhPJw702OfRU1pjFE2qSIBy9wKpXKTSecjd8FnQ0qOV/DZ4SZUHs7IP8hOSMy5kvmFfhZP/VhIag== X-Received: by 2002:adf:f4ca:: with SMTP id h10mr2988627wrp.512.1640199403703; Wed, 22 Dec 2021 10:56:43 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b2sm3441780wrd.35.2021.12.22.10.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:43 -0800 (PST) Message-Id: <67d858ec59e1c62d5e227e85e117b3cfcb3dda2a.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:30 +0000 Subject: [PATCH v5 10/16] reftable: handle null refnames in reftable_ref_record_equal Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Spotted by Coverity. Signed-off-by: Han-Wen Nienhuys --- reftable/record.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 8536bd03aa9..8bbcbff1e69 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -1154,9 +1154,11 @@ int reftable_ref_record_equal(struct reftable_ref_record *a, struct reftable_ref_record *b, int hash_size) { assert(hash_size > 0); - if (!(0 == strcmp(a->refname, b->refname) && - a->update_index == b->update_index && - a->value_type == b->value_type)) + if (!null_streq(a->refname, b->refname)) + return 0; + + if (a->update_index != b->update_index || + a->value_type != b->value_type) return 0; switch (a->value_type) { From patchwork Wed Dec 22 18:56:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697134 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 F3F77C433FE for ; Wed, 22 Dec 2021 18:56:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344992AbhLVS46 (ORCPT ); Wed, 22 Dec 2021 13:56:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344951AbhLVS4q (ORCPT ); Wed, 22 Dec 2021 13:56:46 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA1AFC061574 for ; Wed, 22 Dec 2021 10:56:45 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id q16so6769515wrg.7 for ; Wed, 22 Dec 2021 10:56:45 -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=AlJ4s6VuUIBw28gTq8Z491wrrMbH2sSM6EP3y6gVVMTFcIWOV9oMuyF1hfdXGu84Ix yve3q+QScKuUTdEXMjoImo1zTcxash3yvWvBdv2zYLAJnmtDAi6mBWy0cEnTjcXpPRcd u/CMsjbjNYEUb58mnmBHrfmY0XU8X3SXQDbx9AM8oaCk0E0xbCOKUjPA89NLy09/izNW xaqAazF4Tfp1DS2+qLuafWllIDaWhEP9Kx5hQ2oWYklkSlyBmNi/Intrgq4+IWXosG8F aLCJz3J3mvxoSl8yy3kpav/PnJHO7aWrYWjH6IBe1giQjS3N3JqBDiWoJVeheimIBN7O kudg== 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=timXKxQTOvKw5OquYpvdIumF+qFsc3TyDGaBoS+5tKVEclrk0FElezwlhXMaXPudq8 7NWWn+C2DfNTVHFlmopPhhhiM/Y8GCPTHnDCmywRI6UJSi/v0egLNLeB3MS9kNfOZWW5 Xa71fT8p2Zk/7mtn649XtJNOZiqGVpqwxP4PSQeJeaOJV2TbkBe3sQOqPOcgXN0hSfTZ P9czqT85FRK/l+Ns+0OEhMUKZLIic0htTgT2lYSw2EUE1W18VcRPrXy7D5kYGJAt3FXB ounU6Fmo1A6S9QDSl5rffBfTYKGZmZ+XpvykG3XYpeV5e6oAO/F0Je2Y+mQi0urZBAk6 J0Xg== X-Gm-Message-State: AOAM532ROHI674cwdTCfSBLhYcnpoExBHNFoDkRd0iVXEIDlMCSNASKi F5kGoO1UmN5hy03jdWBhGldmJIWMK38= X-Google-Smtp-Source: ABdhPJzKQHcaK73xa4wmw+Y5lOr5OJd15SgmIKFKonEQG/eJqNojUzfvHTE1wUs3CJia2q32SK8m8Q== X-Received: by 2002:a5d:46c1:: with SMTP id g1mr2947660wrs.554.1640199404377; Wed, 22 Dec 2021 10:56:44 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c187sm5904064wme.33.2021.12.22.10.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:44 -0800 (PST) Message-Id: <1e50924894fcc07ee74a88a8b9f44547db01a3df.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:31 +0000 Subject: [PATCH v5 11/16] reftable: make reftable-record.h function signatures const correct Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 Wed Dec 22 18:56:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697135 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 A77B9C433F5 for ; Wed, 22 Dec 2021 18:56:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345014AbhLVS47 (ORCPT ); Wed, 22 Dec 2021 13:56:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344959AbhLVS4r (ORCPT ); Wed, 22 Dec 2021 13:56:47 -0500 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 862A6C061574 for ; Wed, 22 Dec 2021 10:56:46 -0800 (PST) Received: by mail-wr1-x42a.google.com with SMTP id j18so6814263wrd.2 for ; Wed, 22 Dec 2021 10:56:46 -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=o0PBrs+VhHpw0ZMchb3trUY88rjSWUXHs0jR7iXZBnPL93MdSkIwOPmkuOinKsqlqS Jyso2ZtjeSTj0EIcqT4U1nyvOVWGqOYpQEHcUOWqTVM/2fu9g5zrLXmcIHfahI59k1kR csEQHa/gLEA1T2lO8aJJrJV/uFthtJav5+w1SIz2mTCEC4FBF2gzIX+rdcXt2jLNrEXt axcsn/FbdekmprwtAlPH5RzXkKFe1Rfq0q+ITt4d8ZSy1diZjbi3oO5PrxIFGgo3hWEX 05AleM+5kn3bhxeRJ2SBl/PCBjnf1JTVhqXGK0AgxpCwK9nqAMCvhFwhyf37ZeNcZhx8 xD9Q== 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=LnE2ovX+C0c6mT48sJ8pAkBH3E2kD+Q9MyjgqLsXoCuYAikZIhKFOPTwfLfHrxQwSg U4dr/NKXzEIRIiTfeMdl1Axnm1iW1a2shiOlO2MjnAGUfxRL41GGauj9RMRSzOw59hVn nXAuf2KxtJj5mTplwojwDMdGibaX3LNPtQ4GUNI1h3sx32nveuYcKwC1gcKrWyn1SbE2 NQI4EezxqLV1NJU9dt0mEBnv71EQciTL8kgeZYqGDFwnOGbj9pNdTXbkhJz4tJVSlPCO aBZtBRF5eT4lJG4hhZWidekQfpiWOMg+V6knKKN/brZw6Mb7cSGmX3hvXFBF8SOq2r5P oucQ== X-Gm-Message-State: AOAM531oDueDtdAsgHGqqKuyWSIqeR1xiqSvgh+V2/SKaBWGout1+Nrf kXjUTiRdrDqUn+aS+gimS4EdBKW0Oro= X-Google-Smtp-Source: ABdhPJxJ4WdIVWG5+e+9i7m9RBSZ5kEbPVDiT8mirFTQ4ADyF81Irqt+HGm8mu6UK086iOlSyXMzVg== X-Received: by 2002:a5d:51d2:: with SMTP id n18mr2911481wrv.83.1640199404975; Wed, 22 Dec 2021 10:56:44 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y8sm3104699wrd.10.2021.12.22.10.56.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:44 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:32 +0000 Subject: [PATCH v5 12/16] reftable: implement record equality generically Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 Wed Dec 22 18:56:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697136 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 3E0B0C433F5 for ; Wed, 22 Dec 2021 18:57:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345021AbhLVS5D (ORCPT ); Wed, 22 Dec 2021 13:57:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344969AbhLVS4r (ORCPT ); Wed, 22 Dec 2021 13:56:47 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C559C061401 for ; Wed, 22 Dec 2021 10:56:47 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id q16so6769583wrg.7 for ; Wed, 22 Dec 2021 10:56:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=TUCZrT0R8cpMEB18cBkDpHwmjdJOKsrx7vpHWxOMd7M=; b=Blt3r/eTMtmCgMStl3tl7W1N5q24zx+kG3tWEnvp17Io8ApoA3kdQYNuO0MLvOkzXL c6AWFzbMjF0B7McQg4Vm2DAjj+mZ1iJegB7SP3dpA5SZFPShuWu+w0dTc1ez5vRZn/WL BPQZ7bNbXmI1sS9P4H+GKtEXRpB9/R49INxhPzg+VHaEZ4xYUHfiY+Twv9WLumovRd/6 pOEnm1iZaPpCVSceFxgNmUepMuVA60smlLnYbVdQrz0Kw7Op2rCXKGqpavlzlZLXjthp DHMEk8HfoNF48j0jfAcorljl6SdosyIkdBSb+jIfnnxLJv/sj6PJooOT3tDjFtFaamP9 0C3A== 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=mvFLKZCdoeRHxJMzxLJMuRTKgqrKkNrZ+mGh+g0a8dqRfSCx7nsI4mnHIyJL4uoYnM EkdDpCA0wn3AK1Q9DxVKKiFPdyIn32F/zBRuyUVfUyblNyHUd6QgiroF23kU1J9SBV8a bv71nCy1w75LyWIfmlKtmrxGOjzdglDXNokbbJ3RpHakQ81OsSuwv3fLEzvthnGguNP/ DOi4V8XK8jYk8O+B2sXoWd5U/x2NL0SznRPliyFB4/0WOf7WdwW5PlRzRoztThLmvAzH 0xJkY2AZhuML2/b+vhXSOnUUqk+AZvH7y29RXlL698WUzgWfKCVsrk/8HLhjNuw6PDfN 8BCA== X-Gm-Message-State: AOAM530sbdp95zJXp92HECZzIAu1FL+bE0u2I4J9OmyZDoCKD3YseaYQ XshZEmWNlLK+BbH4v3pdT+JkaGJ05gg= X-Google-Smtp-Source: ABdhPJwzLlg16djup8gmEBWQwMQLH64luufEwBMJ/1nNQ6+vs6wjXqKQLS2A6M37TWK4esw4t/W/Dg== X-Received: by 2002:a5d:47c2:: with SMTP id o2mr2948432wrc.637.1640199405660; Wed, 22 Dec 2021 10:56:45 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l6sm3591978wry.18.2021.12.22.10.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:45 -0800 (PST) Message-Id: <4175089ec432da921d158fec9ccb1902be710af6.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:33 +0000 Subject: [PATCH v5 13/16] reftable: remove outdated file reftable.c Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 Wed Dec 22 18:56:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697139 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 0C3FCC433F5 for ; Wed, 22 Dec 2021 18:57:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345005AbhLVS5O (ORCPT ); Wed, 22 Dec 2021 13:57:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345003AbhLVS4z (ORCPT ); Wed, 22 Dec 2021 13:56:55 -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 085F6C06175B for ; Wed, 22 Dec 2021 10:56:48 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id b73so2248791wmd.0 for ; Wed, 22 Dec 2021 10:56:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=y8TDGE/gBEgta19X8hluhddl1YisIHkMCl9efdCDzlY=; b=cqCN/OCknCWQ4WnyGdB4pX69UiVPD4cWjNqZzShGQfMJUXVXsW+y0voA3jjyDpj1cd t95yXU62C6evIaPCo8mN+nuNkEveXCJh7atHr77FoXtq/GyUdq/HGlsnMUOPtVtQi5B5 lhAp5QkLKgQRDliLJWia9LrRrHr17XV09EM8LVV9eFq+RWKUVxHvyVgSx/O2s4rNxP3F pvRzOT4EsuZHPnbafGA4VVO317ObcgfrmKSg+JT7b7OPwxNARweMzK738lahy3gKzzY0 ZXk6CAchJk6co0v/nDCp2yFx6F27q9udMhvvmvhuE7tJbJrbP6E+l616XWgIt4kmvDic 13ew== 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=y8TDGE/gBEgta19X8hluhddl1YisIHkMCl9efdCDzlY=; b=4BL8QKOh+Q8mJmgkNcsxYqecy2X4dSjX+dpI5kRPjuAeyMV5Jio/X7yMGzFQ7LiJH8 W2AOJUcBBorj8Vcm9JOH0YeDmlsPjP6pHbzMz5PjovbWltCBhwKD5UqYuLbw192hCXYl ZUkiA9Ei1IwqTA/v2yR9YIZr3MuqU1JTajPRGPfXMh2ODhfrqYRECsqJT6/cq0rjKjUK zFTd0JcDDBkpwOPWPuvtNcUo2X+4rFq7v2u5fsPeEfPie9FEtXKg4h8ckJiqKxLdmuhP F1KPhG2xFFkSdtBDbPSpvPflejzx9jb//S25FbgT5o7iW1Z2jVf+ZDm9JXA3F2T2comX 99bA== X-Gm-Message-State: AOAM531FoyXa5VMEhTy7Dd7c19HCtMmr8wB+YpCzDPUVkZRRoyv0n3Gd NoYgTRc2PrPxtFao8nbPwCly0oC3B/Y= X-Google-Smtp-Source: ABdhPJw4nOWZ7wiRnLBM0rdxqEfGXce/I4qQ41pMfcI6NBjM0LNV1xVztUvtvWHRfsImJGrxGLnf7Q== X-Received: by 2002:a05:600c:3d0f:: with SMTP id bh15mr1837637wmb.27.1640199406340; Wed, 22 Dec 2021 10:56:46 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id h14sm2662890wrz.31.2021.12.22.10.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:46 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:34 +0000 Subject: [PATCH v5 14/16] reftable: make reftable_record a tagged union Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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 c7a32331c76..a537af5f9a8 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; @@ -481,7 +480,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; @@ -491,34 +490,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; @@ -546,8 +546,8 @@ static int reader_seek_indexed(struct reftable_reader *r, done: block_iter_close(&next.bi); table_iter_close(&index_iter); - reftable_record_release(&want_index_rec); - reftable_record_release(&index_result_rec); + reftable_record_release(&want_index); + reftable_record_release(&index_result); return err; } @@ -596,11 +596,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); } @@ -608,12 +609,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); } @@ -657,31 +657,33 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r, struct reftable_iterator *it, uint8_t *oid) { - struct reftable_obj_record want = { - .hash_prefix = oid, - .hash_prefix_len = r->object_id_len, + struct reftable_record want = { + .type = BLOCK_TYPE_OBJ, + .u.obj = { + .hash_prefix = oid, + .hash_prefix_len = r->object_id_len, + }, }; - struct reftable_record want_rec = { NULL }; struct reftable_iterator oit = { NULL }; - struct reftable_obj_record got = { NULL }; - struct reftable_record got_rec = { NULL }; + struct reftable_record got = { + .type = BLOCK_TYPE_OBJ, + .u.obj = { 0 }, + }; int err = 0; struct indexed_table_ref_iter *itr = NULL; /* Look through the reverse index. */ - reftable_record_from_obj(&want_rec, &want); - err = reader_seek(r, &oit, &want_rec); + err = reader_seek(r, &oit, &want); if (err != 0) goto done; /* read out the reftable_obj_record */ - reftable_record_from_obj(&got_rec, &got); - err = iterator_next(&oit, &got_rec); + err = iterator_next(&oit, &got); if (err < 0) goto done; - if (err > 0 || - memcmp(want.hash_prefix, got.hash_prefix, r->object_id_len)) { + if (err > 0 || memcmp(want.u.obj.hash_prefix, got.u.obj.hash_prefix, + r->object_id_len)) { /* didn't find it; return empty iterator */ iterator_set_empty(it); err = 0; @@ -689,15 +691,16 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r, } err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id), - got.offsets, got.offset_len); + got.u.obj.offsets, + got.u.obj.offset_len); if (err < 0) goto done; - got.offsets = NULL; + got.u.obj.offsets = NULL; iterator_from_indexed_table_ref_iter(it, itr); done: reftable_iterator_destroy(&oit); - reftable_record_release(&got_rec); + reftable_record_release(&got); return err; } diff --git a/reftable/record.c b/reftable/record.c index 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 9fd24fa93ce..02b74d25c0f 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -253,8 +253,10 @@ done: int reftable_writer_add_ref(struct reftable_writer *w, struct reftable_ref_record *ref) { - struct reftable_record rec = { NULL }; - struct reftable_ref_record copy = *ref; + struct reftable_record rec = { + .type = BLOCK_TYPE_REF, + .u.ref = *ref, + }; int err = 0; if (ref->refname == NULL) @@ -263,8 +265,7 @@ int reftable_writer_add_ref(struct reftable_writer *w, ref->update_index > w->max_update_index) return REFTABLE_API_ERROR; - reftable_record_from_ref(&rec, ©); - copy.update_index -= w->min_update_index; + rec.u.ref.update_index -= w->min_update_index; err = writer_add_record(w, &rec); if (err < 0) @@ -303,7 +304,10 @@ int reftable_writer_add_refs(struct reftable_writer *w, static int reftable_writer_add_log_verbatim(struct reftable_writer *w, struct reftable_log_record *log) { - struct reftable_record rec = { NULL }; + struct reftable_record rec = { + .type = BLOCK_TYPE_LOG, + .u.log = *log, + }; if (w->block_writer && block_writer_type(w->block_writer) == BLOCK_TYPE_REF) { int err = writer_finish_public_section(w); @@ -313,8 +317,6 @@ static int reftable_writer_add_log_verbatim(struct reftable_writer *w, w->next -= w->pending_padding; w->pending_padding = 0; - - reftable_record_from_log(&rec, log); return writer_add_record(w, &rec); } @@ -395,8 +397,10 @@ static int writer_finish_section(struct reftable_writer *w) w->index_len = 0; w->index_cap = 0; for (i = 0; i < idx_len; i++) { - struct reftable_record rec = { NULL }; - reftable_record_from_index(&rec, idx + i); + struct reftable_record rec = { + .type = BLOCK_TYPE_INDEX, + .u.idx = idx[i], + }; if (block_writer_add(w->block_writer, &rec) == 0) { continue; } @@ -464,17 +468,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; @@ -487,7 +491,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 Wed Dec 22 18:56:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697137 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 13B4DC433EF for ; Wed, 22 Dec 2021 18:57:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345028AbhLVS5F (ORCPT ); Wed, 22 Dec 2021 13:57:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344996AbhLVS4y (ORCPT ); Wed, 22 Dec 2021 13:56:54 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09226C06175C for ; Wed, 22 Dec 2021 10:56:48 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id e5so6781073wrc.5 for ; Wed, 22 Dec 2021 10:56:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=EQ7LuISxS+OlxHRMui8s5gta6TAMzHzfTV3N4opZx10=; b=nnCAe9mJYbhv5e6TMlGVpMNLNXpO8ZLERgiLgYIepoyAJ+UhKFOdjM7NJC4q+FgG7+ 9zvxZ0RYsiy3ycPrOhzeTP+cdZaMw9o1l/r8Nsixn5/LTyGxpJNxWCkC4oiNM18eJcwU 5RwjKz27wX7RN8EGNCXems/hOnRl1ZgVniZ4LWfoqqZdzgaIq7aiZRBqZfxl0g8PSFLO rTALpDlLEq/LxPcWg5VWqJJj1ylbUmr11e0ifv1loHNS/02Ami/C42f8pOMIeJS801hk 8QgBnTqdp3eOFLAY2bm9mTnQjE9+VM0VsmBXw9WnvVeZ/auZ06vlOp0+jivPjLvFZy/5 KSKg== 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=O7G3hcvAlgMP22ZTWB+GvxiOqRSHB0HzGdqvc1PbK0Wx0rBcKhf4DAy7mOqDmV+YM4 IS5QbkZzfOarrK+L0x2Pq7OvPZWvzJHm8qWqIq0SXp/b/ALrsTidV+d8uMYIrLtOiAGD PTR5njsEvjrX5ITmM2BdeYqwLhx6Uef61snTXrbWDevENQLMn6KHdrrn7U8h8uQTCKe/ GbkQhth6DcOKVkBc9385MkLjpc0T7cn5zy977kmmU9tjzqj6ftsBnF6bV6xV+blWfDF2 SNMt1BJLpqlnUFSo86EZ/WI8kB03csKl9QL/O7mtNrUiYJopytcOMn+mG8U3enACnutH Bcdg== X-Gm-Message-State: AOAM532mBeiQKdkVYTPZ+TuYd4q+7Z/bxsbr1uDDx626T7anUb15J0ez hrfajVUMZMON2ps39mz+TeWhmUdTDOg= X-Google-Smtp-Source: ABdhPJzM5ktidRT/pcS4b2riup6L8Nmk0FQpIFuLkrI0BXDpnzA3hOiw01kpUN/KQH/eaq3CdEFTDQ== X-Received: by 2002:a05:6000:1807:: with SMTP id m7mr2923859wrh.191.1640199406843; Wed, 22 Dec 2021 10:56:46 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b132sm2494789wmd.38.2021.12.22.10.56.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:46 -0800 (PST) Message-Id: <6c06bd91662011fa115d1e5b2420ac0d315befb6.1640199396.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:35 +0000 Subject: [PATCH v5 15/16] reftable: add print functions to the record types Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , 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; From patchwork Wed Dec 22 18:56:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12697138 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 88271C433FE for ; Wed, 22 Dec 2021 18:57:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345032AbhLVS5G (ORCPT ); Wed, 22 Dec 2021 13:57:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345002AbhLVS4z (ORCPT ); Wed, 22 Dec 2021 13:56:55 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E7A3C061763 for ; Wed, 22 Dec 2021 10:56:49 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id i22so6694931wrb.13 for ; Wed, 22 Dec 2021 10:56:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=L/nZw9BRwEFnwFiUj7FGIopgD7BwEORebn06+Gh7xpA=; b=LRfUE58qDZRHb7M3e/0i999hTI7HM7IAkJMIj6TRyPD6VEsuq1yJI4MH/Gxbp01YYI OI332+qF0+B55ZuTfdp2yRMKXJEdTdNH77oJol7gjQu98OK627ldpSjBC+3XL/2gNnQb Aa0N+GEz+88TZAohjiPTn68lTrqt24JMWR3CHilPgeXqB1HzXEv7JoTqgBfWL9MkXE4y K7cDOqd/HK+BAsXJiid87QT4sAFyzAbze6G0iT7+GQtDxvrXFBHp+zDNsR6sVSh6kK2N SWqweMRWDSoqoSEBUHGgnH/GxKuTsraVRMrJM7DOVQqdMjVRtLJyc4POpKmpQukWnhrt O+iw== 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=L/nZw9BRwEFnwFiUj7FGIopgD7BwEORebn06+Gh7xpA=; b=lQMj5TdsJJIq0UyXnMVmijE0POlSOa7AkPA1mwL5G3842qsG78w0Fx5qGtzb2xVhkN 4JUDfcMzPz4+1HUp9EV6VD+ItDR4AsM5PRhPg8EkudoH9r7okgwi/KAucLFE48KKH6b8 9NAI3fu/XN9RrYOgj9poC+OT/XcPfDAHX6gmLT7LvSkwY/O5xITfGo3VwcZg8ySJHzsX +1R056R522WArCxZthzBrSvfJkE3MOTmJry6POtHUf8K8lZqsm295lOTFE2mCfbjn0ov kkJ+0spjtF+pHGYYVzh0gZihgWx/zPNL8Ho4YG2Wa6VvnKh95nXD73i0xpiI4WwcMOAH 7SqQ== X-Gm-Message-State: AOAM530dNeLSyw/1na8Z2unT9GFcMPuZjFsrA6qo2gl/HD3SNnPXNkSJ BQsc7cCmKtShVk5ORPnO8TOi8sJkHUs= X-Google-Smtp-Source: ABdhPJzSXqhVC4g1j3rWY1Lx2KTSIaK8baGM6UzSiXtr+vD1YorHXjK8DifXvoWR/WyETE7Gt5KicQ== X-Received: by 2002:adf:906d:: with SMTP id h100mr2866800wrh.259.1640199407681; Wed, 22 Dec 2021 10:56:47 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d10sm3203861wri.57.2021.12.22.10.56.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Dec 2021 10:56:47 -0800 (PST) Message-Id: In-Reply-To: References: Date: Wed, 22 Dec 2021 18:56:36 +0000 Subject: [PATCH v5 16/16] reftable: be more paranoid about 0-length memcpy calls Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Jeff King , Han-Wen Nienhuys , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys Signed-off-by: Han-Wen Nienhuys --- reftable/record.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index fbaa1fbef56..423e687b220 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -126,7 +126,8 @@ static int encode_string(char *str, struct string_view s) string_view_consume(&s, n); if (s.len < l) return -1; - memcpy(s.buf, str, l); + if (l) + memcpy(s.buf, str, l); string_view_consume(&s, l); return start.len - s.len; @@ -153,7 +154,9 @@ int reftable_encode_key(int *restart, struct string_view dest, if (dest.len < suffix_len) return -1; - memcpy(dest.buf, key.buf + prefix_len, suffix_len); + + if (suffix_len) + memcpy(dest.buf, key.buf + prefix_len, suffix_len); string_view_consume(&dest, suffix_len); return start.len - dest.len; @@ -569,7 +572,8 @@ static int reftable_obj_record_decode(void *rec, struct strbuf key, uint64_t last; int j; r->hash_prefix = reftable_malloc(key.len); - memcpy(r->hash_prefix, key.buf, key.len); + if (key.len) + memcpy(r->hash_prefix, key.buf, key.len); r->hash_prefix_len = key.len; if (val_type == 0) {