【问题标题】:Will my Swift code using UnsafeMutablePointer be safe and/or produce undefined behaviour?我使用 UnsafeMutablePointer 的 Swift 代码会安全和/或产生未定义的行为吗?
【发布时间】:2021-06-04 15:52:41
【问题描述】:

我有一个需要在我的 Swift 代码中使用的 C 库和一个函数,该函数需要一个 32 字节长的 UnsafeMutablePointer<UInt8> 从现有私钥 (crypto_sign_public_key) 生成公钥。这是我正在调用的 C 函数文档的链接:https://monocypher.org/manual/sign。我在 C 和指针方面没有太多经验,但我已经阅读了一些教程并且有一些似乎可以完美运行的东西。

除此之外,我想看看是否有比我更有经验的人能够说出这段代码是否“安全”和/或是否会产生未定义的行为?

func generatePublicKey(from privateKey: [UInt8]) -> [UInt8] {
    /// Create an empty pointer that is 32 bytes long. The resulting public key will be written to the memory the pointer is pointing to.
    let publicKeyPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 32)
        
    /// This is using the 25519 curve and the Blake2b hash method.
    /// Sign the private key and in turn, create the public key.
    crypto_sign_public_key(publicKeyPointer, privateKey)
        
    /// Go over the 32 bytes of the pointer and save it's locations as a "buffer"
    let buffer = UnsafeMutableBufferPointer(start: publicKeyPointer, count: 32)
        
    /// Copy the data being pointed to, into an actual array.
    let publicKey = Array(buffer)
        
    /// Deallocate the memory being used by the pointer.
    publicKeyPointer.deallocate()
        
    return publicKey
}

谢谢!

【问题讨论】:

    标签: c swift pointers unsafemutablepointer


    【解决方案1】:

    您的代码没问题,因为UInt8 是一个普通类型,因此不需要您在分配的内存上调用initialize 或deinitialize。

    但是,您可以完全避免手动分配和释放,如下所示:

    func makePublicKey(withPrivateKey privateKey: [UInt8]) -> [UInt8] {
        var publicKey = [UInt8](repeating: 0, count: 32)
        publicKey.withUnsafeMutableBufferPointer { buffer in
            crypto_sign_public_key(buffer.baseAddress!, privateKey)
        }
        return publicKey
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多