Commit graph

178 commits

Author SHA1 Message Date
Tony Liao
dd9d2ad02d [ObjC] Carry recursion depth across MessageSet item parsing (#27571)
## Summary

`GPBMessage::parseMessageSet:` reads each MessageSet item's payload bytes into
a separate buffer (because the type-id and payload tags can appear in any
order), then allocates a fresh `GPBCodedInputStream` to decode that buffer. The
fresh stream's `state_.recursionDepth` starts at `0` even when the parent
stream is already several MessageSet items deep, so a chain of nested
MessageSet items grows the native call stack without ever tripping the
documented `kDefaultRecursionLimit` (100). Other parser entry points that
recurse on the same stream (`readGroup:`, `readMessage:`, `readMapEntry:`,
`SkipToEndGroupInternal`) already increment and check `recursionDepth`; the
MessageSet path was the only spot where depth tracking did not cross the stream
boundary.

This change adds a package-private `-initWithData:parentRecursionDepth:`
initializer to `GPBCodedInputStream` that seeds the child stream's depth from
the parent's and runs `CheckRecursionLimit` before parsing begins. To prevent
memory leaks if `CheckRecursionLimit` raises an exception during
initialization, the check is wrapped in a `@try/@catch` block that releases
`self`. `parseMessageSet:` uses that initializer, so depth tracking carries
across the fresh stream. This mirrors the depth-inheritance done by the C++
`ParseContext` spawn helper.

Regression coverage: `testParseMessageSetRecursionDepthCarriedFromParent` in
`GPBWireFormatTests` builds MessageSet-of-MessageSet payloads (each layer is
`MSetMessage -> MSetMessageExtension1.recursive -> MSetMessage -> ...`, adding
2 to the recursion depth per layer). It verifies that 50 layers (depth 100,
`kDefaultRecursionLimit`) parses successfully, while 51 layers (depth 101,
`kDefaultRecursionLimit + 1`) fails with
`GPBCodedInputStreamErrorRecursionDepthExceeded`. Existing
`testErrorRecursionDepthReached` and the rest of `GPBWireFormatTests` continue
to pass unchanged.

## Test plan

- Existing ObjC test suite passes (`GPBWireFormatTests`,
  `GPBMessageTests+Serialization`, `GPBCodedInputStreamTests`,
  `GPBUnknownFieldsTest`).
- New `testParseMessageSetRecursionDepthCarriedFromParent` passes for both 50
  layers (passing at kDefaultRecursionLimit) and 51 layers (failing at
  kDefaultRecursionLimit + 1).

Closes #27571

PiperOrigin-RevId: 959041690
2026-08-04 08:41:24 -07:00
Nicholas Levin
13ba8cc490 Use XCTAssertEqualObjects to compare enum values with NSNumbers in the GPBDescriptorTests.
Issue identified with tests while evaluating raising the minimum OS version of tests to iOS 15.0.

PiperOrigin-RevId: 932434041
2026-06-15 06:55:05 -07:00
Protobuf Team Bot
290a5376ce [ObjC] Add DEBUG-only validations to extension values in GPBMessage, along with tests.
PiperOrigin-RevId: 902869088
2026-04-20 16:11:48 -07:00
Protobuf Team Bot
18f4d6130e [ObjC] Fix over-release in GPBTextFormatForMessage
When handling deeply nested unknown fields, the `subIndent` string was being over-released in `GPBTextFormatForMessage`, leading to crashes when generating text format for messages with deeply nested unknown fields.

The added test case reproduces this crash by creating a message with unknown fields nested deep enough to cause the indent string to be heap-allocated, and then adding siblings at that depth, triggering the incorrect release.

Removing the redundant `[subIndent release]` resolves the issue.

PiperOrigin-RevId: 895495564
2026-04-06 14:19:57 -07:00
Protobuf Team Bot
e3bee077db Fix naming convention in c_function extension syntax
- ObjC file prefix added to file-scoped extension descriptors
- Proto package prefix removed from message-scoped extension descriptors (uniqueness of ScopeMessage prefix is sufficient, reduces verbosity).

PiperOrigin-RevId: 876467061
2026-02-27 16:48:36 -08:00
Protobuf Team Bot
e1f2f52720 Modify unit tests to use c_function extension syntax via ifdefs
PiperOrigin-RevId: 875974424
2026-02-26 17:51:54 -08:00
Protobuf Team Bot
a0b7794380 Automated Code Change
PiperOrigin-RevId: 860992485
2026-01-25 20:54:45 -08:00
Protobuf Team Bot
d2a42e74b9 This CL starts failing on [unverified_lazy = true] on extensions, which have been
no-op anyway. OSS users shouldn't be affected.

PiperOrigin-RevId: 856798329
2026-01-15 13:39:23 -08:00
Thomas Van Lenten
d4bef77490 [ObjC] Fix some tests to not violate GPBCodedOutputStream invariants.
If the class is writing to an NSOutputStream, the buffer must always
be used to batch things up. The tests were accidentally making a zero
length buffer which isn't valid.

Fix the tests to not do this.

Make the core code assert in debug to ensure the invariant can't be
broken.

PiperOrigin-RevId: 817153199
2025-10-09 06:17:35 -07:00
Mike Kruskal
2aae07eafa Emit hassers for oneofs in objectivec.
This conforms to our other implementations which emit hassers for oneofs and already do track presence.

PiperOrigin-RevId: 816837388
2025-10-08 13:31:34 -07:00
Thomas Van Lenten
e39b79f94e [ObjC] Remove the C++ version check in the test.
The enum still work, and it doesn't seem worth keeping this gate
that has to be updated. Xcode 26 beta 1 tripped this.

PiperOrigin-RevId: 770210931
2025-06-11 10:29:18 -07:00
Protobuf Team Bot
b35e715854 [GPBTestUtil] Fix test with initialization of bytes with null characters
Replaces the string literal initialization to an explicit character array initialization to avoid the implicit null terminator. The string  "\1\0\2\3\0\5" has 7 bytes because of the implicit null terminator.

PiperOrigin-RevId: 769253763
2025-06-09 12:46:07 -07:00
Thomas Van Lenten
aefe47c6c4 [ObjC] Add a test for unknown runtime markers.
PiperOrigin-RevId: 740876067
2025-03-26 13:10:14 -07:00
Thomas Van Lenten
6b7233bd4e [ObjC] Add a new compile test for the new generated code format.
PiperOrigin-RevId: 740829890
2025-03-26 11:00:08 -07:00
Thomas Van Lenten
86ab252a9f [ObjC] Deprecate GPBFieldDescriptor.isOptional.
This also deprecates the internal `GPBFieldOptional` flag, the generator will be
updated to stop using it in the future.

PiperOrigin-RevId: 740378571
2025-03-25 09:38:38 -07:00
Thomas Van Lenten
892ba26862 [ObjC] Add a new test for generated file support
This is the new current version, so capture a test that
ensure it works so this are already set up for the next time
the generate format has to evolve.

PiperOrigin-RevId: 729193386
2025-02-20 11:55:03 -08:00
Thomas Van Lenten
2689ac4fd2 [ObjC] Mark the 30007 test case for when it can be removed.
PiperOrigin-RevId: 727066984
2025-02-14 14:39:28 -08:00
Thomas Van Lenten
e67102d73d [ObjC] Only promote known extensions in messageset
When a field is encoded normally in a MessageSet, only promote
it if it is a known extension, if it is an unknown extension, then leave
it as an unknown field.

PiperOrigin-RevId: 726062078
2025-02-12 08:19:23 -08:00
Thomas Van Lenten
6ace5550c8 [ObjC] Within a MessageSet parse as a normal extension also.
This fixes the issue the recent conformance test call out as there
is a desire to handle this.

PiperOrigin-RevId: 725623031
2025-02-11 07:46:19 -08:00
Thomas Van Lenten
bf068d9e0e [ObjC] Make a test for the 30007 format
Pull in some 30007 generated sources to help ensure the format
doesn't break when moving to a new generated format.

PiperOrigin-RevId: 715082290
2025-01-13 13:17:28 -08:00
Thomas Van Lenten
cffa590260 [ObjC] Breaking Change: Remove support for older generated code.
Remove runtime methods that support the Objective-C gencode from before the 3.22.x release.

PiperOrigin-RevId: 684462445
2024-10-10 09:52:43 -07:00
Thomas Van Lenten
2b93422f7e [ObjC] Breaking Change: Remove GPBUnknownFieldSet.
GPBUnknownFieldSet and the related apis have been replaced by
GPBUnknownFields. The new api allows the Objective-C Protobuf
implementation to be fully conformant around requirements for
parsing/re-serialization of unknown fields.

PiperOrigin-RevId: 684140581
2024-10-09 16:14:56 -07:00
Protobuf Team Bot
23d1b55761 Fix build in C23 with -Wunused-parameter.
As of C23, `va_start` no longer requires a second argument, and ignores it when provided. So, the parameter named `unused` is now...unused.

PiperOrigin-RevId: 683183750
2024-10-07 07:59:45 -07:00
Nathan Baulch
e4cbc79ab4 Fix minor typos (#17682)
Just thought I'd contribute some typo fixes that keep tripping up CI/CD checks in my projects. Nothing controversial (hopefully), just 174 simple fixes.

Use the following command to get a quick and dirty summary of the specific corrections made:
```shell
git diff HEAD^! --word-diff-regex='\w+' -U0 \
  | grep -E '\[\-.*\-\]\{\+.*\+\}' \
  | sed -r 's/.*\[\-(.*)\-\]\{\+(.*)\+\}.*/\1 \2/' \
  | sort | uniq -c | sort -n
```

FWIW, the top typos are:
* trimed → trimmed (37)
* substract → subtract (7)
* qualifed → qualified (7)
* extesion → extension (6)
* mising → missing (5)
* btye → byte (4)
* likey → likely (4)
* candicate → candidate (3)
* decriptor → descriptor (3)
* inherting → inheriting (3)
* colletion → collection (3)
* caluclated → calculated (3)
* unititialized → uninitialized (3)
* implemting → implementing (3)
* binrary → binary (3)
* descripor → descriptor (3)
* negitive → negative (3)

Closes #17682

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/17682 from NathanBaulch:typos d41762d137
PiperOrigin-RevId: 677074418
2024-09-20 20:50:06 -07:00
Thomas Van Lenten
5710bfcbb9 [ObjC] Use more complete imports
Import both the public and private header into impls when the
private is needed.

Also update the tests to use more complete imports.

PiperOrigin-RevId: 671388271
2024-09-05 08:52:31 -07:00
Thomas Van Lenten
cdd3cf7ed4 [ObjC] Make debug runtime tell developers to update their generated code.
Migrate some internals of the library off the older apis.

Also mark some of the old apis as deprecated, but the old generated code
suppressed warnings broadly to support protobuf deprecations.

PiperOrigin-RevId: 668003974
2024-08-27 08:50:22 -07:00
Thomas Van Lenten
e98e8edb15 [ObjC] Add api to add a field to another collection of unknown fields.
PiperOrigin-RevId: 663424215
2024-08-15 13:18:15 -07:00
Thomas Van Lenten
7155a7e95d [ObjC] Fix GPBUnknownField/GPBUnknownFields copy.
Since a group GPBUnknownField uses a GPBUnknownFields and that is mutable, it needs to be copied so two instances aren't linked.

PiperOrigin-RevId: 663323972
2024-08-15 09:01:04 -07:00
Thomas Van Lenten
d794139f09 [ObjC] Deprecate GPBUnknownFieldSet and related apis.
GPBUnknownFields will be going away in the next major release of the ObjC
Protobuf runtime. Code should be updated to make use of GPBUnknownFields
instead.

PiperOrigin-RevId: 659963056
2024-08-06 08:26:33 -07:00
Thomas Van Lenten
1a300ce648 [ObjC] More tests around unknown to known failure cases.
PiperOrigin-RevId: 659715244
2024-08-05 16:13:03 -07:00
Thomas Van Lenten
f26bdff7cc [ObjC] Support errors when merging unknown fields to a message.
PiperOrigin-RevId: 658782615
2024-08-02 07:23:19 -07:00
Thomas Van Lenten
fe7b64e934 [ObjC] Add apis for removing things from GPBUnknownFields.
PiperOrigin-RevId: 658538490
2024-08-01 14:27:44 -07:00
Thomas Van Lenten
5ceae5f3b0 [ObjC] Update tests to use both unknown fields apis.
Also fixes edge case where merging into an autocreated message for a field
wasn't marking the field as set in the parent.

PiperOrigin-RevId: 655196339
2024-07-23 09:38:57 -07:00
Thomas Van Lenten
55e997ea22 [ObjC] Deprecate GPBTextFormatForUnknownFieldSet().
`GPBTextFormatForMessage()` will contain any information for unknown fields.
And it clears the way for `GPBUnknownFieldSet` to eventually be deprecated
also.

PiperOrigin-RevId: 655184316
2024-07-23 09:03:12 -07:00
Thomas Van Lenten
d44c0d577c [ObjC] Parsing helper and tests around unknown fields.
Make a new internal api for collecting up the unknown group fields
that will be used in a future change.

Add testing for the new api and for unknown field parsing of groups in
general.

PiperOrigin-RevId: 654875605
2024-07-22 13:31:59 -07:00
Thomas Van Lenten
af794965f9 [ObjC] Add test around enum unknown values.
PiperOrigin-RevId: 654858735
2024-07-22 12:44:34 -07:00
Thomas Van Lenten
ac84411e4e [ObjC] Support MessageSet with GPBUnknownFields.
Update the initialization from a message to deal with the current differences.

Update some tests to cover the usage.

PiperOrigin-RevId: 652892603
2024-07-16 10:29:29 -07:00
Thomas Van Lenten
8b96aa1e4c [ObjC] Update MessageSet Parsing.
- Follow upb and only accept the first value for `type_id` and `message`
- Reflow some of the logic to hopefully make things a little easier to follow/clear.
- Validate some more assertion about things the extensions for a MessageSet.

PiperOrigin-RevId: 652545240
2024-07-15 11:29:04 -07:00
Thomas Van Lenten
543b404920 [ObjC] Complete the tests for GPBUnknownFields/GPBUnknownField
Ensure all apis are called directly in tests.

Correct `GPBUnknownField` `-copy` to not have to make a new instance
since it is immutable in the new form.

PiperOrigin-RevId: 650690942
2024-07-09 11:08:57 -07:00
Thomas Van Lenten
d05c3feb39 [ObjC] Bring over more unknown fields tests.
Port of the interesting `GPBUnknownFieldsSet` tests to `GPBUnknownFields`
as they provide coverage of general handing of unknown fields during parsing.

Also tweak the `Set` tests to actually use a registry to confirm behaviors around
extensions where the test previously wasn't as complete.

PiperOrigin-RevId: 650660181
2024-07-09 09:43:56 -07:00
Thomas Van Lenten
c2299860fd [ObjC] Add initializing a GPBUnknownFields from a message.
PiperOrigin-RevId: 650388684
2024-07-08 15:14:55 -07:00
Thomas Van Lenten
6e5a867f3d [ObjC] Start of the internal api for GPBUnknownFields
Add in the support for pushing the data back onto a `GPBMessage`.

PiperOrigin-RevId: 649125923
2024-07-03 10:44:09 -07:00
Thomas Van Lenten
c3d0ffe4cf [ObjC] Add comment on import for future reference.
PiperOrigin-RevId: 649106953
2024-07-03 09:50:22 -07:00
Thomas Van Lenten
6750ed8cd8 [ObjC] Add Swift helpers for GPBUnknownFields/GPBUnknownField.
`GPBUnknownFields` additions:
- Provide `Optional` based apis for the `getFirst*` apis.
- Map the `NSFastEnumeration` over as a `Sequence` to support looping over the fields.

`GPBUnknownField` addition:
- Add an `enum` with associated values to provide a more type-safe way for inspection.

PiperOrigin-RevId: 649090744
2024-07-03 08:58:32 -07:00
Thomas Van Lenten
47f633eae6 [ObjC] Introduce the new GPBUnknownFields type.
`GPBUnknownFields` will be the eventually replacement for `GPBUnknownFieldSet`. This
introduces the type and the changes to `GPBUnknownField`.

The new api will preserve the wire ordering of unknown fields. This is now checked
in conformance tests.

While this adds the type changes and tests them, it does not yet wire the changes
in to the rest of the Runtime, so the conformance tests still done pass.
`GPBUnknownFieldSet` also hasn't been deprecated yet, that will come in later with
the wiring in to the runtime.

PiperOrigin-RevId: 648361455
2024-07-01 07:32:10 -07:00
Protobuf Team Bot
01b0b8e06e Added a new public API bytesUntilLimit to GPBCodedInputStream.
This simply exposes preexisting logic. This read-only API is for convenience and doesn't reveal any information that was not already available. `[GPBCodedInputStream position]` is already a public API. The `limit` is known to the user because it's specified by them.

PiperOrigin-RevId: 635502694
2024-05-20 10:44:46 -07:00
Sandy Zhang
c99cf4bbfa Add java to reserved names to escape extensions to java_. This may break existing references to gencode for extensions named java.
This prevents shadowing of `java.lang` package commonly used in protobuf gencode. Existing extensions named `java` may or may not previously fail to compile depending on if the contents of their .proto result in gencode using `java.lang`. This is needed to fix `java_features.proto` lite gencode since enum gencode uses `java.lang`. Fields named `java` should already be escaped.

*Warning: This may break user code for existing protos with extensions named `java`. References to the extension should be renamed to use `java_` e.g. registry.add(GeneratedClassName.java_)*

PiperOrigin-RevId: 632508249
2024-05-10 08:49:22 -07:00
Thomas Van Lenten
9f52d5e380 [ObjC] Fix issue using GPBBootstrap.h only.
GPB_ENUM_FWD_DECLARE needs `int32_t`, so add the import so GPBBootstrap.h
stands on its own without needing users to import something else.

Add another compile test file to ensure this doesn't break.

PiperOrigin-RevId: 610403337
2024-02-26 07:37:41 -08:00
Chris Kennelly
34a04c1fd5 Remove lazy=true from repeated fields.
This is currently a no-op in protoc.

PiperOrigin-RevId: 604457261
2024-02-05 15:53:12 -08:00
Protobuf Team Bot
7d508b41df Internal change
PiperOrigin-RevId: 599669392
2024-01-19 06:55:13 -08:00