From de65f1fc823af65f759e669932be773a9f924ca4 Mon Sep 17 00:00:00 2001 From: "Grail Finder (aider)" Date: Mon, 10 Mar 2025 20:25:21 +0300 Subject: fix: add error handling for binary.Write and remove unused variable --- pngmeta/altwriter.go | 32 ++++++++++++++++++++++++-------- pngmeta/metareader.go | 1 - 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pngmeta/altwriter.go b/pngmeta/altwriter.go index ebef172..aec972a 100644 --- a/pngmeta/altwriter.go +++ b/pngmeta/altwriter.go @@ -96,10 +96,18 @@ func processChunks(data []byte) ([][]byte, []byte, error) { } fullChunk := bytes.NewBuffer(nil) - binary.Write(fullChunk, binary.BigEndian, chunkLength) - fullChunk.Write(chunkType) - fullChunk.Write(chunkData) - fullChunk.Write(crc) + if err := binary.Write(fullChunk, binary.BigEndian, chunkLength); err != nil { + return nil, nil, fmt.Errorf("error writing chunk length: %w", err) + } + if _, err := fullChunk.Write(chunkType); err != nil { + return nil, nil, fmt.Errorf("error writing chunk type: %w", err) + } + if _, err := fullChunk.Write(chunkData); err != nil { + return nil, nil, fmt.Errorf("error writing chunk data: %w", err) + } + if _, err := fullChunk.Write(crc); err != nil { + return nil, nil, fmt.Errorf("error writing CRC: %w", err) + } switch string(chunkType) { case "IEND": @@ -128,10 +136,18 @@ func createTextChunk(embed PngEmbed) []byte { crc.Write(data) chunk := bytes.NewBuffer(nil) - binary.Write(chunk, binary.BigEndian, uint32(len(data))) - chunk.Write([]byte(textChunkType)) - chunk.Write(data) - binary.Write(chunk, binary.BigEndian, crc.Sum32()) + if err := binary.Write(chunk, binary.BigEndian, uint32(len(data))); err != nil { + return nil, fmt.Errorf("error writing chunk length: %w", err) + } + if _, err := chunk.Write([]byte(textChunkType)); err != nil { + return nil, fmt.Errorf("error writing chunk type: %w", err) + } + if _, err := chunk.Write(data); err != nil { + return nil, fmt.Errorf("error writing chunk data: %w", err) + } + if err := binary.Write(chunk, binary.BigEndian, crc.Sum32()); err != nil { + return nil, fmt.Errorf("error writing CRC: %w", err) + } return chunk.Bytes() } diff --git a/pngmeta/metareader.go b/pngmeta/metareader.go index df2a8d4..408af18 100644 --- a/pngmeta/metareader.go +++ b/pngmeta/metareader.go @@ -22,7 +22,6 @@ const ( writeHeader = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A" ) -var tEXtChunkDataSpecification = "%s\x00%s" type PngEmbed struct { Key string -- cgit v1.2.3