Refactoring: optimize pointer checks

Remove all direct comparisons to 'NULL' for pointers.
Replace them with boolean-like 'if (ptr)' and 'if (!ptr)'.
This makes conditions shorter and easier to read.

Signed-off-by: Alexander Amelkin <alexander@amelkin.msk.ru>
This commit is contained in:
Alexander Amelkin
2018-08-21 17:16:19 +03:00
parent f3ef88724f
commit e9716e216d
48 changed files with 1050 additions and 1090 deletions

View File

@@ -597,16 +597,16 @@ print_strs(const struct dcmi_cmd * vs, const char * title, int loglevel,
{
int i;
if (vs == NULL)
if (!vs)
return;
if (title != NULL) {
if (title) {
if (loglevel < 0)
printf("\n%s\n", title);
else
lprintf(loglevel, "\n%s", title);
}
for (i = 0; vs[i].str != NULL; i++) {
for (i = 0; vs[i].str; i++) {
if (loglevel < 0) {
if (vs[i].val < 256)
if (verthorz == 0)
@@ -626,7 +626,7 @@ print_strs(const struct dcmi_cmd * vs, const char * title, int loglevel,
/* Check to see if this is NOT the last element in vs.str if true
* print the | else don't print anything.
*/
if ((verthorz == 1) && (vs[i+1].str != NULL))
if (verthorz == 1 && vs[i+1].str)
printf(" | ");
}
if (verthorz == 0) {
@@ -650,10 +650,10 @@ uint16_t
str2val2(const char *str, const struct dcmi_cmd *vs)
{
int i;
if (vs == NULL || str == NULL) {
if (!vs || !str) {
return 0;
}
for (i = 0; vs[i].str != NULL; i++) {
for (i = 0; vs[i].str; i++) {
if (strncasecmp(vs[i].str, str, __maxlen(str, vs[i].str)) == 0)
return vs[i].val;
}
@@ -674,10 +674,10 @@ val2str2(uint16_t val, const struct dcmi_cmd *vs)
static char un_str[32];
int i;
if (vs == NULL)
if (!vs)
return NULL;
for (i = 0; vs[i].str != NULL; i++) {
for (i = 0; vs[i].str; i++) {
if (vs[i].val == val)
return vs[i].str;
}
@@ -695,7 +695,7 @@ chk_rsp(struct ipmi_rs * rsp)
/* if the response from the intf is NULL then the BMC is experiencing
* some issue and cannot complete the command
*/
if (rsp == NULL) {
if (!rsp) {
lprintf(LOG_ERR, "\n Unable to get DCMI information");
return 1;
}
@@ -730,7 +730,7 @@ chk_nm_rsp(struct ipmi_rs * rsp)
/* if the response from the intf is NULL then the BMC is experiencing
* some issue and cannot complete the command
*/
if (rsp == NULL) {
if (!rsp) {
lprintf(LOG_ERR, "\n No response to NM request");
return 1;
}
@@ -812,11 +812,11 @@ ipmi_dcmi_prnt_oobDiscover(struct ipmi_intf * intf)
# else
struct ipmi_session_params *p;
if (intf->opened == 0 && intf->open != NULL) {
if (intf->opened == 0 && intf->open) {
if (intf->open(intf) < 0)
return (-1);
}
if (intf == NULL || intf->session == NULL)
if (!intf || !intf->session)
return -1;
p = &intf->ssn_params;
@@ -830,7 +830,7 @@ ipmi_dcmi_prnt_oobDiscover(struct ipmi_intf * intf)
if (p->retry == 0)
p->retry = IPMI_LAN_RETRY;
if (p->hostname == NULL || strlen((const char *)p->hostname) == 0) {
if (!p->hostname || strlen((const char *)p->hostname) == 0) {
lprintf(LOG_ERR, "No hostname specified!");
return -1;
}
@@ -1003,7 +1003,7 @@ ipmi_dcmi_prnt_getcapabilities(struct ipmi_intf * intf, uint8_t selector)
printf(" DCMI Specification %d.%d\n", reply[1], reply[2]);
printf(" Rolling average time period options: %d\n", reply[4]);
printf(" Sample time options: ");
for (j = 1; dcmi_sampling_vals[j-1].str != NULL; j++)
for (j = 1; dcmi_sampling_vals[j-1].str; j++)
printf(" %s ", val2str2(reply[4+j],dcmi_sampling_vals));
printf("\n");
break;
@@ -1075,7 +1075,7 @@ ipmi_dcmi_prnt_getassettag(struct ipmi_intf * intf)
/* macro has no effect here where can generate sig segv
* if rsp occurs with null
*/
if (rsp != NULL) {
if (rsp) {
GOOD_ASSET_TAG_CCODE(rsp->ccode);
}
if (chk_rsp(rsp)) {
@@ -1591,7 +1591,7 @@ ipmi_dcmi_prnt_get_temp_readings(struct ipmi_intf * intf)
int i,j, tota_inst, get_inst, offset = 0;
/* Print sensor description */
printf("\n\tEntity ID\t\t\tEntity Instance\t Temp. Readings");
for (i = 0; dcmi_temp_read_vals[i].str != NULL; i++) {
for (i = 0; dcmi_temp_read_vals[i].str; i++) {
/* get all of the information about this sensor */
rsp = ipmi_dcmi_get_temp_readings(intf,
dcmi_temp_read_vals[i].val,
@@ -1794,7 +1794,7 @@ ipmi_dcmi_pwr_prnt_glimit(struct ipmi_intf * intf)
/* rsp can be a null so check response before any operation
* on it to avoid sig segv
*/
if (rsp != NULL) {
if (rsp) {
realCc = rsp->ccode;
GOOD_PWR_GLIMIT_CCODE(rsp->ccode);
}
@@ -1842,7 +1842,7 @@ ipmi_dcmi_pwr_slimit(struct ipmi_intf * intf, const char * option,
/* rsp can be a null so check response before any operation on it to
* avoid sig segv
*/
if (rsp != NULL) {
if (rsp) {
GOOD_PWR_GLIMIT_CCODE(rsp->ccode);
}
if (chk_rsp(rsp)) {
@@ -2512,7 +2512,7 @@ ipmi_nm_getcapabilities(struct ipmi_intf * intf, int argc, char **argv)
while (--argc > 0) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
if ((option = str2val2(argv[0], nm_capability_opts)) == 0xFF) {
print_strs(nm_capability_opts, "Capability commands", LOG_ERR, 0);
return -1;
@@ -2594,7 +2594,7 @@ ipmi_nm_get_policy(struct ipmi_intf * intf, int argc, char **argv)
while (--argc) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
if ((option = str2val2(argv[0], nm_policy_options)) == 0xFF) {
print_strs(nm_policy_options, "Get Policy commands", LOG_ERR, 0);
return -1;
@@ -2688,8 +2688,9 @@ ipmi_nm_policy(struct ipmi_intf * intf, int argc, char **argv)
argv++;
argc--;
if ((argv[0] == NULL) ||
((action = str2val2(argv[0], nm_policy_action)) == 0xFF)) {
if (!argv[0] ||
0xFF == (action = str2val2(argv[0], nm_policy_action)))
{
print_strs(nm_policy_action, "Policy commands", LOG_ERR, 0);
return -1;
}
@@ -2705,7 +2706,7 @@ ipmi_nm_policy(struct ipmi_intf * intf, int argc, char **argv)
*/
while (--argc > 0) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
if ((option = str2val2(argv[0], nm_policy_options)) == 0xFF) {
print_strs(nm_policy_options, "Policy options", LOG_ERR, 0);
return -1;
@@ -2823,8 +2824,8 @@ ipmi_nm_control(struct ipmi_intf * intf, int argc, char **argv)
argv++;
argc--;
/* nm_ctl_cmds returns 0 for disable, 1 for enable */
if ((argv[0] == NULL) ||
((action = str2val2(argv[0], nm_ctl_cmds)) == 0xFF))
if (!argv[0] ||
0xFF == (action = str2val2(argv[0], nm_ctl_cmds)))
{
print_strs(nm_ctl_cmds, "Control parameters:", LOG_ERR, 0);
print_strs(nm_ctl_domain, "control Scope (required):", LOG_ERR, 0);
@@ -2833,14 +2834,14 @@ ipmi_nm_control(struct ipmi_intf * intf, int argc, char **argv)
argv++;
while (--argc) {
/* nm_ctl_domain returns correct bit field except for action */
if ((argv[0] == NULL) ||
((scope = str2val2(argv[0], nm_ctl_domain)) == 0xFF))
if (!argv[0] ||
0xFF == (scope = str2val2(argv[0], nm_ctl_domain)))
{
print_strs(nm_ctl_domain, "Control Scope (required):", LOG_ERR, 0);
return -1;
}
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
if (scope == 0x02) { /* domain */
if ((domain = str2val2(argv[0], nm_domain_vals)) == 0xFF) {
print_strs(nm_domain_vals, "Domain Scope:", LOG_ERR, 0);
@@ -2883,15 +2884,15 @@ ipmi_nm_get_statistics(struct ipmi_intf * intf, int argc, char **argv)
time_t t;
argv++;
if ((argv[0] == NULL) ||
((mode = str2val2(argv[0], nm_stats_mode)) == 0xFF))
if (!argv[0] ||
0xFF == (mode = str2val2(argv[0], nm_stats_mode)))
{
print_strs(nm_stats_mode, "Statistics commands", LOG_ERR, 0);
return -1;
}
while (--argc) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
if ((option = str2val2(argv[0], nm_stats_opts)) == 0xFF) {
print_strs(nm_stats_opts, "Control Scope options", LOG_ERR, 0);
return -1;
@@ -3008,15 +3009,15 @@ ipmi_nm_reset_statistics(struct ipmi_intf * intf, int argc, char **argv)
uint8_t have_policy_id = FALSE;
argv++;
if ((argv[0] == NULL) ||
((mode = str2val2(argv[0], nm_reset_mode)) == 0xFF))
if (!argv[0] ||
0xFF == (mode = str2val2(argv[0], nm_reset_mode)))
{
print_strs(nm_reset_mode, "Reset Statistics Modes:", LOG_ERR, 0);
return -1;
}
while (--argc) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
if ((option = str2val2(argv[0], nm_stats_opts)) == 0xFF) {
print_strs(nm_stats_opts, "Reset Scope options", LOG_ERR, 0);
return -1;
@@ -3060,7 +3061,7 @@ ipmi_nm_set_range(struct ipmi_intf * intf, int argc, char **argv)
while (--argc) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
if ((param = str2val2(argv[0], nm_power_range)) == 0xFF) {
print_strs(nm_power_range, "power range parameters:", LOG_ERR, 0);
return -1;
@@ -3144,8 +3145,8 @@ ipmi_nm_alert(struct ipmi_intf * intf, int argc, char **argv)
argv++;
argc--;
if ((argv[0] == NULL) ||
((action = str2val2(argv[0], nm_alert_opts)) == 0xFF))
if (!argv[0] ||
0xFF == (action = str2val2(argv[0], nm_alert_opts)))
{
print_strs(nm_alert_opts, "Alert commands", LOG_ERR, 0);
return -1;
@@ -3156,7 +3157,7 @@ ipmi_nm_alert(struct ipmi_intf * intf, int argc, char **argv)
memset(&alert, 0, sizeof(alert));
while (--argc) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
if ((param = str2val2(argv[0], nm_set_alert_param)) == 0xFF) {
print_strs(nm_set_alert_param, "Set alert Parameters:", LOG_ERR, 0);
return -1;
@@ -3236,8 +3237,8 @@ ipmi_nm_thresh(struct ipmi_intf * intf, int argc, char **argv)
argv++;
argc--;
/* set or get */
if ((argv[0] == NULL) || (argc < 3) ||
((action = str2val2(argv[0], nm_thresh_cmds)) == 0xFF))
if (!argv[0] || argc < 3
|| 0xFF == (action = str2val2(argv[0], nm_thresh_cmds)))
{
print_strs(nm_thresh_cmds, "Threshold commands", LOG_ERR, 0);
return -1;
@@ -3245,7 +3246,7 @@ ipmi_nm_thresh(struct ipmi_intf * intf, int argc, char **argv)
memset(&thresh, 0, sizeof(thresh));
while (--argc) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
option = str2val2(argv[0], nm_thresh_param);
switch (option) {
case 0x01: /* get domain scope */
@@ -3356,8 +3357,8 @@ ipmi_nm_suspend(struct ipmi_intf * intf, int argc, char **argv)
argv++;
argc--;
/* set or get */
if ((argv[0] == NULL) || (argc < 3) ||
((action = str2val2(argv[0], nm_suspend_cmds)) == 0xFF))
if (!argv[0] || argc < 3 ||
0xFF == (action = str2val2(argv[0], nm_suspend_cmds)))
{
print_strs(nm_suspend_cmds, "Suspend commands", LOG_ERR, 0);
return -1;
@@ -3365,7 +3366,7 @@ ipmi_nm_suspend(struct ipmi_intf * intf, int argc, char **argv)
memset(&suspend, 0, sizeof(suspend));
while (--argc > 0) {
argv++;
if (argv[0] == NULL) break;
if (!argv[0]) break;
option = str2val2(argv[0], nm_thresh_param);
switch (option) {
case 0x01: /* get domain scope */
@@ -3517,7 +3518,7 @@ ipmi_dcmi_set_limit(struct ipmi_intf * intf, int argc, char **argv)
}
} else {
/* loop through each parameter and value until we have neither */
while ((argv[1] != NULL) && (argv[2] != NULL)) {
while (argv[1] && argv[2]) {
rc = ipmi_dcmi_pwr_slimit(intf, argv[1], argv[2]);
/* catch any error that the set limit function returned */
if (rc > 0) {
@@ -3543,7 +3544,7 @@ ipmi_dcmi_parse_power(struct ipmi_intf * intf, int argc, char **argv)
switch (str2val2(argv[0], dcmi_pwrmgmt_vals)) {
case 0x00:
/* get reading */
if (argv[1] != NULL) {
if (argv[1]) {
if (!(sample_time = str2val2(argv[1], dcmi_sampling_vals))) {
print_strs(dcmi_sampling_vals,
"Invalid sample time. Valid times are: ",
@@ -3713,7 +3714,7 @@ ipmi_dcmi_main(struct ipmi_intf * intf, int argc, char **argv)
switch (str2val2(argv[0], dcmi_cmd_vals)) {
case 0x00:
/* discover capabilities*/
for (i = 1; dcmi_capable_vals[i-1].str != NULL; i++) {
for (i = 1; dcmi_capable_vals[i-1].str; i++) {
if (ipmi_dcmi_prnt_getcapabilities(intf, i) < 0) {
lprintf(LOG_ERR,"Error discovering %s capabilities!\n",
val2str2(i, dcmi_capable_vals));
@@ -3724,7 +3725,7 @@ ipmi_dcmi_main(struct ipmi_intf * intf, int argc, char **argv)
case 0x01:
/* power */
argv++;
if (argv[0] == NULL) {
if (!argv[0]) {
print_strs(dcmi_pwrmgmt_vals, "power <command>",
LOG_ERR, 0);
return -1;
@@ -3738,7 +3739,7 @@ ipmi_dcmi_main(struct ipmi_intf * intf, int argc, char **argv)
* and if it exists, print the sdr record id(s) for it.
* Use the val from each one as the sensor number.
*/
for (i = 0; dcmi_discvry_snsr_vals[i].str != NULL; i++) {
for (i = 0; dcmi_discvry_snsr_vals[i].str; i++) {
/* get all of the information about this sensor */
rc = ipmi_dcmi_prnt_discvry_snsr(intf,
dcmi_discvry_snsr_vals[i].val);
@@ -3852,7 +3853,7 @@ ipmi_dcmi_main(struct ipmi_intf * intf, int argc, char **argv)
}
case 0x0B:
{
if (intf->session == NULL) {
if (!intf->session) {
lprintf(LOG_ERR,
"\nOOB discovery is available only via RMCP interface.");
return -1;
@@ -3971,24 +3972,24 @@ ipmi_print_sensor_info(struct ipmi_intf *intf, uint16_t rec_id)
uint8_t *rec = NULL;
itr = ipmi_sdr_start(intf, 0);
if (itr == NULL) {
if (!itr) {
lprintf(LOG_ERR, "Unable to open SDR for reading");
return (-1);
}
while ((header = ipmi_sdr_get_next_header(intf, itr)) != NULL) {
while ((header = ipmi_sdr_get_next_header(intf, itr))) {
if (header->id == rec_id) {
break;
}
}
if (header == NULL) {
if (!header) {
lprintf(LOG_DEBUG, "header == NULL");
ipmi_sdr_end(intf, itr);
return (-1);
}
/* yes, we found the SDR for this record ID, now get full record */
rec = ipmi_sdr_get_record(intf, header, itr);
if (rec == NULL) {
if (!rec) {
lprintf(LOG_DEBUG, "rec == NULL");
ipmi_sdr_end(intf, itr);
return (-1);