Skip to content

Commit 8a1ca99

Browse files
committed
Session: Use zend_string* consistently for key in mod_mm
1 parent f872972 commit 8a1ca99

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

ext/session/mod_mm.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct ps_sd {
4545
void *data;
4646
size_t datalen; /* amount of valid data */
4747
size_t alloclen; /* amount of allocated memory for data */
48-
char key[1]; /* inline key */
48+
zend_string *key;
4949
} ps_sd;
5050

5151
typedef struct {
@@ -64,14 +64,15 @@ static ps_mm *ps_mm_instance = NULL;
6464
# define ps_mm_debug(a)
6565
#endif
6666

67-
static inline uint32_t ps_sd_hash(const char *data, size_t len)
67+
static inline uint32_t ps_sd_hash(const zend_string *data)
6868
{
6969
uint32_t h;
70-
const char *e = data + len;
70+
const char *data_char = ZSTR_VAL(data);
71+
const char *e = ZSTR_VAL(data) + ZSTR_LEN(data);
7172

72-
for (h = 2166136261U; data < e; ) {
73+
for (h = 2166136261U; data_char < e; ) {
7374
h *= 16777619;
74-
h ^= *data++;
75+
h ^= *data_char++;
7576
}
7677

7778
return h;
@@ -106,30 +107,27 @@ static void hash_split(ps_mm *data)
106107
data->hash_max = nmax;
107108
}
108109

109-
static ps_sd *ps_sd_new(ps_mm *data, const char *key)
110+
static ps_sd *ps_sd_new(ps_mm *data, zend_string *key)
110111
{
111112
uint32_t hv, slot;
112113
ps_sd *sd;
113-
size_t keylen;
114114

115-
keylen = strlen(key);
116-
117-
sd = mm_malloc(data->mm, sizeof(ps_sd) + keylen);
115+
sd = mm_malloc(data->mm, sizeof(ps_sd) + ZSTR_LEN(key));
118116
if (!sd) {
119117

120118
php_error_docref(NULL, E_WARNING, "mm_malloc failed, avail %ld, err %s", mm_available(data->mm), mm_error());
121119
return NULL;
122120
}
123121

124-
hv = ps_sd_hash(key, keylen);
122+
hv = ps_sd_hash(key);
125123
slot = hv & data->hash_max;
126124

127125
sd->ctime = 0;
128126
sd->hv = hv;
129127
sd->data = NULL;
130128
sd->alloclen = sd->datalen = 0;
131129

132-
memcpy(sd->key, key, keylen + 1);
130+
sd->key = zend_string_copy(key);
133131

134132
sd->next = data->hash[slot];
135133
data->hash[slot] = sd;
@@ -142,7 +140,7 @@ static ps_sd *ps_sd_new(ps_mm *data, const char *key)
142140
}
143141
}
144142

145-
ps_mm_debug(("inserting %s(%p) into slot %d\n", key, sd, slot));
143+
ps_mm_debug(("inserting %s(%p) into slot %d\n", ZSTR_VAL(key), sd, slot));
146144

147145
return sd;
148146
}
@@ -151,7 +149,7 @@ static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
151149
{
152150
uint32_t slot;
153151

154-
slot = ps_sd_hash(sd->key, strlen(sd->key)) & data->hash_max;
152+
slot = ps_sd_hash(sd->key) & data->hash_max;
155153

156154
if (data->hash[slot] == sd) {
157155
data->hash[slot] = sd->next;
@@ -168,20 +166,21 @@ static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
168166
if (sd->data) {
169167
mm_free(data->mm, sd->data);
170168
}
169+
zend_string_release(sd->key);
171170

172171
mm_free(data->mm, sd);
173172
}
174173

175-
static ps_sd *ps_sd_lookup(ps_mm *data, const char *key, bool rw)
174+
static ps_sd *ps_sd_lookup(ps_mm *data, const zend_string *key, bool rw)
176175
{
177176
uint32_t hv, slot;
178177
ps_sd *ret, *prev;
179178

180-
hv = ps_sd_hash(key, strlen(key));
179+
hv = ps_sd_hash(key);
181180
slot = hv & data->hash_max;
182181

183182
for (prev = NULL, ret = data->hash[slot]; ret; prev = ret, ret = ret->next) {
184-
if (ret->hv == hv && !strcmp(ret->key, key)) {
183+
if (ret->hv == hv && zend_string_equals(ret->key, key)) {
185184
break;
186185
}
187186
}
@@ -196,12 +195,12 @@ static ps_sd *ps_sd_lookup(ps_mm *data, const char *key, bool rw)
196195
data->hash[slot] = ret;
197196
}
198197

199-
ps_mm_debug(("lookup(%s): ret=%p,hv=%u,slot=%d\n", key, ret, hv, slot));
198+
ps_mm_debug(("lookup(%s): ret=%p,hv=%u,slot=%d\n", ZSTR_VAL(key), ret, hv, slot));
200199

201200
return ret;
202201
}
203202

204-
static zend_result ps_mm_key_exists(ps_mm *data, const char *key)
203+
static zend_result ps_mm_key_exists(ps_mm *data, const zend_string *key)
205204
{
206205
ps_sd *sd;
207206

@@ -349,7 +348,7 @@ PS_READ_FUNC(mm)
349348

350349
/* If there is an ID and strict mode, verify existence */
351350
if (PS(use_strict_mode)
352-
&& ps_mm_key_exists(data, key->val) == FAILURE) {
351+
&& ps_mm_key_exists(data, key) == FAILURE) {
353352
/* key points to PS(id), but cannot change here. */
354353
if (key) {
355354
efree(PS(id));
@@ -366,7 +365,7 @@ PS_READ_FUNC(mm)
366365
PS(session_status) = php_session_active;
367366
}
368367

369-
sd = ps_sd_lookup(data, PS(id)->val, 0);
368+
sd = ps_sd_lookup(data, PS(id), 0);
370369
if (sd) {
371370
*val = zend_string_init(sd->data, sd->datalen, 0);
372371
ret = SUCCESS;
@@ -384,10 +383,10 @@ PS_WRITE_FUNC(mm)
384383

385384
mm_lock(data->mm, MM_LOCK_RW);
386385

387-
sd = ps_sd_lookup(data, key->val, 1);
386+
sd = ps_sd_lookup(data, key, 1);
388387
if (!sd) {
389-
sd = ps_sd_new(data, key->val);
390-
ps_mm_debug(("new entry for %s\n", key->val));
388+
sd = ps_sd_new(data, key);
389+
ps_mm_debug(("new entry for %s\n", ZSTR_VAL(key)));
391390
}
392391

393392
if (sd) {
@@ -423,7 +422,7 @@ PS_DESTROY_FUNC(mm)
423422

424423
mm_lock(data->mm, MM_LOCK_RW);
425424

426-
sd = ps_sd_lookup(data, key->val, 0);
425+
sd = ps_sd_lookup(data, key, 0);
427426
if (sd) {
428427
ps_sd_destroy(data, sd);
429428
}
@@ -454,7 +453,7 @@ PS_GC_FUNC(mm)
454453
for (sd = *ohash; sd; sd = next) {
455454
next = sd->next;
456455
if (sd->ctime < limit) {
457-
ps_mm_debug(("purging %s\n", sd->key));
456+
ps_mm_debug(("purging %s\n", ZSTR_VAL(sd->key)));
458457
ps_sd_destroy(data, sd);
459458
(*nrdels)++;
460459
}
@@ -475,7 +474,7 @@ PS_CREATE_SID_FUNC(mm)
475474
do {
476475
sid = php_session_create_id((void **)&data);
477476
/* Check collision */
478-
if (ps_mm_key_exists(data, sid->val) == SUCCESS) {
477+
if (ps_mm_key_exists(data, sid) == SUCCESS) {
479478
if (sid) {
480479
zend_string_release_ex(sid, 0);
481480
sid = NULL;

0 commit comments

Comments
 (0)