Skip to content
Merged
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
67 changes: 67 additions & 0 deletions challenge-3/submissions/Kosench/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import "fmt"

type Employee struct {
ID int
Name string
Age int
Salary float64
}

type Manager struct {
Employees []Employee
}

// AddEmployee adds a new employee to the manager's list.
func (m *Manager) AddEmployee(e Employee) {
m.Employees = append(m.Employees, e)
}

// RemoveEmployee removes an employee by ID from the manager's list.
func (m *Manager) RemoveEmployee(id int) {
for i := range m.Employees {
if m.Employees[i].ID == id {
m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
return
}
}
}
Comment on lines +21 to +29
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Pointer invalidation risk when combining FindEmployeeByID and RemoveEmployee.

FindEmployeeByID returns a pointer to an element in the Employees slice. When RemoveEmployee modifies the slice by reslicing, any previously returned pointers can become invalid or point to different employees. This creates a critical correctness issue.

Example scenario:

emp := manager.FindEmployeeByID(1)  // returns pointer to slice element
manager.RemoveEmployee(1)            // reslices, shifting elements
// emp now may point to wrong employee or invalid memory

Apply this diff to return a copy instead of a pointer:

 // FindEmployeeByID finds and returns an employee by their ID.
-func (m *Manager) FindEmployeeByID(id int) *Employee {
+func (m *Manager) FindEmployeeByID(id int) (Employee, bool) {
 	for i := range m.Employees {
 		if m.Employees[i].ID == id {
-			return &m.Employees[i]
+			return m.Employees[i], true
 		}
 	}
-	return nil
+	return Employee{}, false
 }

And update the call site in main:

-	employee := manager.FindEmployeeByID(2)
+	employee, found := manager.FindEmployeeByID(2)
 
 	fmt.Printf("Average Salary: %f\n", averageSalary)
-	if employee != nil {
-		fmt.Printf("Employee found: %+v\n", *employee)
+	if found {
+		fmt.Printf("Employee found: %+v\n", employee)
 	}

Also applies to: 45-53

🤖 Prompt for AI Agents
In challenge-3/submissions/Kosench/solution-template.go around lines 21-29 (and
also address similar code at lines 45-53), FindEmployeeByID currently returns a
pointer into the Employees slice which can be invalidated by RemoveEmployee's
reslicing; change FindEmployeeByID to return an Employee value and a boolean
(e.g., func (m *Manager) FindEmployeeByID(id int) (Employee, bool)) so callers
receive a copy, update all call sites (including main) to handle the (Employee,
bool) return (check the bool before using the returned value), and keep
RemoveEmployee as-is; this prevents pointer invalidation by ensuring callers
work with copies rather than pointers into the slice.


// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
if len(m.Employees) == 0 {
return 0
}

var sum float64
for _, employee := range m.Employees {
sum += employee.Salary
}

return sum / float64(len(m.Employees))
}

// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for i := range m.Employees {
if m.Employees[i].ID == id {
return &m.Employees[i]
}
}
return nil
}

func main() {
manager := Manager{}
manager.AddEmployee(Employee{ID: 1, Name: "Alice", Age: 30, Salary: 70000})
manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000})
manager.RemoveEmployee(1)
averageSalary := manager.GetAverageSalary()
employee := manager.FindEmployeeByID(2)

fmt.Printf("Average Salary: %f\n", averageSalary)
if employee != nil {
fmt.Printf("Employee found: %+v\n", *employee)
}
}
Loading