[PATCH] D43230: [clangd] Explicitly initialize all primitive fields in Protocol.h

Eric Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 13 10:21:36 PST 2018


ioeric added a comment.

In https://reviews.llvm.org/D43230#1006469, @ilya-biryukov wrote:

> In https://reviews.llvm.org/D43230#1006104, @ioeric wrote:
>
> > But I think it's safe and probably easier to rely on default values of primitive types like int, bool etc
>
>
> It's not always safe, as primitive types are sometimes left uninitialized (e.g. when constructed on the stack) and reading an uninitialized value is UB.


Oh, you are absolutely right! I I think l had protos get into my mind...

> 
> 
>> but do we really want to make this a requirement for future changes or even in our coding style?
> 
> I think we should, default values are less surprising than UB. Other people may disagree, though. 
>  @sammccall , @hokein , WDYT? Should we always initialize primitive types in our code?

I think it would probably depend on whether we could find a sensible default value for a field (e.g. is 0 a better default value than UINT_MAX for line number?) and whether we expect users to always initialize a field (e.g. would it be better to pollute the field instead of providing a default value so that uninitialized values would be caught more easily).


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D43230





More information about the cfe-commits mailing list