Bug 187260 - WebCore: Fix clang static analyzer warnings: Assigned value is garbage or undefined
Summary: WebCore: Fix clang static analyzer warnings: Assigned value is garbage or und...
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: David Kilzer (:ddkilzer)
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-07-02 12:30 PDT by David Kilzer (:ddkilzer)
Modified: 2018-07-09 08:50 PDT (History)
9 users (show)

See Also:


Attachments
Patch v1 (12.56 KB, patch)
2018-07-02 12:34 PDT, David Kilzer (:ddkilzer)
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Kilzer (:ddkilzer) 2018-07-02 12:30:02 PDT
Fix "Assigned value is garbage or undefined" warnings in WebCore found by clang static analyzer deep analysis.
Comment 1 Radar WebKit Bug Importer 2018-07-02 12:30:23 PDT
<rdar://problem/41735280>
Comment 2 David Kilzer (:ddkilzer) 2018-07-02 12:34:46 PDT
Created attachment 344127 [details]
Patch v1
Comment 3 EWS Watchlist 2018-07-02 12:36:55 PDT
Attachment 344127 [details] did not pass style-queue:


ERROR: Source/WebCore/crypto/CryptoAlgorithmIdentifier.h:33:  enum members should use InterCaps with an initial capital letter or initial 'k' for C-style enums.  [readability/enum_casing] [4]
Total errors found: 1 in 12 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 4 youenn fablet 2018-07-02 18:44:10 PDT
Comment on attachment 344127 [details]
Patch v1

View in context: https://bugs.webkit.org/attachment.cgi?id=344127&action=review

> Source/WebCore/Modules/mediastream/RTCStatsReport.h:57
>          Type type;

Should type have a default value as well?
Comment 5 David Kilzer (:ddkilzer) 2018-07-03 10:39:46 PDT
Comment on attachment 344127 [details]
Patch v1

View in context: https://bugs.webkit.org/attachment.cgi?id=344127&action=review

>> Source/WebCore/Modules/mediastream/RTCStatsReport.h:57
>>          Type type;
> 
> Should type have a default value as well?

Yes, but there is no good "Invalid" option to use here as a default value.

I'm torn between using Type::Codec (which would have a value of zero), adding a new "Invalid" enum, or doing nothing and letting the static analyzer warn when a subclass (substruct?) fails to set it.

Thoughts?
Comment 6 youenn fablet 2018-07-03 11:02:15 PDT
Comment on attachment 344127 [details]
Patch v1

LGTM, modulo the comment on VRDisplayEvent below.

(In reply to David Kilzer (:ddkilzer) from comment #5)
> Comment on attachment 344127 [details]
> Patch v1
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=344127&action=review
> 
> >> Source/WebCore/Modules/mediastream/RTCStatsReport.h:57
> >>          Type type;
> > 
> > Should type have a default value as well?
> 
> Yes, but there is no good "Invalid" option to use here as a default value.

And we cannot add one since it is bound to IDL.

> I'm torn between using Type::Codec (which would have a value of zero),
> adding a new "Invalid" enum, or doing nothing and letting the static
> analyzer warn when a subclass (substruct?) fails to set it.
> 
> Thoughts?

Ideally, the analyzer warning should be an error for that specific case.
If we cannot make it so, status quo seems fine to me.

View in context: https://bugs.webkit.org/attachment.cgi?id=344127&action=review

> Source/WebCore/Modules/webvr/VRDisplayEvent.h:39
> +        VRDisplayEventReason reason { VRDisplayEventReason::Mounted };

Looking at this one, reason is not required, so it should either be std::optional<VRDisplayEventReason> or something like VRDisplayEventReason::None.
Comment 7 David Kilzer (:ddkilzer) 2018-07-03 13:21:02 PDT
Comment on attachment 344127 [details]
Patch v1

View in context: https://bugs.webkit.org/attachment.cgi?id=344127&action=review

>>>> Source/WebCore/Modules/mediastream/RTCStatsReport.h:57
>>>>          Type type;
>>> 
>>> Should type have a default value as well?
>> 
>> Yes, but there is no good "Invalid" option to use here as a default value.
>> 
>> I'm torn between using Type::Codec (which would have a value of zero), adding a new "Invalid" enum, or doing nothing and letting the static analyzer warn when a subclass (substruct?) fails to set it.
>> 
>> Thoughts?
> 
> And we cannot add one since it is bound to IDL.

I reran the clang static analyzer in deep mode, and I found that in JSRTCStatsReport.cpp, the various convertDictionary() methods can leave Stats.type and (below) IceCandidatePairStats.state undefined.

What are reasonable default values in each of these cases?  The ones that equate to the value zero?
Comment 8 youenn fablet 2018-07-03 13:33:22 PDT
> > And we cannot add one since it is bound to IDL.
> 
> I reran the clang static analyzer in deep mode, and I found that in
> JSRTCStatsReport.cpp, the various convertDictionary() methods can leave
> Stats.type and (below) IceCandidatePairStats.state undefined.
> 
> What are reasonable default values in each of these cases?  The ones that
> equate to the value zero?

Would std::optional<Type> type do the trick?
Comment 9 David Kilzer (:ddkilzer) 2018-07-03 14:57:37 PDT
(In reply to youenn fablet from comment #8)
> > > And we cannot add one since it is bound to IDL.
> > 
> > I reran the clang static analyzer in deep mode, and I found that in
> > JSRTCStatsReport.cpp, the various convertDictionary() methods can leave
> > Stats.type and (below) IceCandidatePairStats.state undefined.
> > 
> > What are reasonable default values in each of these cases?  The ones that
> > equate to the value zero?
> 
> Would std::optional<Type> type do the trick?

Yes.  We'd have to update the IDL for that change, or just change the instance variables?  (Sorry, I'm not up-to-date on the latest IDL support.)
Comment 10 youenn fablet 2018-07-03 15:01:42 PDT
As long as the dictionary member is not required in the IDL, I would think we can use optional directly.
Comment 11 David Kilzer (:ddkilzer) 2018-07-03 16:46:00 PDT
Comment on attachment 344127 [details]
Patch v1

Clearing r? flag until I can post a new patch.
Comment 12 Sergio Villar Senin 2018-07-04 01:29:15 PDT
Comment on attachment 344127 [details]
Patch v1

View in context: https://bugs.webkit.org/attachment.cgi?id=344127&action=review

>> Source/WebCore/Modules/webvr/VRDisplayEvent.h:39
>> +        VRDisplayEventReason reason { VRDisplayEventReason::Mounted };
> 
> Looking at this one, reason is not required, so it should either be std::optional<VRDisplayEventReason> or something like VRDisplayEventReason::None.

Youenn is right the reason is optional. However I'd advice not to change this because I'm going to modify it in a patch that is about to be uploaded for review.
Comment 13 David Kilzer (:ddkilzer) 2018-07-09 08:50:20 PDT
(In reply to Sergio Villar Senin from comment #12)
> Comment on attachment 344127 [details]
> Patch v1
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=344127&action=review
> 
> >> Source/WebCore/Modules/webvr/VRDisplayEvent.h:39
> >> +        VRDisplayEventReason reason { VRDisplayEventReason::Mounted };
> > 
> > Looking at this one, reason is not required, so it should either be std::optional<VRDisplayEventReason> or something like VRDisplayEventReason::None.
> 
> Youenn is right the reason is optional. However I'd advice not to change
> this because I'm going to modify it in a patch that is about to be uploaded
> for review.

Okay.  Assuming this is Bug 187343.