Skip to content

Commit da94d43

Browse files
tomas-winkler-sndkigaw
authored andcommitted
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 360bd10 commit da94d43

3 files changed

Lines changed: 59 additions & 54 deletions

File tree

plugins/wdc/wdc-nvme.c

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

925925
static int wdc_get_serial_name(struct nvme_dev *dev, char *file, size_t len, const char *suffix);
926-
static int wdc_create_log_file(char *file, __u8 *drive_log_data, __u32 drive_log_length);
926+
static int wdc_create_log_file(const char *file, const __u8 *drive_log_data,
927+
__u32 drive_log_length);
927928
static int wdc_do_clear_dump(struct nvme_dev *dev, __u8 opcode, __u32 cdw12);
928-
static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len, __u32 cdw12, char *file,
929-
__u32 xfer_size);
929+
static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len, __u32 cdw12,
930+
const char *file, __u32 xfer_size);
930931
static int wdc_do_crash_dump(struct nvme_dev *dev, char *file, int type);
931-
static int wdc_crash_dump(struct nvme_dev *dev, char *file, int type);
932+
static int wdc_crash_dump(struct nvme_dev *dev, const char *file, int type);
932933
static int wdc_get_crash_dump(int argc, char **argv, struct command *command,
933934
struct plugin *plugin);
934-
static int wdc_do_drive_log(struct nvme_dev *dev, char *file);
935+
static int wdc_do_drive_log(struct nvme_dev *dev, const char *file);
935936
static int wdc_drive_log(int argc, char **argv, struct command *command, struct plugin *plugin);
936937
static const char *wdc_purge_mon_status_to_string(__u32 status);
937938
static int wdc_purge(int argc, char **argv, struct command *command, struct plugin *plugin);
@@ -955,7 +956,7 @@ static int wdc_namespace_resize(int argc, char **argv, struct command *command,
955956
static int wdc_do_namespace_resize(struct nvme_dev *dev, __u32 nsid, __u32 op_option);
956957
static int wdc_reason_identifier(int argc, char **argv, struct command *command,
957958
struct plugin *plugin);
958-
static int wdc_do_get_reason_id(struct nvme_dev *dev, char *file, int log_id);
959+
static int wdc_do_get_reason_id(struct nvme_dev *dev, const char *file, int log_id);
959960
static int wdc_save_reason_id(struct nvme_dev *dev, __u8 *rsn_ident, int size);
960961
static int wdc_clear_reason_id(struct nvme_dev *dev);
961962
static int wdc_log_page_directory(int argc, char **argv, struct command *command,
@@ -2150,8 +2151,8 @@ static int wdc_get_serial_name(struct nvme_dev *dev, char *file, size_t len,
21502151
return 0;
21512152
}
21522153

2153-
static int wdc_create_log_file(char *file, __u8 *drive_log_data,
2154-
__u32 drive_log_length)
2154+
static int wdc_create_log_file(const char *file, const __u8 *drive_log_data,
2155+
__u32 drive_log_length)
21552156
{
21562157
int fd;
21572158
int ret;
@@ -3022,7 +3023,7 @@ static __u32 wdc_dump_dui_data_v2(int fd, __u32 dataLen, __u64 offset, __u8 *dum
30223023
}
30233024

30243025
static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len,
3025-
__u32 cdw12, char *file, __u32 xfer_size)
3026+
__u32 cdw12, const char *file, __u32 xfer_size)
30263027
{
30273028
int ret = 0;
30283029
__u8 *dump_data;
@@ -3081,7 +3082,7 @@ static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len,
30813082
}
30823083

30833084
static int wdc_do_dump_e6(int fd, __u32 opcode, __u32 data_len,
3084-
__u32 cdw12, char *file, __u32 xfer_size, __u8 *log_hdr)
3085+
__u32 cdw12, char *file, __u32 xfer_size, __u8 *log_hdr)
30853086
{
30863087
int ret = 0;
30873088
__u8 *dump_data;
@@ -3155,8 +3156,8 @@ static int wdc_do_dump_e6(int fd, __u32 opcode, __u32 data_len,
31553156
return ret;
31563157
}
31573158

3158-
static int wdc_do_cap_telemetry_log(struct nvme_dev *dev, char *file,
3159-
__u32 bs, int type, int data_area)
3159+
static int wdc_do_cap_telemetry_log(struct nvme_dev *dev, const char *file,
3160+
__u32 bs, int type, int data_area)
31603161
{
31613162
struct nvme_telemetry_log *log;
31623163
size_t full_size = 0;
@@ -3798,9 +3799,9 @@ static int wdc_cap_diag(int argc, char **argv, struct command *command,
37983799
struct plugin *plugin)
37993800
{
38003801
nvme_root_t r;
3801-
char *desc = "Capture Diagnostics Log.";
3802-
char *file = "Output file pathname.";
3803-
char *size = "Data retrieval transfer size.";
3802+
const char *desc = "Capture Diagnostics Log.";
3803+
const char *file = "Output file pathname.";
3804+
const char *size = "Data retrieval transfer size.";
38043805
__u64 capabilities = 0;
38053806
char f[PATH_MAX] = {0};
38063807
struct nvme_dev *dev;
@@ -4091,7 +4092,7 @@ static int wdc_do_sn730_get_and_tar(int fd, char *outputName)
40914092
return ret;
40924093
}
40934094

4094-
static int dump_internal_logs(struct nvme_dev *dev, char *dir_name, int verbose)
4095+
static int dump_internal_logs(struct nvme_dev *dev, const char *dir_name, int verbose)
40954096
{
40964097
char file_path[PATH_MAX];
40974098
void *telemetry_log;
@@ -4190,14 +4191,17 @@ static int dump_internal_logs(struct nvme_dev *dev, char *dir_name, int verbose)
41904191
static int wdc_vs_internal_fw_log(int argc, char **argv, struct command *command,
41914192
struct plugin *plugin)
41924193
{
4193-
char *desc = "Internal Firmware Log.";
4194-
char *file = "Output file pathname.";
4195-
char *size = "Data retrieval transfer size.";
4196-
char *data_area = "Data area to retrieve up to. Currently only supported on the SN340, SN640, SN730, and SN840 devices.";
4197-
char *file_size = "Output file size. Currently only supported on the SN340 device.";
4198-
char *offset = "Output file data offset. Currently only supported on the SN340 device.";
4199-
char *type = "Telemetry type - NONE, HOST, or CONTROLLER. Currently only supported on the SN530, SN640, SN730, SN740, SN810, SN840 and ZN350 devices.";
4200-
char *verbose = "Display more debug messages.";
4194+
const char *desc = "Internal Firmware Log.";
4195+
const char *file = "Output file pathname.";
4196+
const char *size = "Data retrieval transfer size.";
4197+
const char *data_area =
4198+
"Data area to retrieve up to. Currently only supported on the SN340, SN640, SN730, and SN840 devices.";
4199+
const char *file_size = "Output file size. Currently only supported on the SN340 device.";
4200+
const char *offset =
4201+
"Output file data offset. Currently only supported on the SN340 device.";
4202+
const char *type =
4203+
"Telemetry type - NONE, HOST, or CONTROLLER Currently only supported on the SN530, SN640, SN730, SN740, SN810, SN840 and ZN350 devices.";
4204+
const char *verbose = "Display more debug messages.";
42014205
char f[PATH_MAX] = {0};
42024206
char fb[PATH_MAX/2] = {0};
42034207
char fileSuffix[PATH_MAX] = {0};
@@ -4527,7 +4531,7 @@ static int wdc_do_crash_dump(struct nvme_dev *dev, char *file, int type)
45274531
return ret;
45284532
}
45294533

4530-
static int wdc_crash_dump(struct nvme_dev *dev, char *file, int type)
4534+
static int wdc_crash_dump(struct nvme_dev *dev, const char *file, int type)
45314535
{
45324536
char f[PATH_MAX] = {0};
45334537
const char *dump_type;
@@ -4549,7 +4553,7 @@ static int wdc_crash_dump(struct nvme_dev *dev, char *file, int type)
45494553
return ret;
45504554
}
45514555

4552-
static int wdc_do_drive_log(struct nvme_dev *dev, char *file)
4556+
static int wdc_do_drive_log(struct nvme_dev *dev, const char *file)
45534557
{
45544558
int ret;
45554559
__u8 *drive_log_data;
@@ -4694,8 +4698,8 @@ static int wdc_get_crash_dump(int argc, char **argv, struct command *command,
46944698
static int wdc_get_pfail_dump(int argc, char **argv, struct command *command,
46954699
struct plugin *plugin)
46964700
{
4697-
char *desc = "Get Pfail Crash Dump.";
4698-
char *file = "Output file pathname.";
4701+
const char *desc = "Get Pfail Crash Dump.";
4702+
const char *file = "Output file pathname.";
46994703
__u64 capabilities = 0;
47004704
struct nvme_dev *dev;
47014705
struct config {
@@ -5133,7 +5137,7 @@ static void wdc_print_latency_monitor_log_json(struct wdc_ssd_latency_monitor_lo
51335137
{
51345138
int i, j;
51355139
char buf[128];
5136-
char *operation[3] = {"Read", "Write", "Trim"};
5140+
const char *operation[3] = {"Read", "Write", "Trim"};
51375141
struct json_object *root = json_create_object();
51385142

51395143
json_object_add_value_int(root, "Feature Status", log_data->feature_status);
@@ -5956,7 +5960,7 @@ static void wdc_print_fw_act_history_log_normal(__u8 *data, int num_entries,
59565960
__u16 oldestEntryIdx = 0, entryIdx = 0;
59575961
uint64_t timestamp;
59585962
__u64 timestamp_sec;
5959-
char *null_fw = "--------";
5963+
const char *null_fw = "--------";
59605964

59615965
memset((void *)time_str, '\0', 100);
59625966

@@ -8959,7 +8963,7 @@ static int wdc_do_clear_pcie_correctable_errors_fid(int fd)
89598963
static int wdc_clear_pcie_correctable_errors(int argc, char **argv, struct command *command,
89608964
struct plugin *plugin)
89618965
{
8962-
char *desc = "Clear PCIE Correctable Errors.";
8966+
const char *desc = "Clear PCIE Correctable Errors.";
89638967
__u64 capabilities = 0;
89648968
struct nvme_dev *dev;
89658969
nvme_root_t r;
@@ -9002,7 +9006,7 @@ static int wdc_clear_pcie_correctable_errors(int argc, char **argv, struct comma
90029006
static int wdc_drive_status(int argc, char **argv, struct command *command,
90039007
struct plugin *plugin)
90049008
{
9005-
char *desc = "Get Drive Status.";
9009+
const char *desc = "Get Drive Status.";
90069010
struct nvme_dev *dev;
90079011
int ret = 0;
90089012
bool uuid_found = false;
@@ -9145,7 +9149,7 @@ static int wdc_drive_status(int argc, char **argv, struct command *command,
91459149
static int wdc_clear_assert_dump(int argc, char **argv, struct command *command,
91469150
struct plugin *plugin)
91479151
{
9148-
char *desc = "Clear Assert Dump Present Status.";
9152+
const char *desc = "Clear Assert Dump Present Status.";
91499153
struct nvme_dev *dev;
91509154
int ret = -1;
91519155
nvme_root_t r;
@@ -9455,7 +9459,7 @@ static int wdc_do_clear_fw_activate_history_fid(int fd)
94559459
static int wdc_clear_fw_activate_history(int argc, char **argv, struct command *command,
94569460
struct plugin *plugin)
94579461
{
9458-
char *desc = "Clear FW activate history table.";
9462+
const char *desc = "Clear FW activate history table.";
94599463
__u64 capabilities = 0;
94609464
struct nvme_dev *dev;
94619465
nvme_root_t r;
@@ -9491,10 +9495,10 @@ static int wdc_clear_fw_activate_history(int argc, char **argv, struct command *
94919495
static int wdc_vs_telemetry_controller_option(int argc, char **argv, struct command *command,
94929496
struct plugin *plugin)
94939497
{
9494-
char *desc = "Disable/Enable Controller Option of the Telemetry Log Page.";
9495-
char *disable = "Disable controller option of the telemetry log page.";
9496-
char *enable = "Enable controller option of the telemetry log page.";
9497-
char *status = "Displays the current state of the controller initiated log page.";
9498+
const char *desc = "Disable/Enable Controller Option of the Telemetry Log Page.";
9499+
const char *disable = "Disable controller option of the telemetry log page.";
9500+
const char *enable = "Enable controller option of the telemetry log page.";
9501+
const char *status = "Displays the current state of the controller initiated log page.";
94989502
__u64 capabilities = 0;
94999503
struct nvme_dev *dev;
95009504
nvme_root_t r;
@@ -9883,7 +9887,8 @@ static int wdc_fetch_log_file_from_device(struct nvme_dev *dev, __u32 fileId,
98839887
return ret;
98849888
}
98859889

9886-
static int wdc_de_get_dump_trace(struct nvme_dev *dev, char *filePath, __u16 binFileNameLen, char *binFileName)
9890+
static int wdc_de_get_dump_trace(struct nvme_dev *dev, const char *filePath, __u16 binFileNameLen,
9891+
const char *binFileName)
98879892
{
98889893
int ret = WDC_STATUS_FAILURE;
98899894
__u8 *readBuffer = NULL;
@@ -10292,8 +10297,8 @@ static int wdc_do_drive_essentials(nvme_root_t r, struct nvme_dev *dev,
1029210297
static int wdc_drive_essentials(int argc, char **argv, struct command *command,
1029310298
struct plugin *plugin)
1029410299
{
10295-
char *desc = "Capture Drive Essentials.";
10296-
char *dirName = "Output directory pathname.";
10300+
const char *desc = "Capture Drive Essentials.";
10301+
const char *dirName = "Output directory pathname.";
1029710302
char d[PATH_MAX] = {0};
1029810303
char k[PATH_MAX] = {0};
1029910304
__u64 capabilities = 0;
@@ -10307,7 +10312,7 @@ static int wdc_drive_essentials(int argc, char **argv, struct command *command,
1030710312
};
1030810313

1030910314
struct config cfg = {
10310-
.dirName = NULL,
10315+
.dirName = NULL,
1031110316
};
1031210317

1031310318
OPT_ARGS(opts) = {
@@ -10936,7 +10941,7 @@ static int wdc_get_drive_reason_id(struct nvme_dev *dev, char *drive_reason_id,
1093610941
int ret;
1093710942
int res_len = 0;
1093810943
struct nvme_id_ctrl ctrl;
10939-
char *reason_id_str = "reason_id";
10944+
const char *reason_id_str = "reason_id";
1094010945

1094110946
i = sizeof(ctrl.sn) - 1;
1094210947
j = sizeof(ctrl.mn) - 1;
@@ -11056,7 +11061,7 @@ static int wdc_dump_telemetry_hdr(struct nvme_dev *dev, int log_id, struct nvme_
1105611061
return ret;
1105711062
}
1105811063

11059-
static int wdc_do_get_reason_id(struct nvme_dev *dev, char *file, int log_id)
11064+
static int wdc_do_get_reason_id(struct nvme_dev *dev, const char *file, int log_id)
1106011065
{
1106111066
int ret;
1106211067
struct nvme_telemetry_log *log_hdr;
@@ -12260,10 +12265,10 @@ static int wdc_cloud_boot_SSD_version(int argc, char **argv, struct command *com
1226012265

1226112266
static int wdc_enc_get_log(int argc, char **argv, struct command *command, struct plugin *plugin)
1226212267
{
12263-
char *desc = "Get Enclosure Log.";
12264-
char *file = "Output file pathname.";
12265-
char *size = "Data retrieval transfer size.";
12266-
char *log = "Enclosure Log Page ID.";
12268+
const char *desc = "Get Enclosure Log.";
12269+
const char *file = "Output file pathname.";
12270+
const char *size = "Data retrieval transfer size.";
12271+
const char *log = "Enclosure Log Page ID.";
1226712272
struct nvme_dev *dev;
1226812273
FILE *output_fd;
1226912274
int xfer_size = 0;

plugins/wdc/wdc-utils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int wdc_UtilsGetTime(PUtilsTimeInfo timeInfo)
9797
return WDC_STATUS_SUCCESS;
9898
}
9999

100-
int wdc_UtilsCreateDir(char *path)
100+
int wdc_UtilsCreateDir(const char *path)
101101
{
102102
int retStatus;
103103
int status = WDC_STATUS_SUCCESS;
@@ -118,7 +118,7 @@ int wdc_UtilsCreateDir(char *path)
118118
return status;
119119
}
120120

121-
int wdc_WriteToFile(char *fileName, char *buffer, unsigned int bufferLen)
121+
int wdc_WriteToFile(const char *fileName, const char *buffer, unsigned int bufferLen)
122122
{
123123
int status = WDC_STATUS_SUCCESS;
124124
FILE *file;
@@ -150,7 +150,7 @@ int wdc_WriteToFile(char *fileName, char *buffer, unsigned int bufferLen)
150150
* 1 if the pcSrc string is lexically higher than pcDst or
151151
* -1 if the pcSrc string is lexically lower than pcDst.
152152
*/
153-
int wdc_UtilsStrCompare(char *pcSrc, char *pcDst)
153+
int wdc_UtilsStrCompare(const char *pcSrc, const char *pcDst)
154154
{
155155
while ((toupper(*pcSrc) == toupper(*pcDst)) && (*pcSrc != '\0')) {
156156
pcSrc++;

plugins/wdc/wdc-utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ 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,
8181
struct nvme_id_uuid_list *uuid_list);

0 commit comments

Comments
 (0)