]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
json_discovery_log: avoid buffer overrun
authorCaleb Sander <csander@purestorage.com>
Thu, 26 Jan 2023 17:53:07 +0000 (10:53 -0700)
committerCaleb Sander <csander@purestorage.com>
Thu, 26 Jan 2023 18:28:45 +0000 (11:28 -0700)
nvme_strip_spaces() has an off-by-one error and starts stripping spaces
at the byte AFTER the end of the buffer. Use space_strip_len() instead,
which doesn't have this bug and is already used in print_discovery_log().

Signed-off-by: Caleb Sander <csander@purestorage.com>
fabrics.c
nvme.h

index 8c19b07e6246f908d1bb4cb882839ca8b87ea8e2..a8c5cd81dadcfddb21c24e56534d3404022d08a0 100644 (file)
--- a/fabrics.c
+++ b/fabrics.c
@@ -350,9 +350,9 @@ static void json_discovery_log(struct nvmf_discovery_log *log, int numrec)
                struct nvmf_disc_log_entry *e = &log->entries[i];
                struct json_object *entry = json_create_object();
 
-               nvme_strip_spaces(e->trsvcid, NVMF_TRSVCID_SIZE);
-               nvme_strip_spaces(e->subnqn, NVMF_NQN_SIZE);
-               nvme_strip_spaces(e->traddr, NVMF_TRADDR_SIZE);
+               space_strip_len(NVMF_TRSVCID_SIZE, e->trsvcid);
+               space_strip_len(NVMF_NQN_SIZE, e->subnqn);
+               space_strip_len(NVMF_TRADDR_SIZE, e->traddr);
 
                json_object_add_value_string(entry, "trtype",
                                             nvmf_trtype_str(e->trtype));
diff --git a/nvme.h b/nvme.h
index c13ca8e90bc9fbc9d96140394b67589adada4095..1938afb934f7e951bc6a06ce675644923b734aaa 100644 (file)
--- a/nvme.h
+++ b/nvme.h
@@ -114,12 +114,6 @@ const char *nvme_strerror(int errnum);
 unsigned long long elapsed_utime(struct timeval start_time,
                                        struct timeval end_time);
 
-static inline void nvme_strip_spaces(char *s, int l)
-{
-        while (l && (s[l] == '\0' || s[l] == ' '))
-                s[l--] = '\0';
-}
-
 /* nvme-print.c */
 const char *nvme_select_to_string(int sel);