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

Add some nullables to methods & properties that can return nil #589

Merged
merged 4 commits into from
Nov 23, 2016

Conversation

Uncommon
Copy link
Contributor

Xcode 8 now complains about methods and properties that return nil but are not declared nullable. This fixes all of those analyzer warnings.

@@ -46,7 +46,7 @@ NS_ASSUME_NONNULL_BEGIN
/// type - the credential types allowed by the operation.
/// URL - the URL the operation is authenticating against.
/// userName - the user name provided by the operation. Can be nil, and might be ignored.
- (GTCredential * _Nullable)credentialForType:(GTCredentialType)type URL:(NSString *)URL userName:(nullable NSString *)userName;
- (GTCredential * _Nullable)credentialForType:(GTCredentialType)type URL:(nullable NSString *)URL userName:(nullable NSString *)userName;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

GTCredentialAcquireCallback potentially calls this with a nil url.

Copy link
Contributor

Choose a reason for hiding this comment

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

The libgit2 documentation implies that it's never NULL. At least git_remote_connect will return an error on a NULL URL. It seems the only other user is proxy support, and in that case it's not clear (there's a check skipping NULLs in git_proxy_options_dup).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't even find where it's used, searching for git_cred_acquire_cb etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

I used the regex /->cred(entials|_acquire_cb)/ against libgit2 and got most of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like it's probably supposed to be non-null, but to guarantee it I'd rather depend on someone who knows the code better.

@tiennou
Copy link
Contributor

tiennou commented Sep 15, 2016

Question : does NSAssert-ing that the return value is not nil makes the warning go away ? Some of those changes look strange, eg. GTOID having a nullable -SHA, or GTReference and a nullable -OID...

@Uncommon
Copy link
Contributor Author

Yes it does. Would you like me to replace the null checks with NSAsserts?

@tiennou
Copy link
Contributor

tiennou commented Sep 16, 2016

Good question. From an API standpoint, I think asserting makes it cleaner. On the other hand, if it ever happens, we'll throw an exception at runtime, which is Not Cool.

@Uncommon
Copy link
Contributor Author

Let's think about the actual failure cases, then.

For GTOID.SHA:

  • Failure to malloc 40 bytes. If that happens, you're in trouble anyway.
  • NSString.initWithBytesNoCopy is declared as nullable, but doesn't specify when it might return null. Presumably it's if the given string doesn't fit the specified encoding. If git_oid_fmt yields something that's not UTF-8 compliant, then something is seriously wrong.

I think it's okay to assert in those cases. I'll proceed similarly with the others and see what you think.

@@ -52,7 +52,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, strong) GTRepository *repository;
@property (nonatomic, readonly) GTReferenceType referenceType;
@property (nonatomic, readonly) const git_oid *git_oid;
@property (nonatomic, strong, readonly) GTOID *OID;
@property (nonatomic, strong, readonly, nullable) GTOID *OID;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

git_reference_target returns null if it's not a direct reference, so this should be nullable.

@@ -42,10 +42,10 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, copy, nullable) NSString *email;

/// The time when the action happened.
@property (nonatomic, readonly, strong) NSDate *time;
@property (nonatomic, readonly, strong, nullable) NSDate *time;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other properties are nullable, so this is consistent.

On the other hand, they all return nil if git_signature is nil, and it's not nullable, and it doesn't look like it should ever be null.

But since I'm making this consistent with the other properties, I'll leave that as a separate issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC it's done that way because libgit2 might fail to parse an object's headers (in some degenerate-odb cases), hence allowing some of them to be null.

@@ -42,10 +42,10 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, copy, nullable) NSString *email;

/// The time when the action happened.
@property (nonatomic, readonly, strong) NSDate *time;
@property (nonatomic, readonly, strong, nullable) NSDate *time;
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC it's done that way because libgit2 might fail to parse an object's headers (in some degenerate-odb cases), hence allowing some of them to be null.


git_oid_fmt(SHA, self.git_oid);

NSString *str = [[NSString alloc] initWithBytesNoCopy:SHA length:GIT_OID_HEXSZ encoding:NSUTF8StringEncoding freeWhenDone:YES];
if (str == nil) free(SHA);
NSAssert(str != nil, @"Failed to allocate SHA string");
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe

  char *SHA = git_oid_tostr_s(self.git_oid); // Stored in TLS
  NSString *str = [[NSString alloc] initWithBytes:SHA length:GIT_OID_HEXSZ encoding:NSUTF8StringEncoding]; // So we copy the TLS data
  NSAssert(str != nil, @"Failed to allocate SHA string");

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even better!

@@ -46,7 +46,7 @@ NS_ASSUME_NONNULL_BEGIN
/// type - the credential types allowed by the operation.
/// URL - the URL the operation is authenticating against.
/// userName - the user name provided by the operation. Can be nil, and might be ignored.
- (GTCredential * _Nullable)credentialForType:(GTCredentialType)type URL:(NSString *)URL userName:(nullable NSString *)userName;
- (GTCredential * _Nullable)credentialForType:(GTCredentialType)type URL:(nullable NSString *)URL userName:(nullable NSString *)userName;
Copy link
Contributor

Choose a reason for hiding this comment

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

I used the regex /->cred(entials|_acquire_cb)/ against libgit2 and got most of them.

@Uncommon
Copy link
Contributor Author

I see the Travis failure, but it doesn't seem to be related to my changes.

@Uncommon
Copy link
Contributor Author

(bump) Any further changes needed on this?

char *SHA = git_oid_tostr_s(self.git_oid);
NSString *str = [[NSString alloc] initWithBytes:SHA
length:GIT_OID_HEXSZ
encoding:NSUTF8StringEncoding];
Copy link
Contributor

Choose a reason for hiding this comment

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

Crazy indentation here 😉 .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like Xcode was doing tabs instead of spaces, at 2 spaces per tab. Fixed.

@Uncommon
Copy link
Contributor Author

Another bump. Any more issues?

@tiennou
Copy link
Contributor

tiennou commented Nov 23, 2016

Not that I can see. A local run of script/cibuild worked here so I'm merging this. Thanks for the PR !

@tiennou tiennou merged commit 3480278 into libgit2:master Nov 23, 2016
@Uncommon Uncommon deleted the nullables branch December 5, 2016 16:08
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.

2 participants