Skip to content

Commit 8eaf299

Browse files
committed
Refactor psk file code
1 parent 44e5015 commit 8eaf299

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/psk_file.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ static int psk__cleanup(struct mosquitto__psk **psk);
3131
static int psk__file_parse(struct mosquitto__psk **psk_id, const char *psk_file);
3232

3333

34+
static void psk__free_item(struct mosquitto__psk *psk)
35+
{
36+
mosquitto_FREE(psk->username);
37+
mosquitto_FREE(psk->password);
38+
mosquitto_FREE(psk);
39+
}
40+
41+
3442
int psk_file__init(void)
3543
{
3644
int rc;
@@ -134,7 +142,7 @@ static int pwfile__parse(const char *file, struct mosquitto__psk **root)
134142

135143
psk->username = mosquitto_strdup(username);
136144
if(!psk->username){
137-
mosquitto_FREE(psk);
145+
psk__free_item(psk);
138146
mosquitto_FREE(buf);
139147
fclose(pwfile);
140148
return MOSQ_ERR_NOMEM;
@@ -145,25 +153,21 @@ static int pwfile__parse(const char *file, struct mosquitto__psk **root)
145153

146154
if(strlen(password) > 65535){
147155
log__printf(NULL, MOSQ_LOG_NOTICE, "Warning: Invalid line in password file '%s', password too long.", file);
148-
mosquitto_FREE(psk->username);
149-
mosquitto_FREE(psk);
156+
psk__free_item(psk);
150157
continue;
151158
}
152159

153160
psk->password = mosquitto_strdup(password);
154161
if(!psk->password){
155162
log__printf(NULL, MOSQ_LOG_NOTICE, "Warning: Unable to decode line in password file '%s'.", file);
156-
mosquitto_FREE(psk->username);
157-
mosquitto_FREE(psk);
163+
psk__free_item(psk);
158164
continue;
159165
}
160166

161167
HASH_ADD_KEYPTR(hh, *root, psk->username, strlen(psk->username), psk);
162168
}else{
163169
log__printf(NULL, MOSQ_LOG_NOTICE, "Warning: Invalid line in psk file '%s': %s", file, buf);
164-
mosquitto_FREE(psk->username);
165-
mosquitto_FREE(psk->password);
166-
mosquitto_FREE(psk);
170+
psk__free_item(psk);
167171
}
168172
}
169173
}
@@ -175,15 +179,6 @@ static int pwfile__parse(const char *file, struct mosquitto__psk **root)
175179
}
176180

177181

178-
void psk__free_item(struct mosquitto__psk **psk, struct mosquitto__psk *item)
179-
{
180-
mosquitto_FREE(item->username);
181-
mosquitto_FREE(item->password);
182-
HASH_DEL(*psk, item);
183-
mosquitto_FREE(item);
184-
}
185-
186-
187182
static int psk__file_parse(struct mosquitto__psk **psk_id, const char *psk_file)
188183
{
189184
int rc;
@@ -220,9 +215,7 @@ static int psk__cleanup(struct mosquitto__psk **root)
220215

221216
HASH_ITER(hh, *root, psk, tmp){
222217
HASH_DEL(*root, psk);
223-
mosquitto_FREE(psk->username);
224-
mosquitto_FREE(psk->password);
225-
mosquitto_FREE(psk);
218+
psk__free_item(psk);
226219
}
227220

228221
*root = NULL;

0 commit comments

Comments
 (0)