Skip to content

Commit

Permalink
[ObjC] Expose a helper for stream error.
Browse files Browse the repository at this point in the history
Also mark the helper as not inline able to avoid the code being over duplicated
within the file with some compiler optimizations turned on.

PiperOrigin-RevId: 654826102
  • Loading branch information
thomasvl committed Jul 24, 2024
1 parent ddf9b76 commit cf7abf6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
29 changes: 17 additions & 12 deletions objectivec/GPBCodedInputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#import "GPBUtilities_PackagePrivate.h"
#import "GPBWireFormat.h"

// TODO: Consider using on other functions to reduce bloat when
// some compiler optimizations are enabled.
#define GPB_NOINLINE __attribute__((noinline))

NSString *const GPBCodedInputStreamException = GPBNSStringifySymbol(GPBCodedInputStreamException);

NSString *const GPBCodedInputStreamUnderlyingErrorKey =
Expand All @@ -28,7 +32,8 @@
// int CodedInputStream::default_recursion_limit_ = 100;
static const NSUInteger kDefaultRecursionLimit = 100;

static void RaiseException(NSInteger code, NSString *reason) {
GPB_NOINLINE
void GPBRaiseStreamError(NSInteger code, NSString *reason) {
NSDictionary *errorInfo = nil;
if ([reason length]) {
errorInfo = @{GPBErrorReasonKey : reason};
Expand All @@ -44,7 +49,7 @@ static void RaiseException(NSInteger code, NSString *reason) {

GPB_INLINE void CheckRecursionLimit(GPBCodedInputStreamState *state) {
if (state->recursionDepth >= kDefaultRecursionLimit) {
RaiseException(GPBCodedInputStreamErrorRecursionDepthExceeded, nil);
GPBRaiseStreamError(GPBCodedInputStreamErrorRecursionDepthExceeded, nil);
}
}

Expand All @@ -56,19 +61,19 @@ GPB_INLINE void CheckFieldSize(uint64_t size) {
if (size > 0x7fffffff) {
// TODO: Maybe a different error code for this, but adding one is a breaking
// change so reuse an existing one.
RaiseException(GPBCodedInputStreamErrorInvalidSize, nil);
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidSize, nil);
}
}

static void CheckSize(GPBCodedInputStreamState *state, size_t size) {
size_t newSize = state->bufferPos + size;
if (newSize > state->bufferSize) {
RaiseException(GPBCodedInputStreamErrorInvalidSize, nil);
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidSize, nil);
}
if (newSize > state->currentLimit) {
// Fast forward to end of currentLimit;
state->bufferPos = state->currentLimit;
RaiseException(GPBCodedInputStreamErrorSubsectionLimitReached, nil);
GPBRaiseStreamError(GPBCodedInputStreamErrorSubsectionLimitReached, nil);
}
}

Expand Down Expand Up @@ -110,7 +115,7 @@ static int64_t ReadRawVarint64(GPBCodedInputStreamState *state) {
}
shift += 7;
}
RaiseException(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64");
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64");
return 0;
}

Expand Down Expand Up @@ -201,12 +206,12 @@ int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state) {
state->lastTag = ReadRawVarint32(state);
// Tags have to include a valid wireformat.
if (!GPBWireFormatIsValidTag(state->lastTag)) {
RaiseException(GPBCodedInputStreamErrorInvalidTag, @"Invalid wireformat in tag.");
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag, @"Invalid wireformat in tag.");
}
// Zero is not a valid field number.
if (GPBWireFormatGetTagFieldNumber(state->lastTag) == 0) {
RaiseException(GPBCodedInputStreamErrorInvalidTag,
@"A zero field number on the wire is invalid.");
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag,
@"A zero field number on the wire is invalid.");
}
return state->lastTag;
}
Expand All @@ -231,7 +236,7 @@ int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state) {
NSLog(@"UTF-8 failure, is some field type 'string' when it should be "
@"'bytes'?");
#endif
RaiseException(GPBCodedInputStreamErrorInvalidUTF8, nil);
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidUTF8, nil);
}
}
return result;
Expand Down Expand Up @@ -266,7 +271,7 @@ size_t GPBCodedInputStreamPushLimit(GPBCodedInputStreamState *state, size_t byte
byteLimit += state->bufferPos;
size_t oldLimit = state->currentLimit;
if (byteLimit > oldLimit) {
RaiseException(GPBCodedInputStreamErrorInvalidSubsectionLimit, nil);
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidSubsectionLimit, nil);
}
state->currentLimit = byteLimit;
return oldLimit;
Expand All @@ -286,7 +291,7 @@ BOOL GPBCodedInputStreamIsAtEnd(GPBCodedInputStreamState *state) {

void GPBCodedInputStreamCheckLastTagWas(GPBCodedInputStreamState *state, int32_t value) {
if (state->lastTag != value) {
RaiseException(GPBCodedInputStreamErrorInvalidTag, @"Unexpected tag read");
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag, @"Unexpected tag read");
}
}

Expand Down
1 change: 1 addition & 0 deletions objectivec/GPBCodedInputStream_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef struct GPBCodedInputStreamState {

CF_EXTERN_C_BEGIN

void GPBRaiseStreamError(NSInteger code, NSString *reason);
int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state);

double GPBCodedInputStreamReadDouble(GPBCodedInputStreamState *state);
Expand Down

0 comments on commit cf7abf6

Please sign in to comment.