[PATCH] btf_encoder: Add O_TRUNC to btf_encoder__write_raw_file()

Namhyung Kim <[email protected]> Fri, 17 Jul 2026 11:01:05 -0700
Newsgroups org.kernel.vger.dwarves
Message-ID <[email protected]>
When it creates a raw file, it should truncate the content in case there
is an old file with the same name.  Otherwise it may have some garbage
at the end of the file and it could make the kernel fail to verify BTF.

Signed-off-by: Namhyung Kim <[email protected]>
---
 btf_encoder.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/btf_encoder.c b/btf_encoder.c
index d36984a..b810dd5 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -1878,7 +1878,7 @@ static int btf_encoder__write_raw_file(struct btf_encoder *encoder)
 		return -1;
 	}
 
-	fd = open(filename, O_WRONLY | O_CREAT, 0640);
+	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0640);
 	if (fd < 0) {
 		fprintf(stderr, "%s: Couldn't open %s for writing the raw BTF info: %s\n", __func__, filename, strerror(errno));
 		return -1;
-- 
2.55.0.229.g6434b31f56-goog