Bug 209718 - Fix "Dead nested assignment" static analyzer warning in monthFromDayInYear()
Summary: Fix "Dead nested assignment" static analyzer warning in monthFromDayInYear()
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: David Kilzer (:ddkilzer)
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-03-29 09:04 PDT by David Kilzer (:ddkilzer)
Modified: 2020-03-30 09:59 PDT (History)
7 users (show)

See Also:


Attachments
Patch v1 (1.83 KB, patch)
2020-03-29 09:08 PDT, David Kilzer (:ddkilzer)
no flags Details | Formatted Diff | Diff
Patch v2 (1.52 KB, patch)
2020-03-29 12:40 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) 2020-03-29 09:04:27 PDT
Add debug assertions to monthFromDayInYear() in DateMath.h to validate the `dayInYear` parameter.

This has the added benefit of silencing many "Dead nested assignment" issues when running the clang static analyzer.
Comment 1 David Kilzer (:ddkilzer) 2020-03-29 09:08:09 PDT
Created attachment 394867 [details]
Patch v1
Comment 2 Darin Adler 2020-03-29 09:46:49 PDT
Comment on attachment 394867 [details]
Patch v1

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

Not sure why the static analyzer trusts our assertions

> Source/WTF/wtf/DateMath.h:351
> +    ASSERT(d < (step + 31));

No need for the parentheses here
Comment 3 David Kilzer (:ddkilzer) 2020-03-29 10:35:05 PDT
(In reply to Darin Adler from comment #2)
> Comment on attachment 394867 [details]
> Patch v1
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=394867&action=review
> 
> Not sure why the static analyzer trusts our assertions

The static analyzer runs on Debug builds, so by adding this assertion, the value of `step` is read and the analyzer won't issue a warning that `step` is written to but not read from here:

   if (d < (step += 30))

> > Source/WTF/wtf/DateMath.h:351
> > +    ASSERT(d < (step + 31));
> 
> No need for the parentheses here

Will fix when landing.
Comment 4 David Kilzer (:ddkilzer) 2020-03-29 10:37:39 PDT
Hmm...well, it looks like we have tests that end up passing in invalid values:

Less than zero:

js/date-constructor.html
js/date-parse-comments-test.html
js/date-parse-test.html

More than 364/365:

js/date-timeClip-large-values.html
Comment 5 David Kilzer (:ddkilzer) 2020-03-29 10:55:23 PDT
Comment on attachment 394867 [details]
Patch v1

I think using UNUSED_VARIABLE() or a similar macro would be better here.

Should I make a new macro that has a more explicit name like IGNORE_UNUSED_VARIABLE_WRITE()?
Comment 6 Darin Adler 2020-03-29 11:11:09 PDT
I don’t think UNUSED is how we should fix this.

I suggest we change that last one to just:

    d < step + 30

without the +=.

Or ... rewrite to whole thing to not use a local variable at all. Either to use an array to do this rather than writing it out unrolled, or find some other form to write it in that doesn’t involve this "too clever" += mixed in with if statements. After all, these are constants.

One neat way to write it would be to write a constexpr function to compute the start day of a month and use that function, passing a month integer instead of having the function compute these values live. Then the looping will be done at compile time, not runtime.

There are probably all sorts of other fun ways to write this.
Comment 7 David Kilzer (:ddkilzer) 2020-03-29 12:40:19 PDT
Created attachment 394875 [details]
Patch v2
Comment 8 David Kilzer (:ddkilzer) 2020-03-29 13:21:55 PDT
(In reply to Darin Adler from comment #6)
> I don’t think UNUSED is how we should fix this.
> 
> I suggest we change that last one to just:
> 
>     d < step + 30
> 
> without the +=.

I went with this option for now.  Thanks!
Comment 9 EWS 2020-03-30 09:58:33 PDT
Committed r259202: <https://trac.webkit.org/changeset/259202>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 394875 [details].
Comment 10 Radar WebKit Bug Importer 2020-03-30 09:59:17 PDT
<rdar://problem/61060041>