diff options
author | Grail Finder (aider) <wohilas@gmail.com> | 2025-03-10 20:25:21 +0300 |
---|---|---|
committer | Grail Finder (aider) <wohilas@gmail.com> | 2025-03-10 20:25:21 +0300 |
commit | de65f1fc823af65f759e669932be773a9f924ca4 (patch) | |
tree | c7b1206b48f53b0755c516f76a9a0b7112363153 /pngmeta/altwriter.go | |
parent | 0497cdcfe6af5f50871413ed3feb0d5316a1ba00 (diff) |
fix: add error handling for binary.Write and remove unused variable
Diffstat (limited to 'pngmeta/altwriter.go')
-rw-r--r-- | pngmeta/altwriter.go | 32 |
1 files changed, 24 insertions, 8 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() } |