From b397e0eb637619a9689a74c45745a6df14bc72eb Mon Sep 17 00:00:00 2001
From: David Sterba <dsterba@suse.com>
Date: Mon, 28 Nov 2022 19:57:43 +0100
Subject: [PATCH] btrfs-progs: string-table: check bounds before writing to a
 cell

Signed-off-by: David Sterba <dsterba@suse.com>
---
 common/string-table.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/common/string-table.c b/common/string-table.c
index a75cac1b..dc9a5f74 100644
--- a/common/string-table.c
+++ b/common/string-table.c
@@ -18,6 +18,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include "common/messages.h"
 #include "common/string-table.h"
 
 /*
@@ -54,6 +55,12 @@ char *table_vprintf(struct string_table *tab, unsigned int column, unsigned int
 	if (!msg)
 		return NULL;
 
+	if (column >= tab->ncols || row >= tab->nrows) {
+		error("attempt to write outside of table: col %u row %u fmt %s",
+			column, row, fmt);
+		return NULL;
+	}
+
 	if (tab->cells[idx])
 		free(tab->cells[idx]);
 	tab->cells[idx] = msg;