Skip to content

[NFC] Fix uninitialized scalar field in constructor. #118324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 5, 2024

Conversation

zahiraam
Copy link
Contributor

@zahiraam zahiraam commented Dec 2, 2024

Non-static class field is not initialized in constructor.

@zahiraam zahiraam changed the title [NFC] Fix uninitialized data member in constructor. [NFC] Fix uninitialized scalar field in constructor. Dec 2, 2024
@zahiraam zahiraam marked this pull request as ready for review December 2, 2024 19:19
@llvmbot
Copy link
Member

llvmbot commented Dec 2, 2024

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clangd

Author: Zahira Ammarguellat (zahiraam)

Changes

Non-static class field is not initialized in constructor.


Full diff: https://github.com/llvm/llvm-project/pull/118324.diff

2 Files Affected:

  • (modified) clang-tools-extra/clangd/index/MemIndex.h (+1)
  • (modified) clang-tools-extra/clangd/index/dex/Dex.h (+1)
diff --git a/clang-tools-extra/clangd/index/MemIndex.h b/clang-tools-extra/clangd/index/MemIndex.h
index fba2c1a7120a2b..879d7750ac0480 100644
--- a/clang-tools-extra/clangd/index/MemIndex.h
+++ b/clang-tools-extra/clangd/index/MemIndex.h
@@ -43,6 +43,7 @@ class MemIndex : public SymbolIndex {
     KeepAlive = std::shared_ptr<void>(
         std::make_shared<Payload>(std::move(BackingData)), nullptr);
     this->BackingDataSize = BackingDataSize;
+    this->IdxContents = IndexContents::All;
   }
 
   template <typename SymbolRange, typename RefRange, typename RelationRange,
diff --git a/clang-tools-extra/clangd/index/dex/Dex.h b/clang-tools-extra/clangd/index/dex/Dex.h
index 69e161d51135b6..f907c9a55b935b 100644
--- a/clang-tools-extra/clangd/index/dex/Dex.h
+++ b/clang-tools-extra/clangd/index/dex/Dex.h
@@ -58,6 +58,7 @@ class Dex : public SymbolIndex {
     KeepAlive = std::shared_ptr<void>(
         std::make_shared<Payload>(std::move(BackingData)), nullptr);
     this->BackingDataSize = BackingDataSize;
+    this->IdxContents = IndexContents::All;
   }
 
   template <typename SymbolRange, typename RefsRange, typename RelationsRange,

@zahiraam zahiraam requested a review from tahonermann December 2, 2024 20:58
Copy link
Contributor

@tahonermann tahonermann left a comment

Choose a reason for hiding this comment

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

How did you decide on IndexContents::All as the default initialization value. I would be more inclined towards IndexContents::None.

I think it would be preferred to use a member initializer in the declaration of IdxContents. That will ensure an initialization is performed if additional constructors are added.

@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 2, 2024

How did you decide on IndexContents::All as the default initialization value. I would be more inclined towards IndexContents::None.

I think it would be preferred to use a member initializer in the declaration of IdxContents. That will ensure an initialization is performed if additional constructors are added.

Actually that makes more sense and it will obviously be assigned to IndexContents::None, which after thoughts seems more correct.

@HighCommander4
Copy link
Collaborator

HighCommander4 commented Dec 2, 2024

Seems like MemIndex::IdxContents has the same issue.

It's a bit suspicious to me that the other DexIndex and MemIndex constructors are not passing in an IndexContents value. Either there's a bug lurking there, or the field is only relevant in certain situations (e.g. only for dynamic indexes) in which case that should be documented at the field's declaration. Would you mind filing a follow-up issue about auditing this situation more carefully, so it doesn't get forgotten?

@kadircet
Copy link
Member

kadircet commented Dec 3, 2024

idxcontents is only used when we have a set of file names available. hence this was only set via relevant constructors and rest of the runtime doesn't use the field.

but we don't construct tons of new index instances every second. so it's totally fine to just initialize them to none

@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 3, 2024

Seems like MemIndex::IdxContents has the same issue.

It's a bit suspicious to me that the other DexIndex and MemIndex constructors are not passing in an IndexContents value. Either there's a bug lurking there, or the field is only relevant in certain situations (e.g. only for dynamic indexes) in which case that should be documented at the field's declaration. Would you mind filing a follow-up issue about auditing this situation more carefully, so it doesn't get forgotten?

I could also initialize all the others to None and see if this doesn't break any tests?

@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 3, 2024

idxcontents is only used when we have a set of file names available. hence this was only set via relevant constructors and rest of the runtime doesn't use the field.

but we don't construct tons of new index instances every second. so it's totally fine to just initialize them to none

Thanks for the explanation.

@zahiraam zahiraam requested a review from tahonermann December 3, 2024 17:19
@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 4, 2024

@HighCommander4 Is that good as a change? Do you still want an issue to be opened?
@tahonermann can you please review? Thanks.

Copy link
Member

@kadircet kadircet left a comment

Choose a reason for hiding this comment

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

thanks, lgtm!

@zahiraam zahiraam merged commit 4443314 into llvm:main Dec 5, 2024
8 checks passed
@HighCommander4
Copy link
Collaborator

Do you still want an issue to be opened?

I sent out a follow-up PR at #118906 to add a comment describing the situations in which IdxContents is used, to avoid future confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants