Skip to content

Commit

Permalink
Fix frontend crash when deleting announcements (mastodon#13283)
Browse files Browse the repository at this point in the history
This two-line change fixes a crash in the front end that occurred
under the following circumstances:
 *  A server had more than one announcement,
 *  A user was displaying the announcements, and
 *  An announcement was deleted (or unpublished, which amounts to
    the same thing.)

As might be expected, the bug was caused by attempting to access a
notification using an index value outside the bounds of the existing
announcements.  Specifically, in two places.  First,
`_markAnnouncementAsRead` attempts to modify announcements based on
the current index.  This is what caused the front end crash.  Second,
when rendering the `Announcements` component, the code paginates the
announcements and displays the current one.  This did not cause a
crash, but caused the front end to confusingly display a blank
announcement (in situations that would have caused a crash) with no
way for the user to navigate back to previous announcements.

This commit fixes both issues by adding a check to ensure that the
code never attempts to access an announcement with an index greater
than or equal to the number of announcements present.
  • Loading branch information
codesections authored Mar 21, 2020
1 parent b5dace6 commit 8758221
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class Announcements extends ImmutablePureComponent {
_markAnnouncementAsRead () {
const { dismissAnnouncement, announcements } = this.props;
const { index } = this.state;
const announcement = announcements.get(index);
const announcement = announcements.get(index) || announcements.get(index - 1);
if (!announcement.get('read')) dismissAnnouncement(announcement.get('id'));
}

Expand All @@ -407,7 +407,7 @@ class Announcements extends ImmutablePureComponent {

render () {
const { announcements, intl } = this.props;
const { index } = this.state;
const index = this.state.index < announcements.size ? this.state.index : announcements.size - 1;

if (announcements.isEmpty()) {
return null;
Expand Down

0 comments on commit 8758221

Please sign in to comment.