Skip to content

Commit df4b2b7

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 e0fecfc commit df4b2b7

3 files changed

Lines changed: 53 additions & 53 deletions

File tree

plugins/wdc/wdc-nvme.c

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -922,15 +922,15 @@ 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, __u32 drive_log_length);
926926
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,
927+
static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len, __u32 cdw12, const char *file,
928928
__u32 xfer_size);
929929
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);
930+
static int wdc_crash_dump(struct nvme_dev *dev, const char *file, int type);
931931
static int wdc_get_crash_dump(int argc, char **argv, struct command *command,
932932
struct plugin *plugin);
933-
static int wdc_do_drive_log(struct nvme_dev *dev, char *file);
933+
static int wdc_do_drive_log(struct nvme_dev *dev, const char *file);
934934
static int wdc_drive_log(int argc, char **argv, struct command *command, struct plugin *plugin);
935935
static const char *wdc_purge_mon_status_to_string(__u32 status);
936936
static int wdc_purge(int argc, char **argv, struct command *command, struct plugin *plugin);
@@ -951,7 +951,7 @@ static int wdc_namespace_resize(int argc, char **argv, struct command *command,
951951
static int wdc_do_namespace_resize(struct nvme_dev *dev, __u32 nsid, __u32 op_option);
952952
static int wdc_reason_identifier(int argc, char **argv, struct command *command,
953953
struct plugin *plugin);
954-
static int wdc_do_get_reason_id(struct nvme_dev *dev, char *file, int log_id);
954+
static int wdc_do_get_reason_id(struct nvme_dev *dev, const char *file, int log_id);
955955
static int wdc_save_reason_id(struct nvme_dev *dev, __u8 *rsn_ident, int size);
956956
static int wdc_clear_reason_id(struct nvme_dev *dev);
957957
static int wdc_log_page_directory(int argc, char **argv, struct command *command,
@@ -2135,8 +2135,8 @@ static int wdc_get_serial_name(struct nvme_dev *dev, char *file, size_t len,
21352135
return 0;
21362136
}
21372137

2138-
static int wdc_create_log_file(char *file, __u8 *drive_log_data,
2139-
__u32 drive_log_length)
2138+
static int wdc_create_log_file(const char *file, const __u8 *drive_log_data,
2139+
__u32 drive_log_length)
21402140
{
21412141
int fd;
21422142
int ret;
@@ -2948,7 +2948,7 @@ static __u32 wdc_dump_dui_data_v2(int fd, __u32 dataLen, __u64 offset, __u8 *dum
29482948
}
29492949

29502950
static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len,
2951-
__u32 cdw12, char *file, __u32 xfer_size)
2951+
__u32 cdw12, const char *file, __u32 xfer_size)
29522952
{
29532953
int ret = 0;
29542954
__u8 *dump_data;
@@ -3007,7 +3007,7 @@ static int wdc_do_dump(struct nvme_dev *dev, __u32 opcode, __u32 data_len,
30073007
}
30083008

30093009
static int wdc_do_dump_e6(int fd, __u32 opcode, __u32 data_len,
3010-
__u32 cdw12, char *file, __u32 xfer_size, __u8 *log_hdr)
3010+
__u32 cdw12, char *file, __u32 xfer_size, __u8 *log_hdr)
30113011
{
30123012
int ret = 0;
30133013
__u8 *dump_data;
@@ -3081,8 +3081,8 @@ static int wdc_do_dump_e6(int fd, __u32 opcode, __u32 data_len,
30813081
return ret;
30823082
}
30833083

3084-
static int wdc_do_cap_telemetry_log(struct nvme_dev *dev, char *file,
3085-
__u32 bs, int type, int data_area)
3084+
static int wdc_do_cap_telemetry_log(struct nvme_dev *dev, const char *file,
3085+
__u32 bs, int type, int data_area)
30863086
{
30873087
struct nvme_telemetry_log *log;
30883088
size_t full_size = 0;
@@ -3724,9 +3724,9 @@ static int wdc_cap_diag(int argc, char **argv, struct command *command,
37243724
struct plugin *plugin)
37253725
{
37263726
nvme_root_t r;
3727-
char *desc = "Capture Diagnostics Log.";
3728-
char *file = "Output file pathname.";
3729-
char *size = "Data retrieval transfer size.";
3727+
const char *desc = "Capture Diagnostics Log.";
3728+
const char *file = "Output file pathname.";
3729+
const char *size = "Data retrieval transfer size.";
37303730
__u64 capabilities = 0;
37313731
char f[PATH_MAX] = {0};
37323732
struct nvme_dev *dev;
@@ -4017,7 +4017,7 @@ static int wdc_do_sn730_get_and_tar(int fd, char *outputName)
40174017
return ret;
40184018
}
40194019

4020-
static int dump_internal_logs(struct nvme_dev *dev, char *dir_name, int verbose)
4020+
static int dump_internal_logs(struct nvme_dev *dev, const char *dir_name, int verbose)
40214021
{
40224022
char file_path[PATH_MAX];
40234023
void *telemetry_log;
@@ -4116,14 +4116,14 @@ static int dump_internal_logs(struct nvme_dev *dev, char *dir_name, int verbose)
41164116
static int wdc_vs_internal_fw_log(int argc, char **argv, struct command *command,
41174117
struct plugin *plugin)
41184118
{
4119-
char *desc = "Internal Firmware Log.";
4120-
char *file = "Output file pathname.";
4121-
char *size = "Data retrieval transfer size.";
4122-
char *data_area = "Data area to retrieve up to. Currently only supported on the SN340, SN640, SN730, and SN840 devices.";
4123-
char *file_size = "Output file size. Currently only supported on the SN340 device.";
4124-
char *offset = "Output file data offset. Currently only supported on the SN340 device.";
4125-
char *type = "Telemetry type - NONE, HOST, or CONTROLLER. Currently only supported on the SN530, SN640, SN730, SN740, SN810, SN840 and ZN350 devices.";
4126-
char *verbose = "Display more debug messages.";
4119+
const char *desc = "Internal Firmware Log.";
4120+
const char *file = "Output file pathname.";
4121+
const char *size = "Data retrieval transfer size.";
4122+
const char *data_area = "Data area to retrieve up to. Currently only supported on the SN340, SN640, SN730, and SN840 devices.";
4123+
const char *file_size = "Output file size. Currently only supported on the SN340 device.";
4124+
const char *offset = "Output file data offset. Currently only supported on the SN340 device.";
4125+
const char *type = "Telemetry type - NONE, HOST, or CONTROLLER. Currently only supported on the SN530, SN640, SN730, SN740, SN810, SN840 and ZN350 devices.";
4126+
const char *verbose = "Display more debug messages.";
41274127
char f[PATH_MAX] = {0};
41284128
char fb[PATH_MAX/2] = {0};
41294129
char fileSuffix[PATH_MAX] = {0};
@@ -4453,7 +4453,7 @@ static int wdc_do_crash_dump(struct nvme_dev *dev, char *file, int type)
44534453
return ret;
44544454
}
44554455

4456-
static int wdc_crash_dump(struct nvme_dev *dev, char *file, int type)
4456+
static int wdc_crash_dump(struct nvme_dev *dev, const char *file, int type)
44574457
{
44584458
char f[PATH_MAX] = {0};
44594459
const char *dump_type;
@@ -4475,7 +4475,7 @@ static int wdc_crash_dump(struct nvme_dev *dev, char *file, int type)
44754475
return ret;
44764476
}
44774477

4478-
static int wdc_do_drive_log(struct nvme_dev *dev, char *file)
4478+
static int wdc_do_drive_log(struct nvme_dev *dev, const char *file)
44794479
{
44804480
int ret;
44814481
__u8 *drive_log_data;
@@ -4620,8 +4620,8 @@ static int wdc_get_crash_dump(int argc, char **argv, struct command *command,
46204620
static int wdc_get_pfail_dump(int argc, char **argv, struct command *command,
46214621
struct plugin *plugin)
46224622
{
4623-
char *desc = "Get Pfail Crash Dump.";
4624-
char *file = "Output file pathname.";
4623+
const char *desc = "Get Pfail Crash Dump.";
4624+
const char *file = "Output file pathname.";
46254625
__u64 capabilities = 0;
46264626
struct nvme_dev *dev;
46274627
struct config {
@@ -5059,7 +5059,7 @@ static void wdc_print_latency_monitor_log_json(struct wdc_ssd_latency_monitor_lo
50595059
{
50605060
int i, j;
50615061
char buf[128];
5062-
char *operation[3] = {"Read", "Write", "Trim"};
5062+
const char *operation[3] = {"Read", "Write", "Trim"};
50635063
struct json_object *root = json_create_object();
50645064

50655065
json_object_add_value_int(root, "Feature Status", log_data->feature_status);
@@ -5882,7 +5882,7 @@ static void wdc_print_fw_act_history_log_normal(__u8 *data, int num_entries,
58825882
__u16 oldestEntryIdx = 0, entryIdx = 0;
58835883
uint64_t timestamp;
58845884
__u64 timestamp_sec;
5885-
char *null_fw = "--------";
5885+
const char *null_fw = "--------";
58865886

58875887
memset((void *)time_str, '\0', 100);
58885888

@@ -8881,7 +8881,7 @@ static int wdc_do_clear_pcie_correctable_errors_fid(int fd)
88818881
static int wdc_clear_pcie_correctable_errors(int argc, char **argv, struct command *command,
88828882
struct plugin *plugin)
88838883
{
8884-
char *desc = "Clear PCIE Correctable Errors.";
8884+
const char *desc = "Clear PCIE Correctable Errors.";
88858885
__u64 capabilities = 0;
88868886
struct nvme_dev *dev;
88878887
nvme_root_t r;
@@ -8924,7 +8924,7 @@ static int wdc_clear_pcie_correctable_errors(int argc, char **argv, struct comma
89248924
static int wdc_drive_status(int argc, char **argv, struct command *command,
89258925
struct plugin *plugin)
89268926
{
8927-
char *desc = "Get Drive Status.";
8927+
const char *desc = "Get Drive Status.";
89288928
struct nvme_dev *dev;
89298929
int ret = 0;
89308930
nvme_root_t r;
@@ -9052,7 +9052,7 @@ static int wdc_drive_status(int argc, char **argv, struct command *command,
90529052
static int wdc_clear_assert_dump(int argc, char **argv, struct command *command,
90539053
struct plugin *plugin)
90549054
{
9055-
char *desc = "Clear Assert Dump Present Status.";
9055+
const char *desc = "Clear Assert Dump Present Status.";
90569056
struct nvme_dev *dev;
90579057
int ret = -1;
90589058
nvme_root_t r;
@@ -9361,7 +9361,7 @@ static int wdc_do_clear_fw_activate_history_fid(int fd)
93619361
static int wdc_clear_fw_activate_history(int argc, char **argv, struct command *command,
93629362
struct plugin *plugin)
93639363
{
9364-
char *desc = "Clear FW activate history table.";
9364+
const char *desc = "Clear FW activate history table.";
93659365
__u64 capabilities = 0;
93669366
struct nvme_dev *dev;
93679367
nvme_root_t r;
@@ -9397,10 +9397,10 @@ static int wdc_clear_fw_activate_history(int argc, char **argv, struct command *
93979397
static int wdc_vs_telemetry_controller_option(int argc, char **argv, struct command *command,
93989398
struct plugin *plugin)
93999399
{
9400-
char *desc = "Disable/Enable Controller Option of the Telemetry Log Page.";
9401-
char *disable = "Disable controller option of the telemetry log page.";
9402-
char *enable = "Enable controller option of the telemetry log page.";
9403-
char *status = "Displays the current state of the controller initiated log page.";
9400+
const char *desc = "Disable/Enable Controller Option of the Telemetry Log Page.";
9401+
const char *disable = "Disable controller option of the telemetry log page.";
9402+
const char *enable = "Enable controller option of the telemetry log page.";
9403+
const char *status = "Displays the current state of the controller initiated log page.";
94049404
__u64 capabilities = 0;
94059405
struct nvme_dev *dev;
94069406
nvme_root_t r;
@@ -9789,7 +9789,7 @@ static int wdc_fetch_log_file_from_device(struct nvme_dev *dev, __u32 fileId,
97899789
return ret;
97909790
}
97919791

9792-
static int wdc_de_get_dump_trace(struct nvme_dev *dev, char *filePath, __u16 binFileNameLen, char *binFileName)
9792+
static int wdc_de_get_dump_trace(struct nvme_dev *dev, const char *filePath, __u16 binFileNameLen, const char *binFileName)
97939793
{
97949794
int ret = WDC_STATUS_FAILURE;
97959795
__u8 *readBuffer = NULL;
@@ -10198,8 +10198,8 @@ static int wdc_do_drive_essentials(nvme_root_t r, struct nvme_dev *dev,
1019810198
static int wdc_drive_essentials(int argc, char **argv, struct command *command,
1019910199
struct plugin *plugin)
1020010200
{
10201-
char *desc = "Capture Drive Essentials.";
10202-
char *dirName = "Output directory pathname.";
10201+
const char *desc = "Capture Drive Essentials.";
10202+
const char *dirName = "Output directory pathname.";
1020310203
char d[PATH_MAX] = {0};
1020410204
char k[PATH_MAX] = {0};
1020510205
__u64 capabilities = 0;
@@ -10213,7 +10213,7 @@ static int wdc_drive_essentials(int argc, char **argv, struct command *command,
1021310213
};
1021410214

1021510215
struct config cfg = {
10216-
.dirName = NULL,
10216+
.dirName = NULL,
1021710217
};
1021810218

1021910219
OPT_ARGS(opts) = {
@@ -10831,7 +10831,7 @@ static int wdc_get_drive_reason_id(struct nvme_dev *dev, char *drive_reason_id,
1083110831
int ret;
1083210832
int res_len = 0;
1083310833
struct nvme_id_ctrl ctrl;
10834-
char *reason_id_str = "reason_id";
10834+
const char *reason_id_str = "reason_id";
1083510835

1083610836
i = sizeof(ctrl.sn) - 1;
1083710837
j = sizeof(ctrl.mn) - 1;
@@ -10951,7 +10951,7 @@ static int wdc_dump_telemetry_hdr(struct nvme_dev *dev, int log_id, struct nvme_
1095110951
return ret;
1095210952
}
1095310953

10954-
static int wdc_do_get_reason_id(struct nvme_dev *dev, char *file, int log_id)
10954+
static int wdc_do_get_reason_id(struct nvme_dev *dev, const char *file, int log_id)
1095510955
{
1095610956
int ret;
1095710957
struct nvme_telemetry_log *log_hdr;
@@ -12155,10 +12155,10 @@ static int wdc_cloud_boot_SSD_version(int argc, char **argv, struct command *com
1215512155

1215612156
static int wdc_enc_get_log(int argc, char **argv, struct command *command, struct plugin *plugin)
1215712157
{
12158-
char *desc = "Get Enclosure Log.";
12159-
char *file = "Output file pathname.";
12160-
char *size = "Data retrieval transfer size.";
12161-
char *log = "Enclosure Log Page ID.";
12158+
const char *desc = "Get Enclosure Log.";
12159+
const char *file = "Output file pathname.";
12160+
const char *size = "Data retrieval transfer size.";
12161+
const char *log = "Enclosure Log Page ID.";
1216212162
struct nvme_dev *dev;
1216312163
FILE *output_fd;
1216412164
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)