From 18ce20b34413f355241bcb0135a5793b7e2d0240 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 8 May 2024 15:58:36 +0200 Subject: executor: fix embeding of headers common_usb.h is included by both common_linux.h and common_netbsd.h. The current version may fail to replace one of these common_usb.h, if say common_linux.h is already replaced, but common_netbsd.h is not yet. Make the replacement algorithm more robust and just replace everything on each iteration until we can't replace anything anymore. Fixes #4783 --- executor/embed.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'executor/embed.go') diff --git a/executor/embed.go b/executor/embed.go index a02413a12..9a8a83141 100644 --- a/executor/embed.go +++ b/executor/embed.go @@ -8,6 +8,7 @@ import ( "embed" "fmt" "io/fs" + "maps" "path" "regexp" ) @@ -36,9 +37,9 @@ var CommonHeader = func() []byte { } } // To not hardcode concrete order in which headers need to be replaced - // we just iteratively try to replace whatever headers can be replaced - // on the current step. - for len(headers) != 0 { + // we just iteratively try to replace whatever headers can be replaced. + unused := maps.Clone(headers) + for { relacedSomething := false for file := range headers { replace := []byte("#include \"" + path.Base(file) + "\"") @@ -53,13 +54,16 @@ var CommonHeader = func() []byte { panic(err) } data = bytes.ReplaceAll(data, replace, contents) - delete(headers, file) + delete(unused, file) relacedSomething = true } if !relacedSomething { - panic(fmt.Sprintf("can't find includes for %v", headers)) + break } } + if len(unused) != 0 { + panic(fmt.Sprintf("can't find includes for %v", unused)) + } // Remove `//` comments, but keep lines which start with `//%`. for _, remove := range []string{ "(\n|^)\\s*//$", -- cgit mrf-deployment