Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Use objcType to identify the value types. #73

Merged
merged 4 commits into from
Nov 29, 2017
Merged

Use objcType to identify the value types. #73

merged 4 commits into from
Nov 29, 2017

Conversation

jverkoey
Copy link
Contributor

This makes it much more scalable to support arbitrary key paths.

@jverkoey jverkoey requested review from samnm and romoore November 29, 2017 16:14
static BOOL IsValueType(id someValue, char *objCType) {
if ([someValue isKindOfClass:[NSValue class]]) {
NSValue *asValue = (NSValue *)someValue;
return strcmp(asValue.objCType, objCType) == 0;
Copy link

@romoore romoore Nov 29, 2017

Choose a reason for hiding this comment

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

Please switch to strncmp and limit the length to something short like 10 characters.
CGSize = "{CGSize=dd}"
CGRect = "{CGRect={CGPoint=dd}{CGSize=dd}}"
CGPoint = "{CGPoint=dd}"
CGRectNull = "{CGRect={CGPoint=dd}{CGSize=dd}}"

Reference: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html

Copy link
Contributor Author

@jverkoey jverkoey Nov 29, 2017

Choose a reason for hiding this comment

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

Fixed by using objCType's string length. objCType is "trusted" in that we're only ever providing @encode values to it.

@jverkoey jverkoey dismissed romoore’s stale review November 29, 2017 20:31

Ready for re-review

static BOOL IsValueType(id someValue, char *objCType) {
if ([someValue isKindOfClass:[NSValue class]]) {
NSValue *asValue = (NSValue *)someValue;
return strncmp(asValue.objCType, objCType, strlen(objCType)) == 0;
Copy link

Choose a reason for hiding this comment

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

If you're going to trust one of the strings, I would trust the asValue.objCType and not the char * passed in.

This whole thing seems safer as a list of functions:

static BOOL IsCGSizeValueType(id someValue);
static BOOL IsCGPointValueType(id someValue);
static BOOL IsCGRectValueType(id someValue);

Copy link
Contributor Author

@jverkoey jverkoey Nov 29, 2017

Choose a reason for hiding this comment

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

My thinking is that id someValue comes from user-land, while objCType comes from this file, so the vector of attack for objCType is smaller than that of id someValue.

Can you help me understand your thinking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I can see how you feel the specifically-typed methods would help make this safer, but I'm not certain that there's a large benefit when the APIs are private and scoped to this file. What do you think?

Copy link

Choose a reason for hiding this comment

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

I was thinking more of the "let's make this more general" risk later on. Someone copy-pastes this out into a header or utility class and keeps the behavior. In that case, the classes passed to this function are more likely to be first-party, but the char * could be anything.

Since within this class we only ever pass the result of the compiler @encode it's safe. I don't think the really is a good "generic" as well as buffer-safe approach. I'm OK merging this as-is since it is currently a static, internal function .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed with that thinking. I'll tidy this up so that copy-pasting is safer.

Jeff Verkoeyen added 3 commits November 29, 2017 16:00
This makes it much more scalable to support arbitrary key paths.
@jverkoey jverkoey merged commit 5e6b649 into develop Nov 29, 2017
@jverkoey jverkoey deleted the objctype branch November 29, 2017 22:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants