Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Sources/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SmfSessionHandler extends SessionHandler implements SessionHandlerInterfac
*
* Holds our session data, used for multiple reads/validations to reduce database impact.
*/
private /*PHP 8.0 ?string*/ $data = null;
private /*PHP 7.4 ?string*/ $data = null;

/**
* Implementation of SessionHandler::open() replacing the standard open handler.
Expand Down Expand Up @@ -158,7 +158,8 @@ public function read(/*PHP 8.0 string*/$id)/*PHP 8.0: string|false*/
if (!$this->isValidSessionID($id))
return '';

if ($this->data === null) {
if ($this->data === null)
{
// Look for it in the database.
$result = $smcFunc['db_query']('', '
SELECT data
Expand All @@ -169,10 +170,14 @@ public function read(/*PHP 8.0 string*/$id)/*PHP 8.0: string|false*/
'session_id' => $id,
)
);

list ($sess_data) = $smcFunc['db_fetch_row']($result);
$smcFunc['db_free_result']($result);

$this->data = $sess_data;
if ($sess_data !== null)
{
$this->data = $sess_data;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $sess_data failed to set because of a returned result, we should ensure that $this->data is not retaining a bad result.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice.

  1. This is what @Oldiesmann had to fix in Make sure we actually have session data before trying to set it #9858
  2. It passes my unit test https://github.com/live627/SMF2.1/blob/unittest/tests/SmfSessionHandlerTest.php
  3. This has the potential to become another highly visible bug.

}

return $this->data != null ? $this->data : '';
Expand Down Expand Up @@ -301,7 +306,7 @@ public function validateId(/*PHP 8.0 string */$id): bool
$this->read($id);
}

return $this->data !== '';
return $this->data !== null;
}

/**
Expand All @@ -324,7 +329,7 @@ public function updateTimestamp(/*PHP 8.0 string */$id, /*PHP 8.0 string */$data
}

// Just call the write.
return $this->write($id, data);
return $this->write($id, $data);
}
}

Expand Down
Loading