Skip to content
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

Init index_in_metadata_ without condition #2523

Merged
merged 1 commit into from Dec 22, 2016

Conversation

jbrianceau
Copy link
Contributor

Chromium MemorySanitizer (MSan) reports a use-of-uninitialized-value
warning because of index_in_metadata_ attribute of EnumGenerator
class (introduced in 5a76e63). Fix
it by initializing it to 0.

@grpc-kokoro
Copy link

Thanks for your pull request. The automated tests will run as soon as one of the admins verifies this change is ok for us to run on our infrastructure.

1 similar comment
@grpc-kokoro
Copy link

Thanks for your pull request. The automated tests will run as soon as one of the admins verifies this change is ok for us to run on our infrastructure.

@bazel-io
Copy link

Can one of the admins verify this patch?

@jbrianceau
Copy link
Contributor Author

See https://codereview.chromium.org/2590803003/ for further details.
@acozzette, @xfxyjwf for review.

@acozzette
Copy link
Member

ok to test

@acozzette
Copy link
Member

@jbrianceau Thanks for sending this pull request. It seems to me like the root of the problem is that for each kind of generator, we assign that index_in_metadata_ variable here, but that's inside a block that is only executed if HasDescriptorMethods(...) is true (i.e. only for non-lite). So I think the right solution would be to change that code so that we assign the variable unconditionally, regardless of whether we're using lite. I think with your current solution the variable is always assigned 0 in lite mode, and this works for now only because we never actually use it for lite. But if we end up needing it for lite in the future it will be confusing because we will get the wrong value (always 0).

Sorry about the test failures by the way; I believe those were caused by a separate thing I broke and should now be fixed.

@acozzette
Copy link
Member

retest this please

@jbrianceau
Copy link
Contributor Author

So I think the right solution would be to change that code so that we assign the variable unconditionally, regardless of whether we're using lite.

You're right. If I add a else case for HasDescriptorMethods(...) looking like this, it fixes MSan warning too :

  if (HasDescriptorMethods(file_, options_)) {
    [...]
  } else {
    for (int i = 0; i < enum_generators_.size(); i++) {
      // Set index_in_metadata_ value to avoid use-of-uninitialized-value warning
      enum_generators_[i]->index_in_metadata_ = i;
    }
  }

But if we end up needing it for lite in the future it will be confusing because we will get the wrong value (always 0).

Setting it to zero in EnumGenerator constructor shouldn't prevent it to be set to the right value after, am I missing something ?

What solution do you prefer @acozzette ? Current patch + the above else case or just the above else case (i.e. no init in EnumGenerator's constructor)

@acozzette
Copy link
Member

That's true, setting it to zero wouldn't prevent it from being set to the right value later on. I guess I would lean slightly toward not setting it in the constructor, though; that way if we forget to set it again then MSAN will hopefully catch the bug like it did this time.

About the else case, maybe instead of adding an else you could just move the code outside of the if block so that it's not duplicated. I think we probably want to do this for message_generators_ and service_generators_ in addition to enum_generators.

I guess the message_generators_ one is a little complicated because part of that probably has to happen inside the if since it has a side effect (from the GenerateDescriptorDeclarations call):

for (int i = 0; i < message_generators_.size(); i++) {
  message_generators_[i]->index_in_metadata_ = i;
  message_generators_[i]->GenerateDescriptorDeclarations(printer);
}

So I would say maybe have three loops for initializing index_in_metadata_ before the if block, and but keep this inside the if:

for (int i = 0; i < message_generators_.size(); i++) {
  message_generators_[i]->GenerateDescriptorDeclarations(printer);
}

Does that work?

@jbrianceau
Copy link
Contributor Author

that way if we forget to set it again then MSAN will hopefully catch the bug like it did this time.

Good point, agreed.

Does that work?

Yes, I've just tested it quickly in my Chromium build and it seems to be ok.
I'm going to update the PR.

Chromium MemorySanitizer (MSan) reports use-of-uninitialized-value
of index_in_metadata_ attribute from EnumGenerator class. Fix these
warnings by initializing these attributes without condition.
@jbrianceau jbrianceau changed the title Set an initial value for index_in_metadata_ Init index_in_metadata_ without condition Dec 22, 2016
@jbrianceau
Copy link
Contributor Author

@acozzette, please take another look

@acozzette
Copy link
Member

Thanks, @jbrianceau!

@acozzette acozzette merged commit f52e188 into protocolbuffers:master Dec 22, 2016
@jbrianceau jbrianceau deleted the init-index-in-metadata branch December 23, 2016 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants