Skip to content

Commit ebbd176

Browse files
dmantipovMikulas Patocka
authored andcommitted
dm: ima: avoid extra calls to strlen()
Since 'scnprintf()' returns the number of characters emitted (not including the trailing '\0'), use that return value instead of the subsequent calls to 'strlen()' where appropriate. Compile tested only. Signed-off-by: Dmitry Antipov <[email protected]> Signed-off-by: Mikulas Patocka <[email protected]>
1 parent 548d88f commit ebbd176

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

drivers/md/dm-ima.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_fl
241241
/*
242242
* First retrieve the target metadata.
243243
*/
244-
scnprintf(target_metadata_buf, DM_IMA_TARGET_METADATA_BUF_LEN,
245-
"target_index=%d,target_begin=%llu,target_len=%llu,",
246-
i, ti->begin, ti->len);
247-
target_metadata_buf_len = strlen(target_metadata_buf);
244+
target_metadata_buf_len =
245+
scnprintf(target_metadata_buf,
246+
DM_IMA_TARGET_METADATA_BUF_LEN,
247+
"target_index=%d,target_begin=%llu,target_len=%llu,",
248+
i, ti->begin, ti->len);
248249

249250
/*
250251
* Then retrieve the actual target data.
@@ -448,11 +449,9 @@ void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap)
448449
if (r)
449450
goto error;
450451

451-
scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
452-
"%sname=%s,uuid=%s;device_resume=no_data;",
453-
DM_IMA_VERSION_STR, dev_name, dev_uuid);
454-
l = strlen(device_table_data);
455-
452+
l = scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
453+
"%sname=%s,uuid=%s;device_resume=no_data;",
454+
DM_IMA_VERSION_STR, dev_name, dev_uuid);
456455
}
457456

458457
capacity_len = strlen(capacity_str);
@@ -561,10 +560,9 @@ void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)
561560
if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio))
562561
goto error;
563562

564-
scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
565-
"%sname=%s,uuid=%s;device_remove=no_data;",
566-
DM_IMA_VERSION_STR, dev_name, dev_uuid);
567-
l = strlen(device_table_data);
563+
l = scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
564+
"%sname=%s,uuid=%s;device_remove=no_data;",
565+
DM_IMA_VERSION_STR, dev_name, dev_uuid);
568566
}
569567

570568
memcpy(device_table_data + l, remove_all_str, remove_all_len);
@@ -647,10 +645,9 @@ void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map)
647645
if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio))
648646
goto error2;
649647

650-
scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
651-
"%sname=%s,uuid=%s;table_clear=no_data;",
652-
DM_IMA_VERSION_STR, dev_name, dev_uuid);
653-
l = strlen(device_table_data);
648+
l = scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
649+
"%sname=%s,uuid=%s;table_clear=no_data;",
650+
DM_IMA_VERSION_STR, dev_name, dev_uuid);
654651
}
655652

656653
capacity_len = strlen(capacity_str);
@@ -706,7 +703,7 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md)
706703
char *old_device_data = NULL, *new_device_data = NULL, *combined_device_data = NULL;
707704
char *new_dev_name = NULL, *new_dev_uuid = NULL, *capacity_str = NULL;
708705
bool noio = true;
709-
int r;
706+
int r, len;
710707

711708
if (dm_ima_alloc_and_copy_device_data(md, &new_device_data,
712709
md->ima.active_table.num_targets, noio))
@@ -728,12 +725,11 @@ void dm_ima_measure_on_device_rename(struct mapped_device *md)
728725
md->ima.active_table.device_metadata = new_device_data;
729726
md->ima.active_table.device_metadata_len = strlen(new_device_data);
730727

731-
scnprintf(combined_device_data, DM_IMA_DEVICE_BUF_LEN * 2,
732-
"%s%snew_name=%s,new_uuid=%s;%s", DM_IMA_VERSION_STR, old_device_data,
733-
new_dev_name, new_dev_uuid, capacity_str);
728+
len = scnprintf(combined_device_data, DM_IMA_DEVICE_BUF_LEN * 2,
729+
"%s%snew_name=%s,new_uuid=%s;%s", DM_IMA_VERSION_STR, old_device_data,
730+
new_dev_name, new_dev_uuid, capacity_str);
734731

735-
dm_ima_measure_data("dm_device_rename", combined_device_data, strlen(combined_device_data),
736-
noio);
732+
dm_ima_measure_data("dm_device_rename", combined_device_data, len, noio);
737733

738734
goto exit;
739735

0 commit comments

Comments
 (0)