Skip to content

Remove unnecessary casts within FileManager #1594

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

Merged
merged 3 commits into from
Jun 12, 2018

Conversation

rauhul
Copy link
Member

@rauhul rauhul commented Jun 12, 2018

No description provided.

@rauhul
Copy link
Member Author

rauhul commented Jun 12, 2018

As the title suggests there were a couple unnecessary casts typically between int types in FileManager.swift. These are fixed in this pr.

@@ -503,16 +503,16 @@ open class FileManager : NSObject {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(fileInfo.st_blksize))
defer { buffer.deallocate() }

var bytesRemaining = Int64(fileInfo.st_size)
var bytesRemaining = Int(fileInfo.st_size)
while bytesRemaining > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

st_size might be 64bits even on 32bit platforms, so this avoids any problems by always converting upto 64bits. The rest of the casts in this section are so that all arithmetic is conducted in 64bits

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I didn't consider this. So is the cast down from Int64 to Int in the following line safe on 32bit platforms because fileInfo.st_blksize is Int32 on all platforms?

let bytesRead = try _readFrom(fd: srcfd, toBuffer: buffer, length: Int(bytesToRead), filename: srcPath)

Copy link
Member Author

Choose a reason for hiding this comment

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

Also would is this just implicit knowledge someone should know or would it be prudent to include in a comment?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, its unlikely st_blksize will be 64bit on any platform and even if it was, its value would be unlikely to exceed Int32.max. If it ever is, it will trap and abort the program and can be dealt with then.

bytesRemaining is computed in 64bits as file size can be large, hence bytesToRead also needs to be computed in 64bits. However the blocks that are read/written will be a lot smaller and read() takes an int hence the cast.

let len = Int(PATH_MAX) + 1
var buf = [Int8](repeating: 0, count: len)
getcwd(&buf, len)
return string(withFileSystemRepresentation: buf, length: len)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

string(withFileSystemRepresentation:length:) takes the length of the NUL-terminated string, not the whole buffer so strlen(buf) is correct here - although destinationOfSymbolicLink(atPath:) seems to be written the wrong way round so needs fixing.

@millenomi
Copy link
Contributor

@swift-ci please test

1 similar comment
@millenomi
Copy link
Contributor

@swift-ci please test

@millenomi millenomi merged commit 5a32c12 into swiftlang:master Jun 12, 2018
@rauhul rauhul deleted the remove_unnecessary_casts branch June 13, 2018 01:16
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.

3 participants