3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
+ #undef _POSIX_C_SOURCE
7
+ #define _POSIX_C_SOURCE 200809L /* for strnlen() */
8
+
6
9
#include <errno.h>
7
10
#include <string.h>
8
11
@@ -169,12 +172,13 @@ static int settings_zms_load_subtree(struct settings_store *cs, const struct set
169
172
{
170
173
struct settings_zms * cf = CONTAINER_OF (cs , struct settings_zms , cf_store );
171
174
struct settings_zms_read_fn_arg read_fn_arg ;
172
- char name [SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1 ];
175
+ char name [SETTINGS_FULL_NAME_LEN ];
173
176
ssize_t rc1 ;
174
177
ssize_t rc2 ;
175
178
uint32_t name_hash ;
179
+ size_t name_len = strnlen (arg -> subtree , SETTINGS_FULL_NAME_LEN );
176
180
177
- name_hash = sys_hash32 (arg -> subtree , strlen ( arg -> subtree ) ) & ZMS_HASH_MASK ;
181
+ name_hash = sys_hash32 (arg -> subtree , name_len ) & ZMS_HASH_MASK ;
178
182
for (int i = 0 ; i <= cf -> hash_collision_num ; i ++ ) {
179
183
name_hash = ZMS_UPDATE_COLLISION_NUM (name_hash , i );
180
184
/* Get the name entry from ZMS */
@@ -214,17 +218,21 @@ static ssize_t settings_zms_load_one(struct settings_store *cs, const char *name
214
218
size_t buf_len )
215
219
{
216
220
struct settings_zms * cf = CONTAINER_OF (cs , struct settings_zms , cf_store );
217
- char r_name [SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1 ];
221
+ char r_name [SETTINGS_FULL_NAME_LEN ];
218
222
ssize_t rc = 0 ;
219
223
uint32_t name_hash ;
220
224
uint32_t value_id ;
225
+ size_t name_len ;
221
226
222
227
/* verify that name is not NULL */
223
228
if (!name || !buf ) {
224
229
return - EINVAL ;
225
230
}
226
231
227
- name_hash = sys_hash32 (name , strlen (name )) & ZMS_HASH_MASK ;
232
+ /* get the name length */
233
+ name_len = strnlen (name , SETTINGS_FULL_NAME_LEN );
234
+
235
+ name_hash = sys_hash32 (name , name_len ) & ZMS_HASH_MASK ;
228
236
for (int i = 0 ; i <= cf -> hash_collision_num ; i ++ ) {
229
237
name_hash = ZMS_UPDATE_COLLISION_NUM (name_hash , i );
230
238
/* Get the name entry from ZMS */
@@ -260,7 +268,7 @@ static int settings_zms_load(struct settings_store *cs, const struct settings_lo
260
268
struct settings_zms * cf = CONTAINER_OF (cs , struct settings_zms , cf_store );
261
269
struct settings_zms_read_fn_arg read_fn_arg ;
262
270
struct settings_hash_linked_list settings_element ;
263
- char name [SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1 ];
271
+ char name [SETTINGS_FULL_NAME_LEN ];
264
272
ssize_t rc1 ;
265
273
ssize_t rc2 ;
266
274
uint32_t ll_hash_id ;
@@ -381,23 +389,27 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
381
389
{
382
390
struct settings_zms * cf = CONTAINER_OF (cs , struct settings_zms , cf_store );
383
391
struct settings_hash_linked_list settings_element ;
384
- char rdname [SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1 ];
392
+ char rdname [SETTINGS_FULL_NAME_LEN ];
385
393
uint32_t name_hash ;
386
394
uint32_t collision_num = 0 ;
387
395
bool delete ;
388
396
bool write_name ;
389
397
bool hash_collision ;
390
398
int rc = 0 ;
391
399
int first_available_hash_index = -1 ;
400
+ size_t name_len ;
392
401
393
402
if (!name ) {
394
403
return - EINVAL ;
395
404
}
396
405
406
+ /* get the name length */
407
+ name_len = strnlen (name , SETTINGS_FULL_NAME_LEN );
408
+
397
409
/* Find out if we are doing a delete */
398
410
delete = ((value == NULL ) || (val_len == 0 ));
399
411
400
- name_hash = sys_hash32 (name , strlen ( name ) ) & ZMS_HASH_MASK ;
412
+ name_hash = sys_hash32 (name , name_len ) & ZMS_HASH_MASK ;
401
413
/* MSB is always 1 */
402
414
name_hash |= BIT (31 );
403
415
@@ -421,7 +433,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
421
433
* name
422
434
*/
423
435
rdname [rc ] = '\0' ;
424
- if (! strcmp (name , rdname )) {
436
+ if (( rc == name_len ) && ! memcmp (name , rdname , rc )) {
425
437
/* Hash exist and the names are equal, we should
426
438
* not write the names again.
427
439
*/
@@ -524,7 +536,7 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
524
536
no_ll_update :
525
537
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
526
538
/* Now let's write the name */
527
- rc = zms_write (& cf -> cf_zms , name_hash , name , strlen ( name ) );
539
+ rc = zms_write (& cf -> cf_zms , name_hash , name , name_len );
528
540
if (rc < 0 ) {
529
541
return rc ;
530
542
}
@@ -535,16 +547,20 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
535
547
static ssize_t settings_zms_get_val_len (struct settings_store * cs , const char * name )
536
548
{
537
549
struct settings_zms * cf = CONTAINER_OF (cs , struct settings_zms , cf_store );
538
- char r_name [SETTINGS_MAX_NAME_LEN + SETTINGS_EXTRA_LEN + 1 ];
550
+ char r_name [SETTINGS_FULL_NAME_LEN ];
539
551
ssize_t rc = 0 ;
540
552
uint32_t name_hash ;
553
+ size_t name_len ;
541
554
542
555
/* verify that name is not NULL */
543
556
if (!name ) {
544
557
return - EINVAL ;
545
558
}
546
559
547
- name_hash = sys_hash32 (name , strlen (name )) & ZMS_HASH_MASK ;
560
+ /* get the name length */
561
+ name_len = strnlen (name , SETTINGS_FULL_NAME_LEN );
562
+
563
+ name_hash = sys_hash32 (name , name_len ) & ZMS_HASH_MASK ;
548
564
for (int i = 0 ; i <= cf -> hash_collision_num ; i ++ ) {
549
565
name_hash = ZMS_UPDATE_COLLISION_NUM (name_hash , i );
550
566
/* Get the name entry from ZMS */
0 commit comments