Skip to content

Commit 047ef56

Browse files
Use C++ scoping for handling buffer deletion
1 parent 6c23c7e commit 047ef56

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

src/helper/UserSession.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,19 @@ namespace SDDM {
135135
long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
136136
if (bufsize == -1)
137137
bufsize = 16384;
138-
char *buffer = (char *)malloc(bufsize);
139-
if (buffer == NULL)
138+
QScopedPointer<char, QScopedPointerPodDeleter> buffer(static_cast<char*>(malloc(bufsize)));
139+
if (buffer.isNull())
140140
exit(Auth::HELPER_OTHER_ERROR);
141-
int err = getpwnam_r(username.constData(), &pw, buffer, bufsize, &rpw);
141+
int err = getpwnam_r(username.constData(), &pw, buffer.data(), bufsize, &rpw);
142142
if (rpw == NULL) {
143143
if (err == 0)
144144
qCritical() << "getpwnam_r(" << username << ") username not found!";
145145
else
146146
qCritical() << "getpwnam_r(" << username << ") failed with error: " << strerror(err);
147-
free(buffer);
148147
exit(Auth::HELPER_OTHER_ERROR);
149148
}
150149
if (setgid(pw.pw_gid) != 0) {
151150
qCritical() << "setgid(" << pw.pw_gid << ") failed for user: " << username;
152-
free(buffer);
153151
exit(Auth::HELPER_OTHER_ERROR);
154152
}
155153

@@ -181,7 +179,6 @@ namespace SDDM {
181179
&n_user_groups)) == -1 ) {
182180
qCritical() << "getgrouplist(" << username << ", " << pw.pw_gid
183181
<< ") failed";
184-
free(buffer);
185182
exit(Auth::HELPER_OTHER_ERROR);
186183
}
187184
}
@@ -198,7 +195,6 @@ namespace SDDM {
198195
// setgroups(2) handles duplicate groups
199196
if (setgroups(n_groups, groups) != 0) {
200197
qCritical() << "setgroups() failed for user: " << username;
201-
free(buffer);
202198
exit (Auth::HELPER_OTHER_ERROR);
203199
}
204200
delete[] groups;
@@ -210,31 +206,28 @@ namespace SDDM {
210206

211207
if (initgroups(pw.pw_name, pw.pw_gid) != 0) {
212208
qCritical() << "initgroups(" << pw.pw_name << ", " << pw.pw_gid << ") failed for user: " << username;
213-
free(buffer);
214209
exit(Auth::HELPER_OTHER_ERROR);
215210
}
216211

217212
#endif /* USE_PAM */
218213

219214
if (setuid(pw.pw_uid) != 0) {
220215
qCritical() << "setuid(" << pw.pw_uid << ") failed for user: " << username;
221-
free(buffer);
222216
exit(Auth::HELPER_OTHER_ERROR);
223217
}
224218
if (chdir(pw.pw_dir) != 0) {
225219
qCritical() << "chdir(" << pw.pw_dir << ") failed for user: " << username;
226220
qCritical() << "verify directory exist and has sufficient permissions";
227-
free(buffer);
228221
exit(Auth::HELPER_OTHER_ERROR);
229222
}
230-
free(buffer);
223+
const QString homeDir = QString::fromLocal8Bit(pw.pw_dir);
231224

232225
//we cannot use setStandardError file as this code is run in the child process
233226
//we want to redirect after we setuid so that the log file is owned by the user
234227

235228
// determine stderr log file based on session type
236229
QString sessionLog = QStringLiteral("%1/%2")
237-
.arg(QString::fromLocal8Bit(pw.pw_dir))
230+
.arg(homeDir)
238231
.arg(sessionType == QLatin1String("x11")
239232
? mainConfig.X11.SessionLogFile.get()
240233
: mainConfig.Wayland.SessionLogFile.get());

0 commit comments

Comments
 (0)