Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix over-iterating buffer in hex string utility. #141

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

VaslD
Copy link

@VaslD VaslD commented Jan 10, 2023

Fixes #139: over-iteration in hex string.

Checklist

  • I've run tests to see all new and existing tests pass
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

If you've made changes to gyb files

  • I've run .script/generate_boilerplate_files_with_gyb and included updated generated files in a commit of this pull request

Motivation:

Upon discussing it in #139, an issue was identified that could cause unexpected hex output for a data buffer whose memory spans multiple non-contiguous regions.

Modifications:

Removed memory region specific logic. Fix applied to 3 copies of the same function.

Also added unit test on Apple platforms to compare with NSString's hex output.

Result:

With multi-region DataProtocols, hexString should return correct hex.

@swift-server-bot
Copy link

Can one of the admins verify this patch?

6 similar comments
@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! One minor tweak in the compiler defines and we should be go.

import XCTest

// Not sure if NSString-bridged "String.init(format:_:)" is available on Linux/Windows.
#if (os(macOS) || os(iOS) || os(tvOS) || os(watchOS)) && CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this line needs to be:

#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
// Skip tests that require @testable imports of CryptoKit.
#else
#if (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@testable import CryptoKit
#else
@testable import Crypto
#endif

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did look at what other tests wrote. But I wasn't checking whether to use CryptoKit or Crypto, rather I needed to know (guess) whether String.init(format:_:) was available in the SDK. On appleOS this API is provided for sure by bridging from NSString's initializer, but on Linux and Windows I couldn't know.

The code you suggested would make this test always available cross-platform. Let me verify if Swift's cross-platform Foundation port has this API. Otherwise I'll need to do something like sprintf to make it cross-platform.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also CryptoTests defines its own copy of PrettyBytes. It's weird because I though @testable import means that test target has access to everything non-private in the original Crypto module.

This would mean the PrettyBytesTests does not actually need to import anything, as the test only tests functions defined in the test target itself.

Can you clarify this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with only testing the one in the tests module, tbh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible Over-Iteration in PrettyBytes
3 participants