apple / apple/swift-crypto

Possible Over-Iteration in PrettyBytes

Open
#139 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Assembly
Stars
1.7k
Forks
223
Avg merge
1d 2h
Merged PRs (30d)
7

Description

I was looking for a not `%02x` way of converting binary to hex `String`, and I stumbled upon the implementation in `PrettyBytes.swift`.

https://github.com/apple/swift-crypto/blob/9cc89f0170308b813af05dadcd26f9a2dee47713/Sources/Crypto/Util/PrettyBytes.swift#L45-L51

I'm not 100% sure about the `Sequence` conformance of non-contiguous `DataProtocol`, but it seemed to me that the `self` on line 46 should be the ignored parameter `$0` (or maybe eliminate the region logic since looping over `self` most likely would have taken non-contiguous memory regions into account?), otherwise the code reads:

> For **each segment** of the data buffer, loop over **the entirety of data buffer** and convert each byte to two hex chars.

Unless non-contiguous `DataProtocol` has non-stable indices and iterators, this code will over/re-iterate the data buffer.

Contributor guide

Open the contributing guide

Research direction

Inspect Sources/Crypto/Util/PrettyBytes.swift at lines 45-51, starting with how the region iteration and Sequence traversal interact for non-contiguous DataProtocol values. Verify the iteration count and resulting hex string; done means each byte is converted once without changing the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.