Skip to content

Commit cf7abf6

Browse files
committed
[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
1 parent ddf9b76 commit cf7abf6

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

objectivec/GPBCodedInputStream.m

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#import "GPBUtilities_PackagePrivate.h"
1414
#import "GPBWireFormat.h"
1515

16+
// TODO: Consider using on other functions to reduce bloat when
17+
// some compiler optimizations are enabled.
18+
#define GPB_NOINLINE __attribute__((noinline))
19+
1620
NSString *const GPBCodedInputStreamException = GPBNSStringifySymbol(GPBCodedInputStreamException);
1721

1822
NSString *const GPBCodedInputStreamUnderlyingErrorKey =
@@ -28,7 +32,8 @@
2832
// int CodedInputStream::default_recursion_limit_ = 100;
2933
static const NSUInteger kDefaultRecursionLimit = 100;
3034

31-
static void RaiseException(NSInteger code, NSString *reason) {
35+
GPB_NOINLINE
36+
void GPBRaiseStreamError(NSInteger code, NSString *reason) {
3237
NSDictionary *errorInfo = nil;
3338
if ([reason length]) {
3439
errorInfo = @{GPBErrorReasonKey : reason};
@@ -44,7 +49,7 @@ static void RaiseException(NSInteger code, NSString *reason) {
4449

4550
GPB_INLINE void CheckRecursionLimit(GPBCodedInputStreamState *state) {
4651
if (state->recursionDepth >= kDefaultRecursionLimit) {
47-
RaiseException(GPBCodedInputStreamErrorRecursionDepthExceeded, nil);
52+
GPBRaiseStreamError(GPBCodedInputStreamErrorRecursionDepthExceeded, nil);
4853
}
4954
}
5055

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

6368
static void CheckSize(GPBCodedInputStreamState *state, size_t size) {
6469
size_t newSize = state->bufferPos + size;
6570
if (newSize > state->bufferSize) {
66-
RaiseException(GPBCodedInputStreamErrorInvalidSize, nil);
71+
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidSize, nil);
6772
}
6873
if (newSize > state->currentLimit) {
6974
// Fast forward to end of currentLimit;
7075
state->bufferPos = state->currentLimit;
71-
RaiseException(GPBCodedInputStreamErrorSubsectionLimitReached, nil);
76+
GPBRaiseStreamError(GPBCodedInputStreamErrorSubsectionLimitReached, nil);
7277
}
7378
}
7479

@@ -110,7 +115,7 @@ static int64_t ReadRawVarint64(GPBCodedInputStreamState *state) {
110115
}
111116
shift += 7;
112117
}
113-
RaiseException(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64");
118+
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidVarInt, @"Invalid VarInt64");
114119
return 0;
115120
}
116121

@@ -201,12 +206,12 @@ int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state) {
201206
state->lastTag = ReadRawVarint32(state);
202207
// Tags have to include a valid wireformat.
203208
if (!GPBWireFormatIsValidTag(state->lastTag)) {
204-
RaiseException(GPBCodedInputStreamErrorInvalidTag, @"Invalid wireformat in tag.");
209+
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag, @"Invalid wireformat in tag.");
205210
}
206211
// Zero is not a valid field number.
207212
if (GPBWireFormatGetTagFieldNumber(state->lastTag) == 0) {
208-
RaiseException(GPBCodedInputStreamErrorInvalidTag,
209-
@"A zero field number on the wire is invalid.");
213+
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag,
214+
@"A zero field number on the wire is invalid.");
210215
}
211216
return state->lastTag;
212217
}
@@ -231,7 +236,7 @@ int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state) {
231236
NSLog(@"UTF-8 failure, is some field type 'string' when it should be "
232237
@"'bytes'?");
233238
#endif
234-
RaiseException(GPBCodedInputStreamErrorInvalidUTF8, nil);
239+
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidUTF8, nil);
235240
}
236241
}
237242
return result;
@@ -266,7 +271,7 @@ size_t GPBCodedInputStreamPushLimit(GPBCodedInputStreamState *state, size_t byte
266271
byteLimit += state->bufferPos;
267272
size_t oldLimit = state->currentLimit;
268273
if (byteLimit > oldLimit) {
269-
RaiseException(GPBCodedInputStreamErrorInvalidSubsectionLimit, nil);
274+
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidSubsectionLimit, nil);
270275
}
271276
state->currentLimit = byteLimit;
272277
return oldLimit;
@@ -286,7 +291,7 @@ BOOL GPBCodedInputStreamIsAtEnd(GPBCodedInputStreamState *state) {
286291

287292
void GPBCodedInputStreamCheckLastTagWas(GPBCodedInputStreamState *state, int32_t value) {
288293
if (state->lastTag != value) {
289-
RaiseException(GPBCodedInputStreamErrorInvalidTag, @"Unexpected tag read");
294+
GPBRaiseStreamError(GPBCodedInputStreamErrorInvalidTag, @"Unexpected tag read");
290295
}
291296
}
292297

objectivec/GPBCodedInputStream_PackagePrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ typedef struct GPBCodedInputStreamState {
5353

5454
CF_EXTERN_C_BEGIN
5555

56+
void GPBRaiseStreamError(NSInteger code, NSString *reason);
5657
int32_t GPBCodedInputStreamReadTag(GPBCodedInputStreamState *state);
5758

5859
double GPBCodedInputStreamReadDouble(GPBCodedInputStreamState *state);

0 commit comments

Comments
 (0)