Skip to content

ICU-22779 Fixed ICU4C DataBuilderCollationIterator initialization order issue - #4111

Merged
markusicu merged 1 commit into
unicode-org:mainfrom
yumaoka:icu22779-fix-initialization
Sep 14, 2026
Merged

markusicu merged 1 commit into
unicode-org:mainfrom
yumaoka:icu22779-fix-initialization

Conversation

@yumaoka

@yumaoka yumaoka commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

There is an initialization ordering problem in the DataBuilderCollationIterator constructor. GCC 14 with -Werror=uninitialized will cause an compilation error.

Checklist

  • Required: Issue filed: ICU-22779
  • Required: The PR title must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
  • Required: Each commit message must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
  • Issue accepted (done by Technical Committee after discussion)
  • Tests included, if applicable
  • API docs and/or User Guide docs changed or added, if applicable
  • Approver: Feel free to merge on my behalf

@yumaoka

yumaoka commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

@markusicu ICU4J implementation takes CollationData in the constructo as below -

    private static final class DataBuilderCollationIterator extends CollationIterator {
        DataBuilderCollationIterator(CollationDataBuilder b, CollationData newData) {
            super(newData, /* numeric= */ false);

In this PR, I kept the constructor's signature unchanged, but added CollationIterator constructor which allows a subclass to set CollationData later. Do you want to make a change the DataBuilderCollationIterator to have a pointer to CollationData, then delete it in the destructor? Or, are you OK with this change (CollationIterator to initialize members in the new protected constructor and a separated setter method)?

@markusicu

Copy link
Copy Markdown
Member

@markusicu ICU4J implementation takes CollationData in the constructo as below -

    private static final class DataBuilderCollationIterator extends CollationIterator {
        DataBuilderCollationIterator(CollationDataBuilder b, CollationData newData) {
            super(newData, /* numeric= */ false);

In this PR, I kept the constructor's signature unchanged, but added CollationIterator constructor which allows a subclass to set CollationData later. Do you want to make a change the DataBuilderCollationIterator to have a pointer to CollationData, then delete it in the destructor? Or, are you OK with this change (CollationIterator to initialize members in the new protected constructor and a separated setter method)?

Interesting... We construct the base class with a pointer to the buildData field (type: CollationData) which isn't initialized yet. The base class (CollationIterator) stores the pointer itself, which should be fine; and it also copies the buildData.trie pointer, which isn't initialized yet.

Why does this work at all?
The CollationData constructor is nearly trivial; it initializes the trie pointer to nullptr.
Some later code must then create the trie and set the pointer in both the buildData and in the CollationIterator base.
So whether the constructors copy a nullptr or an uninitialized value has no effect, except making a smart compiler unhappy.

The simplest right answer is probably to add a new CollationIterator constructor that simply sets the trie pointer to nullptr.

Comment thread icu4c/source/i18n/collationiterator.h Outdated
Comment thread icu4c/source/i18n/collationdatabuilder.cpp Outdated
@markusicu

Copy link
Copy Markdown
Member

PS: It looks like CollationIterator has both a CollationData pointer and a trie pointer which is CollationData->trie “only as an optimization”. Which means that, if we didn't care about an additional pointer indirection, we might be able to simplify this by removing the CollationIterator.trie field and always using an indirect access via data->trie.

I don't know if common compilers can be relied on optimizing this to the equivalent code.

@yumaoka
yumaoka requested a review from markusicu August 13, 2026 18:55
@yumaoka
yumaoka force-pushed the icu22779-fix-initialization branch from 49f7a24 to fc31767 Compare August 14, 2026 17:54
@jira-pull-request-webhook

Copy link
Copy Markdown

Hooray! The files in the branch are the same across the force-push. 😃

~ Your Friendly Jira-GitHub PR Checker Bot

@markusicu markusicu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, follow-up questions.


Re #4111 (comment)
let's not do that -- let's keep the optimization explicit, rather than trusting the compiler.

Comment thread icu4c/source/i18n/collationiterator.h Outdated
Comment thread icu4c/source/i18n/collationdatabuilder.cpp Outdated
@yumaoka
yumaoka requested a review from markusicu September 11, 2026 16:50
markusicu
markusicu previously approved these changes Sep 11, 2026

@markusicu markusicu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to fix the typo; i can quickly re-stamp

Comment thread icu4c/source/i18n/collationiterator.h Outdated
@yumaoka
yumaoka force-pushed the icu22779-fix-initialization branch from e438ff0 to d3d282e Compare September 11, 2026 17:47
@jira-pull-request-webhook

Copy link
Copy Markdown

Hooray! The files in the branch are the same across the force-push. 😃

~ Your Friendly Jira-GitHub PR Checker Bot

@markusicu

Copy link
Copy Markdown
Member

typo still there
rerunning failed msvc dist workflow

@jira-pull-request-webhook

Copy link
Copy Markdown

Notice: the branch changed across the force-push!

  • icu4c/source/i18n/collationiterator.h is different

View Diff Across Force-Push

~ Your Friendly Jira-GitHub PR Checker Bot

@markusicu

Copy link
Copy Markdown
Member

@echeran @mihnita @yumaoka @eggrobin could i ask for a rubber stamp?
i approved yoshito's PR but then fixed a typo

@mihnita mihnita changed the title ICU-22779 Fixed ICU4C DataBuilderCollationIterator initialization ord… ICU-22779 Fixed ICU4C DataBuilderCollationIterator initialization order issue Sep 14, 2026
@markusicu
markusicu merged commit 45a8a28 into unicode-org:main Sep 14, 2026
98 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants