[ObjC] Expose a helper for stream error.

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
pull/17562/head
Thomas Van Lenten 8 months ago committed by Copybara-Service
parent 6290dcd244
commit 6ebdc7a4d4
  1. 29
      objectivec/GPBCodedInputStream.m
  2. 1
      objectivec/GPBCodedInputStream_PackagePrivate.h

@ -13,6 +13,10 @@
#import "GPBUtilities_PackagePrivate.h" #import "GPBUtilities_PackagePrivate.h"
#import "GPBWireFormat.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 GPBCodedInputStreamException = GPBNSStringifySymbol(GPBCodedInputStreamException);
NSString *const GPBCodedInputStreamUnderlyingErrorKey = NSString *const GPBCodedInputStreamUnderlyingErrorKey =
@ -28,7 +32,8 @@ NSString *const GPBCodedInputStreamErrorDomain =
// int CodedInputStream::default_recursion_limit_ = 100; // int CodedInputStream::default_recursion_limit_ = 100;
static const NSUInteger kDefaultRecursionLimit = 100; static const NSUInteger kDefaultRecursionLimit = 100;
static void RaiseException(NSInteger code, NSString *reason) { GPB_NOINLINE
void GPBRaiseStreamError(NSInteger code, NSString *reason) {
NSDictionary *errorInfo = nil; NSDictionary *errorInfo = nil;
if ([reason length]) { if ([reason length]) {
errorInfo = @{GPBErrorReasonKey : reason}; errorInfo = @{GPBErrorReasonKey : reason};
@ -44,7 +49,7 @@ static void RaiseException(NSInteger code, NSString *reason) {
GPB_INLINE void CheckRecursionLimit(GPBCodedInputStreamState *state) { GPB_INLINE void CheckRecursionLimit(GPBCodedInputStreamState *state) {
if (state->recursionDepth >= kDefaultRecursionLimit) { if (state->recursionDepth >= kDefaultRecursionLimit) {
RaiseException(GPBCodedInputStreamErrorRecursionDepthExceeded, nil); GPBRaiseStreamError(GPBCodedInputStreamErrorRecursionDepthExceeded, nil);
} }
} }
@ -56,19 +61,19 @@ GPB_INLINE void CheckFieldSize(uint64_t size) {
if (size > 0x7fffffff) { if (size > 0x7fffffff) {
// TODO: Maybe a different error code for this, but adding one is a breaking // TODO: Maybe a different error code for this, but adding one is a breaking
// change so reuse an existing one. // change so reuse an existing one.
RaiseException(GPBCodedInputStreamErrorInvalidSize, nil); GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidSize, nil);
} }
} }
static void CheckSize(GPBCodedInputStreamState *state, size_t size) { static void CheckSize(GPBCodedInputStreamState *state, size_t size) {
size_t newSize = state->bufferPos + size; size_t newSize = state->bufferPos + size;
if (newSize > state->bufferSize) { if (newSize > state->bufferSize) {
RaiseException(GPBCodedInputStreamErrorInvalidSize, nil); GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidSize, nil);
} }
if (newSize > state->currentLimit) { if (newSize > state->currentLimit) {
// Fast forward to end of currentLimit; // Fast forward to end of currentLimit;
state->bufferPos = state->currentLimit; state->bufferPos = state->currentLimit;
RaiseException(GPBCodedInputStreamErrorSubsectionLimitReached, nil); GPBRaiseStreamError(GPBCodedInputStreamErrorSubsectionLimitReached, nil);
} }
} }
@ -110,7 +115,7 @@ static int64_t ReadRawVarint64(GPBCodedInputStreamState *state) {
} }
shift += 7; shift += 7;
} }
RaiseException(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64"); GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64");
return 0; return 0;
} }
@ -201,12 +206,12 @@ int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state) {
state->lastTag = ReadRawVarint32(state); state->lastTag = ReadRawVarint32(state);
// Tags have to include a valid wireformat. // Tags have to include a valid wireformat.
if (!GPBWireFormatIsValidTag(state->lastTag)) { if (!GPBWireFormatIsValidTag(state->lastTag)) {
RaiseException(GPBCodedInputStreamErrorInvalidTag, @"Invalid wireformat in tag."); GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag, @"Invalid wireformat in tag.");
} }
// Zero is not a valid field number. // Zero is not a valid field number.
if (GPBWireFormatGetTagFieldNumber(state->lastTag) == 0) { if (GPBWireFormatGetTagFieldNumber(state->lastTag) == 0) {
RaiseException(GPBCodedInputStreamErrorInvalidTag, GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag,
@"A zero field number on the wire is invalid."); @"A zero field number on the wire is invalid.");
} }
return state->lastTag; return state->lastTag;
} }
@ -231,7 +236,7 @@ NSString *GPBCodedInputStreamReadRetainedString(GPBCodedInputStreamState *state)
NSLog(@"UTF-8 failure, is some field type 'string' when it should be " NSLog(@"UTF-8 failure, is some field type 'string' when it should be "
@"'bytes'?"); @"'bytes'?");
#endif #endif
RaiseException(GPBCodedInputStreamErrorInvalidUTF8, nil); GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidUTF8, nil);
} }
} }
return result; return result;
@ -266,7 +271,7 @@ size_t GPBCodedInputStreamPushLimit(GPBCodedInputStreamState *state, size_t byte
byteLimit += state->bufferPos; byteLimit += state->bufferPos;
size_t oldLimit = state->currentLimit; size_t oldLimit = state->currentLimit;
if (byteLimit > oldLimit) { if (byteLimit > oldLimit) {
RaiseException(GPBCodedInputStreamErrorInvalidSubsectionLimit, nil); GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidSubsectionLimit, nil);
} }
state->currentLimit = byteLimit; state->currentLimit = byteLimit;
return oldLimit; return oldLimit;
@ -286,7 +291,7 @@ BOOL GPBCodedInputStreamIsAtEnd(GPBCodedInputStreamState *state) {
void GPBCodedInputStreamCheckLastTagWas(GPBCodedInputStreamState *state, int32_t value) { void GPBCodedInputStreamCheckLastTagWas(GPBCodedInputStreamState *state, int32_t value) {
if (state->lastTag != value) { if (state->lastTag != value) {
RaiseException(GPBCodedInputStreamErrorInvalidTag, @"Unexpected tag read"); GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag, @"Unexpected tag read");
} }
} }

@ -53,6 +53,7 @@ typedef struct GPBCodedInputStreamState {
CF_EXTERN_C_BEGIN CF_EXTERN_C_BEGIN
void GPBRaiseStreamError(NSInteger code, NSString *reason);
int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state); int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state);
double GPBCodedInputStreamReadDouble(GPBCodedInputStreamState *state); double GPBCodedInputStreamReadDouble(GPBCodedInputStreamState *state);

Loading…
Cancel
Save