From e44a22e2226a059bb46b06aae47a0f6d1c1284e9 Mon Sep 17 00:00:00 2001 From: Artur Paszkiewicz Date: Tue, 7 May 2019 16:13:00 +0200 Subject: [PATCH] fix segfault when a value is missing from ibpi_str[] Replace all usages of ibpi_str[] with ibpi2str(), which always returns a valid string. Signed-off-by: Artur Paszkiewicz --- src/ibpi.h | 3 ++- src/ledctl.c | 4 ++-- src/ledmon.c | 10 +++++----- src/scsi.c | 2 +- src/smp.c | 8 ++++---- src/sysfs.c | 2 +- src/utils.c | 18 ++++++++++++++++++ src/utils.h | 4 ++++ 9 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/ibpi.h b/src/ibpi.h index 776fba3..109f112 100644 --- a/src/ibpi.h +++ b/src/ibpi.h @@ -93,8 +93,9 @@ enum ibpi_pattern { SES_REQ_DEV_OFF, SES_REQ_FAULT, SES_REQ_PRDFAIL, + ibpi_pattern_count, }; -extern const char *ibpi_str[]; +extern const char *ibpi_str[ibpi_pattern_count]; #endif /* _IBPI_H_INCLUDED_ */ diff --git a/src/ledctl.c b/src/ledctl.c index 2aa1abc..4dada8e 100644 --- a/src/ledctl.c +++ b/src/ledctl.c @@ -267,7 +267,7 @@ static void _determine(struct ibpi_state *state) } else { log_warning ("IBPI %s: missing block device(s)... pattern ignored.", - ibpi_str[state->ibpi]); + ibpi2str(state->ibpi)); } } @@ -458,7 +458,7 @@ static status_t _ibpi_state_add_block(struct ibpi_state *state, char *name) list_append(&state->block_list, blk1); else log_info("%s: %s: device already on the list.", - ibpi_str[state->ibpi], path); + ibpi2str(state->ibpi), path); return STATUS_SUCCESS; } diff --git a/src/ledmon.c b/src/ledmon.c index 0ea2583..b775e6f 100644 --- a/src/ledmon.c +++ b/src/ledmon.c @@ -667,8 +667,8 @@ static void _add_block(struct block_device *block) if (ibpi != temp->ibpi && ibpi <= IBPI_PATTERN_REMOVED) { log_info("CHANGE %s: from '%s' to '%s'.", - temp->sysfs_path, ibpi_str[ibpi], - ibpi_str[temp->ibpi]); + temp->sysfs_path, ibpi2str(ibpi), + ibpi2str(temp->ibpi)); } /* Check if name of the device changed.*/ if (strcmp(temp->sysfs_path, block->sysfs_path)) { @@ -682,7 +682,7 @@ static void _add_block(struct block_device *block) temp = block_device_duplicate(block); if (temp != NULL) { log_info("NEW %s: state '%s'.", temp->sysfs_path, - ibpi_str[temp->ibpi]); + ibpi2str(temp->ibpi)); list_append(&ledmon_block_list, temp); } } @@ -714,8 +714,8 @@ static void _send_msg(struct block_device *block) block->ibpi == IBPI_PATTERN_REMOVED) { if (block->ibpi != IBPI_PATTERN_FAILED_DRIVE) { log_info("CHANGE %s: from '%s' to '%s'.", - block->sysfs_path, ibpi_str[block->ibpi], - ibpi_str[IBPI_PATTERN_FAILED_DRIVE]); + block->sysfs_path, ibpi2str(block->ibpi), + ibpi2str(IBPI_PATTERN_FAILED_DRIVE)); block->ibpi = IBPI_PATTERN_FAILED_DRIVE; } else { char *host = strstr(block->sysfs_path, "host"); diff --git a/src/scsi.c b/src/scsi.c index 3267076..03b43dd 100644 --- a/src/scsi.c +++ b/src/scsi.c @@ -673,7 +673,7 @@ int scsi_ses_write(struct block_device *device, enum ibpi_pattern ibpi) if (ret) { log_warning ("Unable to send %s message to %s. Device is missing?", - ibpi_str[ibpi], strstr(device->sysfs_path, "host")); + ibpi2str(ibpi), strstr(device->sysfs_path, "host")); return ret; } diff --git a/src/smp.c b/src/smp.c index 038fe02..6023b0a 100644 --- a/src/smp.c +++ b/src/smp.c @@ -468,16 +468,16 @@ int scsi_smp_fill_buffer(struct block_device *device, enum ibpi_pattern ibpi) if (c++) { log_debug ("pattern %s not supported for device (/dev/%s)", - ibpi_str[ibpi], c); + ibpi2str(ibpi), c); fprintf(stderr, "%s(): pattern %s not supported for device (/dev/%s)\n", - __func__, ibpi_str[ibpi], c); + __func__, ibpi2str(ibpi), c); } else { log_debug("pattern %s not supported for device %s", - ibpi_str[ibpi], device->sysfs_path); + ibpi2str(ibpi), device->sysfs_path); fprintf(stderr, "%s(): pattern %s not supported for device\n\t(%s)\n", - __func__, ibpi_str[ibpi], device->sysfs_path); + __func__, ibpi2str(ibpi), device->sysfs_path); } __set_errno_and_return(ENOTSUP); } diff --git a/src/sysfs.c b/src/sysfs.c index 694878a..d4622dc 100644 --- a/src/sysfs.c +++ b/src/sysfs.c @@ -487,7 +487,7 @@ static void _set_block_state(struct block_device *block, enum ibpi_pattern ibpi) char *debug_dev = strrchr(block->sysfs_path, '/'); debug_dev = debug_dev ? debug_dev + 1 : block->sysfs_path; log_debug("(%s): device: %s, state: %s", __func__, debug_dev, - ibpi_str[ibpi]); + ibpi2str(ibpi)); if (block->ibpi < ibpi) block->ibpi = ibpi; } diff --git a/src/utils.c b/src/utils.c index eb189b1..2920fba 100644 --- a/src/utils.c +++ b/src/utils.c @@ -616,3 +616,21 @@ status_t set_verbose_level(int log_level) } return STATUS_CMDLINE_ERROR; } + +const char *ibpi2str(enum ibpi_pattern ibpi) +{ + static char buf[20]; + const char *ret; + + if (ibpi >= 0 && ibpi < ibpi_pattern_count) + ret = ibpi_str[ibpi]; + else + ret = NULL; + + if (!ret) { + snprintf(buf, sizeof(buf), "(unknown: %d)", ibpi); + ret = buf; + } + + return ret; +} diff --git a/src/utils.h b/src/utils.h index 720447a..67a211c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -27,6 +27,7 @@ #include "list.h" #include "status.h" #include "syslog.h" +#include "ibpi.h" /** * Maximum number of bytes in temporary buffer. It is used for local variables. @@ -354,4 +355,7 @@ void setup_options(struct option **longopt, char **shortopt, int *options, int options_nr); int get_option_id(const char *optarg); status_t set_verbose_level(int log_level); + +const char *ibpi2str(enum ibpi_pattern ibpi); + #endif /* _UTILS_H_INCLUDED_ */ -- 2.20.1