False positive "Replier can only add posts at the end of a thread" from non-monotomic time()

Xon

Well-known member
Affected version
2.3.6
php's time() function is non-monotomic. This means it can technically go backwards, noticeable on machines which use a timesync service on a very busy forum

Consider the case;
  • UserA starts replying
    • finalSetup - Clock N, Post.post_date = N
  • Clock progress to N+1
  • Another users posts, updating the xf_threadrecord
    • xf_thread.last_post_date = N+1
  • Timsync causes the clock to be set to Clock N
  • UserA's request reloads the last post information for the xf_threadrecord
    • Post.post_date = N
    • xf_thread.last_post_date = N+1
  • Replying fails with an internal server error.

The relevant code:
Code:
php
    $time = time();
    if ($threadLatest['last_post_date'] > $post->post_date && $threadLatest['last_post_date'] <= $time)
    {
        $post->set('post_date', $time, ['forceSet' => true]);
    }

    $this->setPostPosition($threadLatest);
`
The problematic code is && $threadLatest['last_post_date'] <= $time


This should handle wonky time-syncs and preserve the post ordering:
PHP:
$time = max($threadLatest['last_post_date'], time());
if ($threadLatest['last_post_date'] > $post->post_date)
{
    $post->set('post_date', $time, ['forceSet' => true]);
}

Debugging this was tricky as setPostPosition, doesn't report the values and it assumed time() behaved itself :(
 
Back
Top Bottom