Skip to content

Commit c5885f4

Browse files
SolidigmToolsigaw
authored andcommitted
plugins/solidigm: Replace snprintf with asprintf
Replace snprintf with better memory management option Signed-off-by: hsdhillo <[email protected]>
1 parent db2c3e5 commit c5885f4

2 files changed

Lines changed: 49 additions & 35 deletions

File tree

plugins/solidigm/solidigm-internal-logs.c

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static int ilog_dump_assert_logs(struct ilog *ilog)
233233
{
234234
__u8 buf[INTERNAL_LOG_MAX_BYTE_TRANSFER];
235235
__u8 head_buf[INTERNAL_LOG_MAX_BYTE_TRANSFER];
236-
char file_path[PATH_MAX] = {0};
236+
_cleanup_free_ char *file_path = NULL;
237237
char file_name[] = "AssertLog.bin";
238238
struct assert_dump_header *ad = (struct assert_dump_header *) head_buf;
239239
struct nvme_passthru_cmd cmd = {
@@ -249,8 +249,10 @@ static int ilog_dump_assert_logs(struct ilog *ilog)
249249
if (err)
250250
return err;
251251

252-
snprintf(file_path, sizeof(file_path), "%.*s/%s",
253-
(int) (sizeof(file_path) - sizeof(file_name) - 1), ilog->cfg->out_dir, file_name);
252+
if (asprintf(&file_path, "%.*s/%s",
253+
(int) (sizeof(file_path) - sizeof(file_name) - 1),
254+
ilog->cfg->out_dir, file_name) < 0)
255+
return -errno;
254256
output = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, LOG_FILE_PERMISSION);
255257
if (output < 0)
256258
return -errno;
@@ -289,7 +291,7 @@ static int ilog_dump_event_logs(struct ilog *ilog)
289291
{
290292
__u8 buf[INTERNAL_LOG_MAX_BYTE_TRANSFER];
291293
__u8 head_buf[INTERNAL_LOG_MAX_BYTE_TRANSFER];
292-
char file_path[PATH_MAX] = {0};
294+
_cleanup_free_ char *file_path = NULL;
293295
struct event_dump_header *ehdr = (struct event_dump_header *) head_buf;
294296
struct nvme_passthru_cmd cmd = {
295297
.opcode = 0xd2,
@@ -304,7 +306,8 @@ static int ilog_dump_event_logs(struct ilog *ilog)
304306
err = read_header(&cmd, dev_fd(ilog->dev));
305307
if (err)
306308
return err;
307-
snprintf(file_path, sizeof(file_path) - 1, "%s/EventLog.bin", ilog->cfg->out_dir);
309+
if (asprintf(&file_path, "%s/EventLog.bin", ilog->cfg->out_dir))
310+
return -errno;
308311
output = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, LOG_FILE_PERMISSION);
309312
if (output < 0)
310313
return -errno;
@@ -363,7 +366,7 @@ static int ilog_dump_nlogs(struct ilog *ilog, int core)
363366
int err = 0;
364367
__u32 count, core_num;
365368
__u8 buf[INTERNAL_LOG_MAX_BYTE_TRANSFER];
366-
char file_path[PATH_MAX] = {0};
369+
_cleanup_free_ char *file_path = NULL;
367370
struct nlog_dump_header_common *nlog_header = (struct nlog_dump_header_common *)buf;
368371
struct nvme_passthru_cmd cmd = {
369372
.opcode = 0xd2,
@@ -400,11 +403,12 @@ static int ilog_dump_nlogs(struct ilog *ilog, int core)
400403
count = nlog_header->totalnlogs;
401404
core_num = core < 0 ? nlog_header->corecount : 0;
402405
if (!header_size) {
403-
snprintf(file_path, sizeof(file_path) - 1, "%s/NLog.bin",
404-
ilog->cfg->out_dir);
405-
output = open(file_path, O_WRONLY | O_CREAT | O_TRUNC,
406-
LOG_FILE_PERMISSION);
407-
if (output < 0)
406+
if (asprintf(&file_path, "%s/NLog.bin", ilog->cfg->out_dir) >= 0) {
407+
output = open(file_path, O_WRONLY | O_CREAT | O_TRUNC,
408+
LOG_FILE_PERMISSION);
409+
if (output < 0)
410+
return -errno;
411+
} else
408412
return -errno;
409413
header_size = get_nlog_header_size(nlog_header);
410414
is_open = true;
@@ -416,7 +420,7 @@ static int ilog_dump_nlogs(struct ilog *ilog, int core)
416420
print_nlog_header(buf);
417421
cmd.cdw13 = 0x400;
418422
err = cmd_dump_repeat(&cmd, nlog_header->nlogbytesize / 4,
419-
output, dev_fd(ilog->dev), true);
423+
output, dev_fd(ilog->dev), true);
420424
if (err)
421425
break;
422426
} while (++log_select.selectNlog < count);
@@ -432,16 +436,19 @@ static int ilog_dump_nlogs(struct ilog *ilog, int core)
432436

433437
int ensure_dir(const char *parent_dir_name, const char *name)
434438
{
435-
char file_path[PATH_MAX] = {0};
439+
_cleanup_free_ char *file_path = NULL;
436440
struct stat sb;
437441

438-
snprintf(file_path, sizeof(file_path) - 1, "%s/%s", parent_dir_name, name);
442+
if (asprintf(&file_path, "%s/%s", parent_dir_name, name) < 0)
443+
return -errno;
444+
439445
if (!(stat(file_path, &sb) == 0 && S_ISDIR(sb.st_mode))) {
440446
if (mkdir(file_path, 777) != 0) {
441447
perror(file_path);
442448
return -errno;
443449
}
444450
}
451+
445452
return 0;
446453
}
447454

@@ -456,13 +463,14 @@ static int log_save(struct log *log, const char *parent_dir_name, const char *su
456463
const char *file_name, __u8 *buffer, size_t buf_size)
457464
{
458465
_cleanup_fd_ int output = -1;
459-
char file_path[PATH_MAX] = {0};
466+
_cleanup_free_ char *file_path = NULL;
460467
size_t bytes_remaining = 0;
461468

462469
ensure_dir(parent_dir_name, subdir_name);
463470

464-
snprintf(file_path, sizeof(file_path) - 1, "%s/%s/%s", parent_dir_name, subdir_name,
465-
file_name);
471+
if (asprintf(&file_path, "%s/%s/%s", parent_dir_name, subdir_name, file_name) < 0)
472+
return -errno;
473+
466474
output = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, LOG_FILE_PERMISSION);
467475
if (output < 0)
468476
return -errno;
@@ -486,15 +494,15 @@ static int ilog_dump_identify_page(struct ilog *ilog, struct log *cns, __u32 nsi
486494
{
487495
__u8 data[NVME_IDENTIFY_DATA_SIZE];
488496
__u8 *buff = cns->buffer ? cns->buffer : data;
489-
char filename[sizeof(
490-
"cntid_XXXXX_cns_XXX_nsid_XXXXXXXXXX_nvmsetid_XXXXX_csi_XXX.bin")] = {0};
497+
_cleanup_free_ char *filename = NULL;
491498
int err = nvme_identify_cns_nsid(dev_fd(ilog->dev), cns->id, nsid, buff);
492499

493500
if (err)
494501
return err;
495502

496-
snprintf(filename, sizeof(filename) - 1, "cntid_0_cns_%d_nsid_%d_nvmsetid_0_csi_0.bin",
497-
cns->id, nsid);
503+
if (asprintf(&filename, "cntid_0_cns_%d_nsid_%d_nvmsetid_0_csi_0.bin", cns->id, nsid) < 0)
504+
return -errno;
505+
498506
return log_save(cns, ilog->cfg->out_dir, "identify", filename, buff, sizeof(data));
499507
}
500508

@@ -646,9 +654,9 @@ static int ilog_dump_identify_pages(struct ilog *ilog)
646654
static int ilog_dump_log_page(struct ilog *ilog, struct log *lp, __u32 nsid)
647655
{
648656
__u8 *buff = lp->buffer;
649-
char filename[sizeof("lid_0xXX_lsp_0xXX_lsi_0xXXXX.bin")] = {0};
650-
int err;
657+
_cleanup_free_ char *filename = NULL;
651658

659+
int err;
652660
if (!lp->buffer_size)
653661
return -EINVAL;
654662
if (!buff) {
@@ -660,8 +668,9 @@ static int ilog_dump_log_page(struct ilog *ilog, struct log *lp, __u32 nsid)
660668
if (err)
661669
return err;
662670

663-
snprintf(filename, sizeof(filename), "lid_0x%02x_lsp_0x00_lsi_0x0000.bin",
664-
lp->id);
671+
if (asprintf(&filename, "lid_0x%02x_lsp_0x00_lsi_0x0000.bin", lp->id) < 0)
672+
return -errno;
673+
665674
return log_save(lp, ilog->cfg->out_dir, "log_pages", filename, buff, lp->buffer_size);
666675
}
667676

@@ -813,10 +822,11 @@ int solidigm_get_internal_log(int argc, char **argv, struct command *command,
813822
{
814823
char sn_prefix[sizeof(((struct nvme_id_ctrl *)0)->sn)+1];
815824
char date_str[sizeof("-YYYYMMDDHHMMSS")];
816-
char full_folder[PATH_MAX] = {0};
817-
char unique_folder[sizeof(sn_prefix)+sizeof(date_str)-1] = {0};
825+
_cleanup_free_ char *full_folder = NULL;
826+
_cleanup_free_ char *unique_folder = NULL;
827+
_cleanup_free_ char *zip_name = NULL;
828+
818829
char *initial_folder;
819-
char zip_name[PATH_MAX] = {0};
820830
char *output_path;
821831
struct ilog ilog = {0};
822832
int err;
@@ -885,12 +895,16 @@ int solidigm_get_internal_log(int argc, char **argv, struct command *command,
885895

886896
current_time = time(NULL);
887897
strftime(date_str, sizeof(date_str), "-%Y%m%d%H%M%S", localtime(&current_time));
888-
snprintf(unique_folder, sizeof(unique_folder), "%s%s", sn_prefix, date_str);
889-
snprintf(full_folder, sizeof(full_folder) - 1, "%s/%s", cfg.out_dir, unique_folder);
898+
if (asprintf(&unique_folder, "%s%s", sn_prefix, date_str) < 0)
899+
return -errno;
900+
if (asprintf(&full_folder, "%s/%s", cfg.out_dir, unique_folder) < 0)
901+
return -errno;
902+
890903
if (mkdir(full_folder, 0755) != 0) {
891904
perror("mkdir");
892905
return -errno;
893906
}
907+
894908
cfg.out_dir = full_folder;
895909
output_path = full_folder;
896910

@@ -946,10 +960,12 @@ int solidigm_get_internal_log(int argc, char **argv, struct command *command,
946960

947961
if (ilog.count > 0) {
948962
int ret_cmd;
949-
char *cmd;
963+
_cleanup_free_ char *cmd = NULL;
950964
char *quiet = cfg.verbose ? "" : " -q";
951965

952-
snprintf(zip_name, sizeof(zip_name) - 1, "%s.zip", unique_folder);
966+
if (asprintf(&zip_name, "%s.zip", unique_folder) < 0)
967+
return -errno;
968+
953969
if (asprintf(&cmd, "cd \"%s\" && zip -MM -r \"../%s\" ./* %s", cfg.out_dir,
954970
zip_name, quiet) < 0) {
955971
err = errno;
@@ -962,7 +978,6 @@ int solidigm_get_internal_log(int argc, char **argv, struct command *command,
962978
perror(cmd);
963979
else {
964980
output_path = zip_name;
965-
free(cmd);
966981
if (asprintf(&cmd, "rm -rf %s", cfg.out_dir) < 0) {
967982
err = errno;
968983
perror("Can't allocate string for cleanup");
@@ -971,7 +986,6 @@ int solidigm_get_internal_log(int argc, char **argv, struct command *command,
971986
if (system(cmd) != 0)
972987
perror("Failed removing logs folder");
973988
}
974-
free(cmd);
975989
}
976990

977991
out:

plugins/solidigm/solidigm-nvme.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
#include "cmd.h"
1515

16-
#define SOLIDIGM_PLUGIN_VERSION "1.8"
16+
#define SOLIDIGM_PLUGIN_VERSION "1.9"
1717

1818
PLUGIN(NAME("solidigm", "Solidigm vendor specific extensions", SOLIDIGM_PLUGIN_VERSION),
1919
COMMAND_LIST(

0 commit comments

Comments
 (0)