git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Edward Thomson <ethomson@edwardthomson.com>,
	karthik nayak <karthik.188@gmail.com>,
	Taylor Blau <me@ttaylorr.com>, shejialuo <shejialuo@gmail.com>
Subject: [PATCH v3 08/10] reftable/record: adapt `reftable_record_key()` to handle allocation failures
Date: Thu, 17 Oct 2024 06:54:08 +0200	[thread overview]
Message-ID: <268e4cd6fc6fb0d9ab6216723dc39c5cada3734c.1729140565.git.ps@pks.im> (raw)
In-Reply-To: <cover.1729140565.git.ps@pks.im>

The `reftable_record_key()` function cannot pass any errors to the
caller as it has a `void` return type. Adapt it and its callers such
that we can handle errors and start handling allocation failures.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 reftable/block.c  | 20 +++++++++++++++-----
 reftable/reader.c |  8 ++++++--
 reftable/record.c | 32 ++++++++++++++++++++------------
 reftable/record.h |  4 ++--
 reftable/writer.c |  5 ++++-
 5 files changed, 47 insertions(+), 22 deletions(-)

diff --git a/reftable/block.c b/reftable/block.c
index 4f62b823db8..697b8b41531 100644
--- a/reftable/block.c
+++ b/reftable/block.c
@@ -111,9 +111,12 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
 	int is_restart = 0;
 	struct reftable_buf key = REFTABLE_BUF_INIT;
 	int n = 0;
-	int err = -1;
+	int err;
+
+	err = reftable_record_key(rec, &key);
+	if (err < 0)
+		goto done;
 
-	reftable_record_key(rec, &key);
 	if (!key.len) {
 		err = REFTABLE_API_ERROR;
 		goto done;
@@ -121,13 +124,17 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
 
 	n = reftable_encode_key(&is_restart, out, last, key,
 				reftable_record_val_type(rec));
-	if (n < 0)
+	if (n < 0) {
+		err = -1;
 		goto done;
+	}
 	string_view_consume(&out, n);
 
 	n = reftable_record_encode(rec, out, w->hash_size);
-	if (n < 0)
+	if (n < 0) {
+		err = -1;
 		goto done;
+	}
 	string_view_consume(&out, n);
 
 	err = block_writer_register_restart(w, start.len - out.len, is_restart,
@@ -522,6 +529,10 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
 			goto done;
 		}
 
+		err = reftable_record_key(&rec, &it->last_key);
+		if (err < 0)
+			goto done;
+
 		/*
 		 * Check whether the current key is greater or equal to the
 		 * sought-after key. In case it is greater we know that the
@@ -536,7 +547,6 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
 		 * to `last_key` now, and naturally all keys share a prefix
 		 * with themselves.
 		 */
-		reftable_record_key(&rec, &it->last_key);
 		if (reftable_buf_cmp(&it->last_key, want) >= 0) {
 			it->next_off = prev_off;
 			goto done;
diff --git a/reftable/reader.c b/reftable/reader.c
index 388f8bf6d7b..ab89efd9c55 100644
--- a/reftable/reader.c
+++ b/reftable/reader.c
@@ -356,7 +356,9 @@ static int table_iter_seek_linear(struct table_iter *ti,
 	int err;
 
 	reftable_record_init(&rec, reftable_record_type(want));
-	reftable_record_key(want, &want_key);
+	err = reftable_record_key(want, &want_key);
+	if (err < 0)
+		goto done;
 
 	/*
 	 * First we need to locate the block that must contain our record. To
@@ -439,7 +441,9 @@ static int table_iter_seek_indexed(struct table_iter *ti,
 	};
 	int err;
 
-	reftable_record_key(rec, &want_index.u.idx.last_key);
+	err = reftable_record_key(rec, &want_index.u.idx.last_key);
+	if (err < 0)
+		goto done;
 
 	/*
 	 * The index may consist of multiple levels, where each level may have
diff --git a/reftable/record.c b/reftable/record.c
index 0182c973437..672c5f909a9 100644
--- a/reftable/record.c
+++ b/reftable/record.c
@@ -207,12 +207,12 @@ int reftable_decode_key(struct reftable_buf *last_key, uint8_t *extra,
 	return start_len - in.len;
 }
 
-static void reftable_ref_record_key(const void *r, struct reftable_buf *dest)
+static int reftable_ref_record_key(const void *r, struct reftable_buf *dest)
 {
 	const struct reftable_ref_record *rec =
 		(const struct reftable_ref_record *)r;
 	reftable_buf_reset(dest);
-	reftable_buf_addstr(dest, rec->refname);
+	return reftable_buf_addstr(dest, rec->refname);
 }
 
 static int reftable_ref_record_copy_from(void *rec, const void *src_rec,
@@ -465,12 +465,12 @@ static struct reftable_record_vtable reftable_ref_record_vtable = {
 	.cmp = &reftable_ref_record_cmp_void,
 };
 
-static void reftable_obj_record_key(const void *r, struct reftable_buf *dest)
+static int reftable_obj_record_key(const void *r, struct reftable_buf *dest)
 {
 	const struct reftable_obj_record *rec =
 		(const struct reftable_obj_record *)r;
 	reftable_buf_reset(dest);
-	reftable_buf_add(dest, rec->hash_prefix, rec->hash_prefix_len);
+	return reftable_buf_add(dest, rec->hash_prefix, rec->hash_prefix_len);
 }
 
 static void reftable_obj_record_release(void *rec)
@@ -664,19 +664,27 @@ static struct reftable_record_vtable reftable_obj_record_vtable = {
 	.cmp = &reftable_obj_record_cmp_void,
 };
 
-static void reftable_log_record_key(const void *r, struct reftable_buf *dest)
+static int reftable_log_record_key(const void *r, struct reftable_buf *dest)
 {
 	const struct reftable_log_record *rec =
 		(const struct reftable_log_record *)r;
-	int len = strlen(rec->refname);
+	int len = strlen(rec->refname), err;
 	uint8_t i64[8];
 	uint64_t ts = 0;
+
 	reftable_buf_reset(dest);
-	reftable_buf_add(dest, (uint8_t *)rec->refname, len + 1);
+	err = reftable_buf_add(dest, (uint8_t *)rec->refname, len + 1);
+	if (err < 0)
+		return err;
 
 	ts = (~ts) - rec->update_index;
 	put_be64(&i64[0], ts);
-	reftable_buf_add(dest, i64, sizeof(i64));
+
+	err = reftable_buf_add(dest, i64, sizeof(i64));
+	if (err < 0)
+		return err;
+
+	return 0;
 }
 
 static int reftable_log_record_copy_from(void *rec, const void *src_rec,
@@ -1027,11 +1035,11 @@ static struct reftable_record_vtable reftable_log_record_vtable = {
 	.cmp = &reftable_log_record_cmp_void,
 };
 
-static void reftable_index_record_key(const void *r, struct reftable_buf *dest)
+static int reftable_index_record_key(const void *r, struct reftable_buf *dest)
 {
 	const struct reftable_index_record *rec = r;
 	reftable_buf_reset(dest);
-	reftable_buf_add(dest, rec->last_key.buf, rec->last_key.len);
+	return reftable_buf_add(dest, rec->last_key.buf, rec->last_key.len);
 }
 
 static int reftable_index_record_copy_from(void *rec, const void *src_rec,
@@ -1124,9 +1132,9 @@ static struct reftable_record_vtable reftable_index_record_vtable = {
 	.cmp = &reftable_index_record_cmp,
 };
 
-void reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest)
+int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest)
 {
-	reftable_record_vtable(rec)->key(reftable_record_data(rec), dest);
+	return reftable_record_vtable(rec)->key(reftable_record_data(rec), dest);
 }
 
 int reftable_record_encode(struct reftable_record *rec, struct string_view dest,
diff --git a/reftable/record.h b/reftable/record.h
index 271da3bf360..25aa908c859 100644
--- a/reftable/record.h
+++ b/reftable/record.h
@@ -40,7 +40,7 @@ int put_var_int(struct string_view *dest, uint64_t val);
 /* Methods for records. */
 struct reftable_record_vtable {
 	/* encode the key of to a uint8_t reftable_buf. */
-	void (*key)(const void *rec, struct reftable_buf *dest);
+	int (*key)(const void *rec, struct reftable_buf *dest);
 
 	/* The record type of ('r' for ref). */
 	uint8_t type;
@@ -137,7 +137,7 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ);
 /* see struct record_vtable */
 int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b);
 int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size);
-void reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest);
+int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest);
 int reftable_record_copy_from(struct reftable_record *rec,
 			      struct reftable_record *src, int hash_size);
 uint8_t reftable_record_val_type(struct reftable_record *rec);
diff --git a/reftable/writer.c b/reftable/writer.c
index da6941a78ac..377db709c85 100644
--- a/reftable/writer.c
+++ b/reftable/writer.c
@@ -249,7 +249,10 @@ static int writer_add_record(struct reftable_writer *w,
 	struct reftable_buf key = REFTABLE_BUF_INIT;
 	int err;
 
-	reftable_record_key(rec, &key);
+	err = reftable_record_key(rec, &key);
+	if (err < 0)
+		goto done;
+
 	if (reftable_buf_cmp(&w->last_key, &key) >= 0) {
 		err = REFTABLE_API_ERROR;
 		goto done;
-- 
2.47.0.72.gef8ce8f3d4.dirty


  parent reply	other threads:[~2024-10-17  4:54 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-11  6:54 [PATCH 00/10] reftable: stop using `struct strbuf` Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 01/10] reftable: stop using `strbuf_addbuf()` Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 02/10] reftable: stop using `strbuf_addf()` Patrick Steinhardt
2024-10-11  9:51   ` karthik nayak
2024-10-14 13:09     ` Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 03/10] reftable/basics: provide new `reftable_buf` interface Patrick Steinhardt
2024-10-11 10:03   ` karthik nayak
2024-10-14 13:09     ` Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 04/10] reftable: convert from `strbuf` to `reftable_buf` Patrick Steinhardt
2024-10-11 12:12   ` karthik nayak
2024-10-14 13:09     ` Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 05/10] reftable/blocksource: adapt interface name Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 06/10] t/unit-tests: check for `reftable_buf` allocation errors Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 07/10] reftable/stack: adapt `format_name()` to handle allocation failures Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 08/10] reftable/record: adapt `reftable_record_key()` " Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 09/10] reftable/stack: adapt `stack_filename()` " Patrick Steinhardt
2024-10-11  6:54 ` [PATCH 10/10] reftable: handle trivial `reftable_buf` errors Patrick Steinhardt
2024-10-11 12:18 ` [PATCH 00/10] reftable: stop using `struct strbuf` karthik nayak
2024-10-14 13:09   ` Patrick Steinhardt
2024-10-14 13:02 ` [PATCH v2 " Patrick Steinhardt
2024-10-14 13:02   ` [PATCH v2 01/10] reftable: stop using `strbuf_addbuf()` Patrick Steinhardt
2024-10-14 22:19     ` Taylor Blau
2024-10-14 13:02   ` [PATCH v2 02/10] reftable: stop using `strbuf_addf()` Patrick Steinhardt
2024-10-14 22:32     ` Taylor Blau
2024-10-15  4:37       ` Patrick Steinhardt
2024-10-15 19:26         ` Taylor Blau
2024-10-14 13:02   ` [PATCH v2 03/10] reftable/basics: provide new `reftable_buf` interface Patrick Steinhardt
2024-10-14 22:34     ` Taylor Blau
2024-10-15  4:38       ` Patrick Steinhardt
2024-10-15  5:10         ` Eric Sunshine
2024-10-15 19:27           ` Taylor Blau
2024-10-16  8:42             ` Patrick Steinhardt
2024-10-16 20:56               ` Taylor Blau
2024-10-17  4:54                 ` Patrick Steinhardt
2024-10-17 20:59                   ` Taylor Blau
2024-10-14 13:02   ` [PATCH v2 04/10] reftable: convert from `strbuf` to `reftable_buf` Patrick Steinhardt
2024-10-14 22:35     ` Taylor Blau
2024-10-14 13:02   ` [PATCH v2 05/10] reftable/blocksource: adapt interface name Patrick Steinhardt
2024-10-14 13:02   ` [PATCH v2 06/10] t/unit-tests: check for `reftable_buf` allocation errors Patrick Steinhardt
2024-10-14 13:02   ` [PATCH v2 07/10] reftable/stack: adapt `format_name()` to handle allocation failures Patrick Steinhardt
2024-10-14 22:41     ` Taylor Blau
2024-10-14 13:02   ` [PATCH v2 08/10] reftable/record: adapt `reftable_record_key()` " Patrick Steinhardt
2024-10-14 13:02   ` [PATCH v2 09/10] reftable/stack: adapt `stack_filename()` " Patrick Steinhardt
2024-10-14 13:02   ` [PATCH v2 10/10] reftable: handle trivial `reftable_buf` errors Patrick Steinhardt
2024-10-14 22:44   ` [PATCH v2 00/10] reftable: stop using `struct strbuf` Taylor Blau
2024-10-15  4:37     ` Patrick Steinhardt
2024-10-15 10:33       ` shejialuo
2024-10-15 10:44         ` Patrick Steinhardt
2024-10-15 11:23           ` shejialuo
2024-10-17  4:53 ` [PATCH v3 " Patrick Steinhardt
2024-10-17  4:53   ` [PATCH v3 01/10] reftable: stop using `strbuf_addbuf()` Patrick Steinhardt
2024-10-17  4:53   ` [PATCH v3 02/10] reftable: stop using `strbuf_addf()` Patrick Steinhardt
2024-10-17  4:53   ` [PATCH v3 03/10] reftable/basics: provide new `reftable_buf` interface Patrick Steinhardt
2024-10-17  4:53   ` [PATCH v3 04/10] reftable: convert from `strbuf` to `reftable_buf` Patrick Steinhardt
2024-10-17  4:53   ` [PATCH v3 05/10] reftable/blocksource: adapt interface name Patrick Steinhardt
2024-10-17  4:54   ` [PATCH v3 06/10] t/unit-tests: check for `reftable_buf` allocation errors Patrick Steinhardt
2024-10-17  4:54   ` [PATCH v3 07/10] reftable/stack: adapt `format_name()` to handle allocation failures Patrick Steinhardt
2024-10-17  4:54   ` Patrick Steinhardt [this message]
2024-10-17  4:54   ` [PATCH v3 09/10] reftable/stack: adapt `stack_filename()` " Patrick Steinhardt
2024-10-17  4:54   ` [PATCH v3 10/10] reftable: handle trivial `reftable_buf` errors Patrick Steinhardt
2024-10-17 21:00   ` [PATCH v3 00/10] reftable: stop using `struct strbuf` Taylor Blau
2024-10-18  7:46     ` karthik nayak
2024-10-18 21:41       ` Taylor Blau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=268e4cd6fc6fb0d9ab6216723dc39c5cada3734c.1729140565.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=ethomson@edwardthomson.com \
    --cc=git@vger.kernel.org \
    --cc=karthik.188@gmail.com \
    --cc=me@ttaylorr.com \
    --cc=shejialuo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).