Skip to content

Commit e6be8ea

Browse files
committed
fix(anonymizer): truncate output files on open to prevent stale JSON
When running the anonymizer tool multiple times pointing to the same output paths, the files were opened with O_CREATE|O_WRONLY but without O_TRUNC. This means any previous content is kept in place and new data is written on top. If the new output happens to be shorter than the old one, the leftover bytes at the end produce malformed JSON that cannot be parsed. Added os.O_TRUNC to all three os.OpenFile calls across writer.go and extractor.go so the file is always zeroed out before writing begins. Fixes: #8231 Signed-off-by: Sakthi Harish <sakthi.harish@edgeverve.com>
1 parent e75b845 commit e6be8ea

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

cmd/anonymizer/app/uiconv/extractor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type extractor struct {
2424

2525
// newExtractor creates extractor.
2626
func newExtractor(uiFile string, traceID string, reader *spanReader, logger *zap.Logger) (*extractor, error) {
27-
f, err := os.OpenFile(uiFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
27+
f, err := os.OpenFile(uiFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
2828
if err != nil {
2929
return nil, fmt.Errorf("cannot create output file: %w", err)
3030
}

cmd/anonymizer/app/writer/writer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func New(config Config, logger *zap.Logger) (*Writer, error) {
4848
}
4949
logger.Sugar().Infof("Current working dir is %s", wd)
5050

51-
cf, err := os.OpenFile(config.CapturedFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
51+
cf, err := os.OpenFile(config.CapturedFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
5252
if err != nil {
5353
return nil, fmt.Errorf("cannot create output file: %w", err)
5454
}
5555
logger.Sugar().Infof("Writing captured spans to file %s", config.CapturedFile)
5656

57-
af, err := os.OpenFile(config.AnonymizedFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
57+
af, err := os.OpenFile(config.AnonymizedFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
5858
if err != nil {
5959
return nil, fmt.Errorf("cannot create output file: %w", err)
6060
}

0 commit comments

Comments
 (0)