Skip to content

Commit ac22ec9

Browse files
committed
Fixes on GetActiveSession, GetSessionUser and GetSessionDisplay to avoid errors and panic
1 parent d53122b commit ac22ec9

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

login1/dbus.go

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,42 +156,51 @@ func userFromInterfaces(user []interface{}) (*User, error) {
156156
return &ret, nil
157157
}
158158

159-
//GetActiveSession may be used to get the session object path for the current active session
159+
// GetActiveSession may be used to get the session object path for the current active session
160160
func (c *Conn) GetActiveSession() (dbus.ObjectPath, error) {
161161
var activeSessionPath dbus.ObjectPath
162162
var seat0Path dbus.ObjectPath
163-
err := c.object.Call(dbusInterface+".GetSeat", 0, "seat0").Store(&seat0Path)
164-
165-
if err != nil {
166-
return activeSessionPath, err
163+
if err := c.object.Call(dbusInterface+".GetSeat", 0, "seat0").Store(&seat0Path); err != nil {
164+
return "", err
167165
}
168166

169167
seat0Obj := c.conn.Object(dbusDest, seat0Path)
170168
activeSession, err := seat0Obj.GetProperty(dbusDest + ".Seat.ActiveSession")
171169
if err != nil {
172-
return activeSessionPath, err
170+
return "", err
171+
}
172+
activeSessionMap, ok := activeSession.Value().([]interface{})
173+
if !ok || len(activeSessionMap) < 2 {
174+
return "", fmt.Errorf("failed to typecast active session map")
173175
}
174176

175-
activeSessionPath = activeSession.Value().([]interface{})[1].(dbus.ObjectPath)
177+
activeSessionPath, ok = activeSessionMap[1].(dbus.ObjectPath)
178+
if !ok {
179+
return "", fmt.Errorf("failed to typecast dbus active session Path")
180+
}
176181
return activeSessionPath, nil
177182
}
178183

179-
//GetSessionUser may be used to get the user of specific session
184+
// GetSessionUser may be used to get the user of specific session
180185
func (c *Conn) GetSessionUser(sessionPath dbus.ObjectPath) (*User, error) {
186+
if len(sessionPath) == 0 {
187+
return nil, fmt.Errorf("Empty sessionPath")
188+
}
189+
181190
var user User
182191
activeSessionObj := c.conn.Object(dbusDest, sessionPath)
183192
sessionUserName, err := activeSessionObj.GetProperty(dbusDest + ".Session.Name")
184193
if err != nil {
185-
return &user, err
194+
return nil, err
186195
}
187196

188197
sessionUser, err := activeSessionObj.GetProperty(dbusDest + ".Session.User")
189198
if err != nil {
190-
return &user, err
199+
return nil, err
191200
}
192201
dbusUser, ok := sessionUser.Value().([]interface{})
193202
if !ok {
194-
return &user, fmt.Errorf("failed to typecast dbus session user")
203+
return nil, fmt.Errorf("failed to typecast dbus session user")
195204
}
196205

197206
if len(dbusUser) < 2 {
@@ -208,18 +217,21 @@ func (c *Conn) GetSessionUser(sessionPath dbus.ObjectPath) (*User, error) {
208217

209218
user = User{UID: uid, Name: sessionUserName.String(), Path: path}
210219

211-
return &user, err
220+
return &user, nil
212221
}
213222

214-
//GetSessionDisplay may be used to get the display for specific session
223+
// GetSessionDisplay may be used to get the display for specific session
215224
func (c *Conn) GetSessionDisplay(sessionPath dbus.ObjectPath) (string, error) {
225+
if len(sessionPath) == 0 {
226+
return "", fmt.Errorf("Empty sessionPath")
227+
}
216228
sessionObj := c.conn.Object(dbusDest, sessionPath)
217229
display, err := sessionObj.GetProperty(dbusDest + ".Session.Display")
218230
if err != nil {
219231
return "", err
220232
}
221233

222-
return display.String(), err
234+
return display.String(), nil
223235
}
224236

225237
// GetSession may be used to get the session object path for the session with the specified ID.

0 commit comments

Comments
 (0)