Skip to content

Commit 61caac8

Browse files
authored
Merge pull request #310 from igaw/nvme_chomp
fabrics: Avoid out of bounds string chomping
2 parents 558618e + 0290a4b commit 61caac8

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/nvme/fabrics.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@
4646

4747
const char *nvmf_dev = "/dev/nvme-fabrics";
4848

49+
/**
50+
* strchomp() - Strip trailing white space
51+
* @s: String to strip
52+
* @l: Maximum length of string
53+
*/
54+
static void strchomp(char *s, int l)
55+
{
56+
while (l && (s[l] == '\0' || s[l] == ' '))
57+
s[l--] = '\0';
58+
}
59+
4960
const char *arg_str(const char * const *strings,
5061
size_t array_size, size_t idx)
5162
{
@@ -567,8 +578,8 @@ nvme_ctrl_t nvmf_connect_disc_entry(nvme_host_t h,
567578
switch (e->adrfam) {
568579
case NVMF_ADDR_FAMILY_IP4:
569580
case NVMF_ADDR_FAMILY_IP6:
570-
nvme_chomp(e->traddr, NVMF_TRADDR_SIZE);
571-
nvme_chomp(e->trsvcid, NVMF_TRSVCID_SIZE);
581+
strchomp(e->traddr, NVMF_TRADDR_SIZE - 1);
582+
strchomp(e->trsvcid, NVMF_TRSVCID_SIZE - 1);
572583
traddr = e->traddr;
573584
trsvcid = e->trsvcid;
574585
break;
@@ -583,7 +594,7 @@ nvme_ctrl_t nvmf_connect_disc_entry(nvme_host_t h,
583594
case NVMF_TRTYPE_FC:
584595
switch (e->adrfam) {
585596
case NVMF_ADDR_FAMILY_FC:
586-
nvme_chomp(e->traddr, NVMF_TRADDR_SIZE);
597+
strchomp(e->traddr, NVMF_TRADDR_SIZE - 1);
587598
traddr = e->traddr;
588599
break;
589600
default:
@@ -595,7 +606,7 @@ nvme_ctrl_t nvmf_connect_disc_entry(nvme_host_t h,
595606
}
596607
break;
597608
case NVMF_TRTYPE_LOOP:
598-
nvme_chomp(e->traddr, NVMF_TRADDR_SIZE);
609+
strchomp(e->traddr, NVMF_TRADDR_SIZE - 1);
599610
traddr = strlen(e->traddr) ? e->traddr : NULL;
600611
break;
601612
default:

src/nvme/fabrics.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,6 @@ nvme_ctrl_t nvmf_connect_disc_entry(nvme_host_t h,
230230
struct nvmf_disc_log_entry *e,
231231
const struct nvme_fabrics_config *defcfg, bool *discover);
232232

233-
/**
234-
* nvme_chomp() - Strip trailing white space
235-
* @s: String to strip
236-
* @l: Maximum length of string
237-
*/
238-
static inline void nvme_chomp(char *s, int l)
239-
{
240-
while (l && (s[l] == '\0' || s[l] == ' '))
241-
s[l--] = '\0';
242-
}
243-
244233
/**
245234
* nvmf_is_registration_supported - check whether registration can be performed.
246235
* @c: Controller instance

0 commit comments

Comments
 (0)