Skip to content

Commit 86e2edd

Browse files
wdc: make char pointer arguments and variables const where appropriate
1. Refactor function signatures to use `const char *` for char pointer arguments that are not intended to be modified. 2. Constify pointers to constant strings. This improves code clarity and prevents accidental modifications to read-only data. Signed-off-by: Tomas Winkler <[email protected]>
1 parent 5fb04ba commit 86e2edd

3 files changed

Lines changed: 61 additions & 54 deletions

File tree

plugins/wdc/wdc-nvme.c

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -922,15 +922,16 @@ struct __packed feature_latency_monitor {
922922
};
923923

924924
static int wdc_get_serial_name(struct nvme_dev *dev, char *file, size_t len, const char *suffix);
925-
static int wdc_create_log_file(char *file, __u8 *drive_log_data, __u32 drive_log_length);
925+
static int wdc_create_log_file(const char *file, const __u8 *drive_log_data,
926+
__u32 drive_log_length);
926927
static int wdc_do_clear_dump(struct nvme_dev *dev, __u8 opcode, __u32 cdw12);
927-
static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len, __u32 cdw12, char *file,
928-
__u32 xfer_size);
928+
static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len, __u32 cdw12,
929+
const char *file, __u32 xfer_size);
929930
static int wdc_do_crash_dump(struct nvme_dev *dev, char *file, int type);
930-
static int wdc_crash_dump(struct nvme_dev *dev, char *file, int type);
931+
static int wdc_crash_dump(struct nvme_dev *dev, const char *file, int type);
931932
static int wdc_get_crash_dump(int argc, char **argv, struct command *command,
932933
struct plugin *plugin);
933-
static int wdc_do_drive_log(struct nvme_dev *dev, char *file);
934+
static int wdc_do_drive_log(struct nvme_dev *dev, const char *file);
934935
static int wdc_drive_log(int argc, char **argv, struct command *command, struct plugin *plugin);
935936
static const char *wdc_purge_mon_status_to_string(__u32 status);
936937
static int wdc_purge(int argc, char **argv, struct command *command, struct plugin *plugin);
@@ -951,7 +952,7 @@ static int wdc_namespace_resize(int argc, char **argv, struct command *command,
951952
static int wdc_do_namespace_resize(struct nvme_dev *dev, __u32 nsid, __u32 op_option);
952953
static int wdc_reason_identifier(int argc, char **argv, struct command *command,
953954
struct plugin *plugin);
954-
static int wdc_do_get_reason_id(struct nvme_dev *dev, char *file, int log_id);
955+
static int wdc_do_get_reason_id(struct nvme_dev *dev, const char *file, int log_id);
955956
static int wdc_save_reason_id(struct nvme_dev *dev, __u8 *rsn_ident, int size);
956957
static int wdc_clear_reason_id(struct nvme_dev *dev);
957958
static int wdc_log_page_directory(int argc, char **argv, struct command *command,
@@ -2135,8 +2136,8 @@ static int wdc_get_serial_name(struct nvme_dev *dev, char *file, size_t len,
21352136
return 0;
21362137
}
21372138

2138-
static int wdc_create_log_file(char *file, __u8 *drive_log_data,
2139-
__u32 drive_log_length)
2139+
static int wdc_create_log_file(const char *file, const __u8 *drive_log_data,
2140+
__u32 drive_log_length)
21402141
{
21412142
int fd;
21422143
int ret;
@@ -2954,7 +2955,7 @@ static __u32 wdc_dump_dui_data_v2(int fd, __u32 dataLen, __u64 offset, __u8 *dum
29542955
}
29552956

29562957
static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len,
2957-
__u32 cdw12, char *file, __u32 xfer_size)
2958+
__u32 cdw12, const char *file, __u32 xfer_size)
29582959
{
29592960
int ret = 0;
29602961
__u8 *dump_data;
@@ -3013,7 +3014,7 @@ static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len,
30133014
}
30143015

30153016
static int wdc_do_dump_e6(int fd, __u32 opcode, __u32 data_len,
3016-
__u32 cdw12, char *file, __u32 xfer_size, __u8 *log_hdr)
3017+
__u32 cdw12, char *file, __u32 xfer_size, __u8 *log_hdr)
30173018
{
30183019
int ret = 0;
30193020
__u8 *dump_data;
@@ -3087,8 +3088,8 @@ static int wdc_do_dump_e6(int fd, __u32 opcode, __u32 data_len,
30873088
return ret;
30883089
}
30893090

3090-
static int wdc_do_cap_telemetry_log(struct nvme_dev *dev, char *file,
3091-
__u32 bs, int type, int data_area)
3091+
static int wdc_do_cap_telemetry_log(struct nvme_dev *dev, const char *file,
3092+
__u32 bs, int type, int data_area)
30923093
{
30933094
struct nvme_telemetry_log *log;
30943095
size_t full_size = 0;
@@ -3730,9 +3731,9 @@ static int wdc_cap_diag(int argc, char **argv, struct command *command,
37303731
struct plugin *plugin)
37313732
{
37323733
nvme_root_t r;
3733-
char *desc = "Capture Diagnostics Log.";
3734-
char *file = "Output file pathname.";
3735-
char *size = "Data retrieval transfer size.";
3734+
const char *desc = "Capture Diagnostics Log.";
3735+
const char *file = "Output file pathname.";
3736+
const char *size = "Data retrieval transfer size.";
37363737
__u64 capabilities = 0;
37373738
char f[PATH_MAX] = {0};
37383739
struct nvme_dev *dev;
@@ -4023,7 +4024,7 @@ static int wdc_do_sn730_get_and_tar(int fd, char *outputName)
40234024
return ret;
40244025
}
40254026

4026-
static int dump_internal_logs(struct nvme_dev *dev, char *dir_name, int verbose)
4027+
static int dump_internal_logs(struct nvme_dev *dev, const char *dir_name, int verbose)
40274028
{
40284029
char file_path[PATH_MAX];
40294030
void *telemetry_log;
@@ -4122,14 +4123,19 @@ static int dump_internal_logs(struct nvme_dev *dev, char *dir_name, int verbose)
41224123
static int wdc_vs_internal_fw_log(int argc, char **argv, struct command *command,
41234124
struct plugin *plugin)
41244125
{
4125-
char *desc = "Internal Firmware Log.";
4126-
char *file = "Output file pathname.";
4127-
char *size = "Data retrieval transfer size.";
4128-
char *data_area = "Data area to retrieve up to. Currently only supported on the SN340, SN640, SN730, and SN840 devices.";
4129-
char *file_size = "Output file size. Currently only supported on the SN340 device.";
4130-
char *offset = "Output file data offset. Currently only supported on the SN340 device.";
4131-
char *type = "Telemetry type - NONE, HOST, or CONTROLLER. Currently only supported on the SN530, SN640, SN730, SN740, SN810, SN840 and ZN350 devices.";
4132-
char *verbose = "Display more debug messages.";
4126+
const char *desc = "Internal Firmware Log.";
4127+
const char *file = "Output file pathname.";
4128+
const char *size = "Data retrieval transfer size.";
4129+
const char *data_area = "Data area to retrieve up to. "
4130+
"Currently only supported on the SN340, SN640, SN730, and SN840 devices.";
4131+
const char *file_size = "Output file size. "
4132+
"Currently only supported on the SN340 device.";
4133+
const char *offset = "Output file data offset. "
4134+
"Currently only supported on the SN340 device.";
4135+
const char *type = "Telemetry type - NONE, HOST, or CONTROLLER. "
4136+
"Currently only supported on the "
4137+
"SN530, SN640, SN730, SN740, SN810, SN840 and ZN350 devices.";
4138+
const char *verbose = "Display more debug messages.";
41334139
char f[PATH_MAX] = {0};
41344140
char fb[PATH_MAX/2] = {0};
41354141
char fileSuffix[PATH_MAX] = {0};
@@ -4459,7 +4465,7 @@ static int wdc_do_crash_dump(struct nvme_dev *dev, char *file, int type)
44594465
return ret;
44604466
}
44614467

4462-
static int wdc_crash_dump(struct nvme_dev *dev, char *file, int type)
4468+
static int wdc_crash_dump(struct nvme_dev *dev, const char *file, int type)
44634469
{
44644470
char f[PATH_MAX] = {0};
44654471
const char *dump_type;
@@ -4481,7 +4487,7 @@ static int wdc_crash_dump(struct nvme_dev *dev, char *file, int type)
44814487
return ret;
44824488
}
44834489

4484-
static int wdc_do_drive_log(struct nvme_dev *dev, char *file)
4490+
static int wdc_do_drive_log(struct nvme_dev *dev, const char *file)
44854491
{
44864492
int ret;
44874493
__u8 *drive_log_data;
@@ -4626,8 +4632,8 @@ static int wdc_get_crash_dump(int argc, char **argv, struct command *command,
46264632
static int wdc_get_pfail_dump(int argc, char **argv, struct command *command,
46274633
struct plugin *plugin)
46284634
{
4629-
char *desc = "Get Pfail Crash Dump.";
4630-
char *file = "Output file pathname.";
4635+
const char *desc = "Get Pfail Crash Dump.";
4636+
const char *file = "Output file pathname.";
46314637
__u64 capabilities = 0;
46324638
struct nvme_dev *dev;
46334639
struct config {
@@ -5065,7 +5071,7 @@ static void wdc_print_latency_monitor_log_json(struct wdc_ssd_latency_monitor_lo
50655071
{
50665072
int i, j;
50675073
char buf[128];
5068-
char *operation[3] = {"Read", "Write", "Trim"};
5074+
const char *operation[3] = {"Read", "Write", "Trim"};
50695075
struct json_object *root = json_create_object();
50705076

50715077
json_object_add_value_int(root, "Feature Status", log_data->feature_status);
@@ -5888,7 +5894,7 @@ static void wdc_print_fw_act_history_log_normal(__u8 *data, int num_entries,
58885894
__u16 oldestEntryIdx = 0, entryIdx = 0;
58895895
uint64_t timestamp;
58905896
__u64 timestamp_sec;
5891-
char *null_fw = "--------";
5897+
const char *null_fw = "--------";
58925898

58935899
memset((void *)time_str, '\0', 100);
58945900

@@ -8887,7 +8893,7 @@ static int wdc_do_clear_pcie_correctable_errors_fid(int fd)
88878893
static int wdc_clear_pcie_correctable_errors(int argc, char **argv, struct command *command,
88888894
struct plugin *plugin)
88898895
{
8890-
char *desc = "Clear PCIE Correctable Errors.";
8896+
const char *desc = "Clear PCIE Correctable Errors.";
88918897
__u64 capabilities = 0;
88928898
struct nvme_dev *dev;
88938899
nvme_root_t r;
@@ -8930,7 +8936,7 @@ static int wdc_clear_pcie_correctable_errors(int argc, char **argv, struct comma
89308936
static int wdc_drive_status(int argc, char **argv, struct command *command,
89318937
struct plugin *plugin)
89328938
{
8933-
char *desc = "Get Drive Status.";
8939+
const char *desc = "Get Drive Status.";
89348940
struct nvme_dev *dev;
89358941
int ret = 0;
89368942
nvme_root_t r;
@@ -9058,7 +9064,7 @@ static int wdc_drive_status(int argc, char **argv, struct command *command,
90589064
static int wdc_clear_assert_dump(int argc, char **argv, struct command *command,
90599065
struct plugin *plugin)
90609066
{
9061-
char *desc = "Clear Assert Dump Present Status.";
9067+
const char *desc = "Clear Assert Dump Present Status.";
90629068
struct nvme_dev *dev;
90639069
int ret = -1;
90649070
nvme_root_t r;
@@ -9367,7 +9373,7 @@ static int wdc_do_clear_fw_activate_history_fid(int fd)
93679373
static int wdc_clear_fw_activate_history(int argc, char **argv, struct command *command,
93689374
struct plugin *plugin)
93699375
{
9370-
char *desc = "Clear FW activate history table.";
9376+
const char *desc = "Clear FW activate history table.";
93719377
__u64 capabilities = 0;
93729378
struct nvme_dev *dev;
93739379
nvme_root_t r;
@@ -9403,10 +9409,10 @@ static int wdc_clear_fw_activate_history(int argc, char **argv, struct command *
94039409
static int wdc_vs_telemetry_controller_option(int argc, char **argv, struct command *command,
94049410
struct plugin *plugin)
94059411
{
9406-
char *desc = "Disable/Enable Controller Option of the Telemetry Log Page.";
9407-
char *disable = "Disable controller option of the telemetry log page.";
9408-
char *enable = "Enable controller option of the telemetry log page.";
9409-
char *status = "Displays the current state of the controller initiated log page.";
9412+
const char *desc = "Disable/Enable Controller Option of the Telemetry Log Page.";
9413+
const char *disable = "Disable controller option of the telemetry log page.";
9414+
const char *enable = "Enable controller option of the telemetry log page.";
9415+
const char *status = "Displays the current state of the controller initiated log page.";
94109416
__u64 capabilities = 0;
94119417
struct nvme_dev *dev;
94129418
nvme_root_t r;
@@ -9795,7 +9801,8 @@ static int wdc_fetch_log_file_from_device(struct nvme_dev *dev, __u32 fileId,
97959801
return ret;
97969802
}
97979803

9798-
static int wdc_de_get_dump_trace(struct nvme_dev *dev, char *filePath, __u16 binFileNameLen, char *binFileName)
9804+
static int wdc_de_get_dump_trace(struct nvme_dev *dev, const char *filePath, __u16 binFileNameLen,
9805+
const char *binFileName)
97999806
{
98009807
int ret = WDC_STATUS_FAILURE;
98019808
__u8 *readBuffer = NULL;
@@ -10204,8 +10211,8 @@ static int wdc_do_drive_essentials(nvme_root_t r, struct nvme_dev *dev,
1020410211
static int wdc_drive_essentials(int argc, char **argv, struct command *command,
1020510212
struct plugin *plugin)
1020610213
{
10207-
char *desc = "Capture Drive Essentials.";
10208-
char *dirName = "Output directory pathname.";
10214+
const char *desc = "Capture Drive Essentials.";
10215+
const char *dirName = "Output directory pathname.";
1020910216
char d[PATH_MAX] = {0};
1021010217
char k[PATH_MAX] = {0};
1021110218
__u64 capabilities = 0;
@@ -10219,7 +10226,7 @@ static int wdc_drive_essentials(int argc, char **argv, struct command *command,
1021910226
};
1022010227

1022110228
struct config cfg = {
10222-
.dirName = NULL,
10229+
.dirName = NULL,
1022310230
};
1022410231

1022510232
OPT_ARGS(opts) = {
@@ -10837,7 +10844,7 @@ static int wdc_get_drive_reason_id(struct nvme_dev *dev, char *drive_reason_id,
1083710844
int ret;
1083810845
int res_len = 0;
1083910846
struct nvme_id_ctrl ctrl;
10840-
char *reason_id_str = "reason_id";
10847+
const char *reason_id_str = "reason_id";
1084110848

1084210849
i = sizeof(ctrl.sn) - 1;
1084310850
j = sizeof(ctrl.mn) - 1;
@@ -10957,7 +10964,7 @@ static int wdc_dump_telemetry_hdr(struct nvme_dev *dev, int log_id, struct nvme_
1095710964
return ret;
1095810965
}
1095910966

10960-
static int wdc_do_get_reason_id(struct nvme_dev *dev, char *file, int log_id)
10967+
static int wdc_do_get_reason_id(struct nvme_dev *dev, const char *file, int log_id)
1096110968
{
1096210969
int ret;
1096310970
struct nvme_telemetry_log *log_hdr;
@@ -12161,10 +12168,10 @@ static int wdc_cloud_boot_SSD_version(int argc, char **argv, struct command *com
1216112168

1216212169
static int wdc_enc_get_log(int argc, char **argv, struct command *command, struct plugin *plugin)
1216312170
{
12164-
char *desc = "Get Enclosure Log.";
12165-
char *file = "Output file pathname.";
12166-
char *size = "Data retrieval transfer size.";
12167-
char *log = "Enclosure Log Page ID.";
12171+
const char *desc = "Get Enclosure Log.";
12172+
const char *file = "Output file pathname.";
12173+
const char *size = "Data retrieval transfer size.";
12174+
const char *log = "Enclosure Log Page ID.";
1216812175
struct nvme_dev *dev;
1216912176
FILE *output_fd;
1217012177
int xfer_size = 0;

plugins/wdc/wdc-utils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ int wdc_UtilsGetTime(PUtilsTimeInfo timeInfo)
9090
return WDC_STATUS_SUCCESS;
9191
}
9292

93-
int wdc_UtilsCreateDir(char *path)
93+
int wdc_UtilsCreateDir(const char *path)
9494
{
9595
int retStatus;
9696
int status = WDC_STATUS_SUCCESS;
@@ -111,7 +111,7 @@ int wdc_UtilsCreateDir(char *path)
111111
return status;
112112
}
113113

114-
int wdc_WriteToFile(char *fileName, char *buffer, unsigned int bufferLen)
114+
int wdc_WriteToFile(const char *fileName, const char *buffer, unsigned int bufferLen)
115115
{
116116
int status = WDC_STATUS_SUCCESS;
117117
FILE *file;
@@ -143,7 +143,7 @@ int wdc_WriteToFile(char *fileName, char *buffer, unsigned int bufferLen)
143143
* 1 if the pcSrc string is lexically higher than pcDst or
144144
* -1 if the pcSrc string is lexically lower than pcDst.
145145
*/
146-
int wdc_UtilsStrCompare(char *pcSrc, char *pcDst)
146+
int wdc_UtilsStrCompare(const char *pcSrc, const char *pcDst)
147147
{
148148
while ((toupper(*pcSrc) == toupper(*pcDst)) && (*pcSrc != '\0')) {
149149
pcSrc++;

plugins/wdc/wdc-utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ typedef struct _UtilsTimeInfo
7373
int wdc_UtilsSnprintf(char *buffer, unsigned int sizeOfBuffer, const char *format, ...);
7474
void wdc_UtilsDeleteCharFromString(char* buffer, int buffSize, char charToRemove);
7575
int wdc_UtilsGetTime(PUtilsTimeInfo timeInfo);
76-
int wdc_UtilsStrCompare(char *pcSrc, char *pcDst);
77-
int wdc_UtilsCreateDir(char *path);
78-
int wdc_WriteToFile(char *fileName, char *buffer, unsigned int bufferLen);
76+
int wdc_UtilsStrCompare(const char *pcSrc, const char *pcDst);
77+
int wdc_UtilsCreateDir(const char *path);
78+
int wdc_WriteToFile(const char *fileName, const char *buffer, unsigned int bufferLen);
7979
void wdc_StrFormat(char *formatter, size_t fmt_sz, char *tofmt, size_t tofmtsz);
8080
bool wdc_CheckUuidListSupport(struct nvme_dev *dev, struct nvme_id_uuid_list *uuid_list);

0 commit comments

Comments
 (0)