From ca93122050607f51afd4d571680bd2b4d0fdab10 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+github@gmail.com>
Date: Fri, 15 Jul 2022 11:46:38 +0200
Subject: [PATCH] fix bad assignments on previously undeclared arrays in
 StudipController, re #1328

---
 app/controllers/calendar/single.php                    | 3 +++
 app/controllers/studip_controller_properties_trait.php | 7 +++----
 tests/unit/lib/classes/StudipControllerTest.php        | 7 +++++++
 3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/app/controllers/calendar/single.php b/app/controllers/calendar/single.php
index 6cfc133dc27..91525aabe25 100644
--- a/app/controllers/calendar/single.php
+++ b/app/controllers/calendar/single.php
@@ -98,6 +98,7 @@ class Calendar_SingleController extends Calendar_CalendarController
         $timestamp = mktime(12, 0, 0, date('n', $this->atime), date('j', $this->atime), date('Y', $this->atime));
         $monday = $timestamp - 86400 * (strftime('%u', $timestamp) - 1);
         $day_count = $this->settings['type_week'] == 'SHORT' ? 5 : 7;
+
         $this->calendars = [];
         for ($i = 0; $i < $day_count; $i++) {
             $this->calendars[$i] =
@@ -126,6 +127,8 @@ class Calendar_SingleController extends Calendar_CalendarController
         $cor = date('n', $this->atime) == 3 ? 1 : 0;
         $this->first_day = $month_start - $adow * 86400;
         $this->last_day = ((42 - ($adow + date('t', $this->atime))) % 7 + $cor) * 86400 + $month_end;
+
+        $this->calendars = [];
         for ($start_day = $this->first_day; $start_day <= $this->last_day; $start_day += 86400) {
             $this->calendars[] = SingleCalendar::getDayCalendar($this->range_id,
                     $start_day, null, $this->restrictions);
diff --git a/app/controllers/studip_controller_properties_trait.php b/app/controllers/studip_controller_properties_trait.php
index fb3b68d57d6..4e906fae0ef 100644
--- a/app/controllers/studip_controller_properties_trait.php
+++ b/app/controllers/studip_controller_properties_trait.php
@@ -44,11 +44,10 @@ trait StudipControllerPropertiesTrait
      */
     public function &__get(string $offset)
     {
-        $result = null;
-        if (isset($this->_template_variables[$offset])) {
-            $result = &$this->_template_variables[$offset];
+        if (!isset($this->_template_variables[$offset])) {
+            $this->_template_variables[$offset] = null;
         }
-        return $result;
+        return $this->_template_variables[$offset];
     }
 
     /**
diff --git a/tests/unit/lib/classes/StudipControllerTest.php b/tests/unit/lib/classes/StudipControllerTest.php
index 0eea6710be7..f74eae4140f 100644
--- a/tests/unit/lib/classes/StudipControllerTest.php
+++ b/tests/unit/lib/classes/StudipControllerTest.php
@@ -200,6 +200,9 @@ final class StudipControllerTest extends Codeception\Test\Unit
         $controller->baz[] = 23;
         $this->assertEquals([23], $controller->baz);
 
+        $controller->bad[] = 23;
+        $this->assertEquals([23], $controller->bad);
+
         // Test fetching all variables
         $variables = $controller->get_assigned_variables();
 
@@ -219,6 +222,10 @@ final class StudipControllerTest extends Codeception\Test\Unit
         $this->assertArrayHasKey('baz', $variables);
         $this->assertCount(1, $variables['baz']);
         $this->assertEquals([23], $variables['baz']);
+
+        $this->assertArrayHasKey('bad', $variables);
+        $this->assertCount(1, $variables['bad']);
+        $this->assertEquals([23], $variables['bad']);
     }
 
     /**
-- 
GitLab