From cf7abf6c6bbb2a578901e312e3ec08d69049b274 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Mon, 22 Jul 2024 11:08:16 -0700 Subject: [PATCH] [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 --- objectivec/GPBCodedInputStream.m | 29 +++++++++++-------- .../GPBCodedInputStream_PackagePrivate.h | 1 + 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/objectivec/GPBCodedInputStream.m b/objectivec/GPBCodedInputStream.m index c39648b58832..b7256007178f 100644 --- a/objectivec/GPBCodedInputStream.m +++ b/objectivec/GPBCodedInputStream.m @@ -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 = @@ -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}; @@ -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); } } @@ -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); } } @@ -110,7 +115,7 @@ static int64_t ReadRawVarint64(GPBCodedInputStreamState *state) { } shift += 7; } - RaiseException(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64"); + GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64"); return 0; } @@ -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; } @@ -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; @@ -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; @@ -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"); } } diff --git a/objectivec/GPBCodedInputStream_PackagePrivate.h b/objectivec/GPBCodedInputStream_PackagePrivate.h index fbeecf130cea..0c4c3c22a1b8 100644 --- a/objectivec/GPBCodedInputStream_PackagePrivate.h +++ b/objectivec/GPBCodedInputStream_PackagePrivate.h @@ -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);