Skip to content

Commit 4db981b

Browse files
committed
Session: Refactor basedir to be a zend_string in mod_files
1 parent ec43006 commit 4db981b

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

ext/session/mod_files.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@
9090

9191
typedef struct {
9292
zend_string *last_key;
93-
char *basedir;
94-
size_t basedir_len;
93+
zend_string *basedir;
9594
size_t dirdepth;
9695
size_t st_size;
9796
int filemode;
@@ -111,13 +110,13 @@ static char *ps_files_path_create(char *buf, size_t buflen, ps_files *data, cons
111110
size_t n;
112111

113112
if (!data || ZSTR_LEN(key) <= data->dirdepth ||
114-
buflen < (strlen(data->basedir) + 2 * data->dirdepth + ZSTR_LEN(key) + 5 + sizeof(FILE_PREFIX))) {
113+
buflen < (ZSTR_LEN(data->basedir) + 2 * data->dirdepth + ZSTR_LEN(key) + 5 + sizeof(FILE_PREFIX))) {
115114
return NULL;
116115
}
117116

118117
p = ZSTR_VAL(key);
119-
memcpy(buf, data->basedir, data->basedir_len);
120-
n = data->basedir_len;
118+
memcpy(buf, ZSTR_VAL(data->basedir), ZSTR_LEN(data->basedir));
119+
n = ZSTR_LEN(data->basedir);
121120
buf[n++] = PHP_DIR_SEPARATOR;
122121
for (i = 0; i < (int)data->dirdepth; i++) {
123122
buf[n++] = *p++;
@@ -277,48 +276,45 @@ static zend_result ps_files_write(ps_files *data, zend_string *key, zend_string
277276
return SUCCESS;
278277
}
279278

280-
static int ps_files_cleanup_dir(const char *dirname, zend_long maxlifetime)
279+
static int ps_files_cleanup_dir(const zend_string *dirname, zend_long maxlifetime)
281280
{
282281
DIR *dir;
283282
struct dirent *entry;
284283
zend_stat_t sbuf = {0};
285284
char buf[MAXPATHLEN];
286285
time_t now;
287286
int nrdels = 0;
288-
size_t dirname_len;
289287

290-
dir = opendir(dirname);
288+
dir = opendir(ZSTR_VAL(dirname));
291289
if (!dir) {
292-
php_error_docref(NULL, E_NOTICE, "ps_files_cleanup_dir: opendir(%s) failed: %s (%d)", dirname, strerror(errno), errno);
290+
php_error_docref(NULL, E_NOTICE, "ps_files_cleanup_dir: opendir(%s) failed: %s (%d)", ZSTR_VAL(dirname), strerror(errno), errno);
293291
return (0);
294292
}
295293

296294
time(&now);
297295

298-
dirname_len = strlen(dirname);
299-
300-
if (dirname_len >= MAXPATHLEN) {
301-
php_error_docref(NULL, E_NOTICE, "ps_files_cleanup_dir: dirname(%s) is too long", dirname);
296+
if (ZSTR_LEN(dirname) >= MAXPATHLEN) {
297+
php_error_docref(NULL, E_NOTICE, "ps_files_cleanup_dir: dirname(%s) is too long", ZSTR_VAL(dirname));
302298
closedir(dir);
303299
return (0);
304300
}
305301

306302
/* Prepare buffer (dirname never changes) */
307-
memcpy(buf, dirname, dirname_len);
308-
buf[dirname_len] = PHP_DIR_SEPARATOR;
303+
memcpy(buf, ZSTR_VAL(dirname), ZSTR_LEN(dirname));
304+
buf[ZSTR_LEN(dirname)] = PHP_DIR_SEPARATOR;
309305

310306
while ((entry = readdir(dir))) {
311307
/* does the file start with our prefix? */
312308
if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1)) {
313309
size_t entry_len = strlen(entry->d_name);
314310

315311
/* does it fit into our buffer? */
316-
if (entry_len + dirname_len + 2 < MAXPATHLEN) {
312+
if (entry_len + ZSTR_LEN(dirname) + 2 < MAXPATHLEN) {
317313
/* create the full path.. */
318-
memcpy(buf + dirname_len + 1, entry->d_name, entry_len);
314+
memcpy(buf + ZSTR_LEN(dirname) + 1, entry->d_name, entry_len);
319315

320316
/* NUL terminate it and */
321-
buf[dirname_len + entry_len + 1] = '\0';
317+
buf[ZSTR_LEN(dirname) + entry_len + 1] = '\0';
322318

323319
/* check whether its last access was more than maxlifetime ago */
324320
if (VCWD_STAT(buf, &sbuf) == 0 &&
@@ -417,8 +413,7 @@ PS_OPEN_FUNC(files)
417413
data->fd = -1;
418414
data->dirdepth = dirdepth;
419415
data->filemode = filemode;
420-
data->basedir_len = strlen(save_path);
421-
data->basedir = estrndup(save_path, data->basedir_len);
416+
data->basedir = zend_string_init(save_path, strlen(save_path), /* persistent */ false);
422417

423418
if (PS_GET_MOD_DATA()) {
424419
ps_close_files(mod_data);
@@ -450,7 +445,7 @@ PS_CLOSE_FUNC(files)
450445
data->last_key = NULL;
451446
}
452447

453-
efree(data->basedir);
448+
zend_string_release_ex(data->basedir, /* persistent */ false);
454449
efree(data);
455450
PS_SET_MOD_DATA(NULL);
456451

0 commit comments

Comments
 (0)