Changeset View
Changeset View
Standalone View
Standalone View
sbin/mdmfs/mdmfs.c
Show First 20 Lines • Show All 438 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* Attach a memory disk with an unknown unit; use autounit. | * Attach a memory disk with an unknown unit; use autounit. | ||||
*/ | */ | ||||
static void | static void | ||||
do_mdconfig_attach_au(const char *args, const enum md_types mdtype) | do_mdconfig_attach_au(const char *args, const enum md_types mdtype) | ||||
{ | { | ||||
const char *ta; /* Type arg. */ | const char *ta; /* Type arg. */ | ||||
char *linep, *linebuf; /* Line pointer, line buffer. */ | char *linep; | ||||
char linebuf[12]; /* 32-bit unit (10) + '\n' (1) + '\0' (1) */ | |||||
int fd; /* Standard output of mdconfig invocation. */ | int fd; /* Standard output of mdconfig invocation. */ | ||||
FILE *sfd; | FILE *sfd; | ||||
int rv; | int rv; | ||||
char *p; | char *p; | ||||
size_t linelen; | size_t linelen; | ||||
unsigned long ul; | unsigned long ul; | ||||
switch (mdtype) { | switch (mdtype) { | ||||
Show All 18 Lines | do_mdconfig_attach_au(const char *args, const enum md_types mdtype) | ||||
if (norun) { /* Since we didn't run, we can't read. Fake it. */ | if (norun) { /* Since we didn't run, we can't read. Fake it. */ | ||||
unit = 0; | unit = 0; | ||||
return; | return; | ||||
} | } | ||||
sfd = fdopen(fd, "r"); | sfd = fdopen(fd, "r"); | ||||
if (sfd == NULL) | if (sfd == NULL) | ||||
err(1, "fdopen"); | err(1, "fdopen"); | ||||
linep = fgetln(sfd, &linelen); | linep = fgetln(sfd, &linelen); | ||||
if (linep == NULL && linelen < mdnamelen + 1) | |||||
errx(1, "unexpected output from mdconfig (attach)"); | |||||
/* If the output format changes, we want to know about it. */ | /* If the output format changes, we want to know about it. */ | ||||
assert(strncmp(linep, mdname, mdnamelen) == 0); | if (linep == NULL || linelen <= mdnamelen + 1 || | ||||
markj: What if the line is just "md\n", i.e., there's no unit number? I think the second clause won't… | |||||
linebuf = malloc(linelen - mdnamelen + 1); | linelen - mdnamelen >= sizeof(linebuf) || | ||||
assert(linebuf != NULL); | strncmp(linep, mdname, mdnamelen) != 0) | ||||
errx(1, "unexpected output from mdconfig (attach)"); | |||||
linep += mdnamelen; | |||||
linelen -= mdnamelen; | |||||
Done Inline ActionsThis assertion is now pointless. jhb: This assertion is now pointless. | |||||
/* Can't use strlcpy because linep is not NULL-terminated. */ | /* Can't use strlcpy because linep is not NULL-terminated. */ | ||||
strncpy(linebuf, linep + mdnamelen, linelen); | strncpy(linebuf, linep, linelen); | ||||
linebuf[linelen] = '\0'; | linebuf[linelen] = '\0'; | ||||
ul = strtoul(linebuf, &p, 10); | ul = strtoul(linebuf, &p, 10); | ||||
if (ul == ULONG_MAX || *p != '\n') | if (ul == ULONG_MAX || *p != '\n') | ||||
errx(1, "unexpected output from mdconfig (attach)"); | errx(1, "unexpected output from mdconfig (attach)"); | ||||
unit = ul; | unit = ul; | ||||
fclose(sfd); | fclose(sfd); | ||||
close(fd); | |||||
} | } | ||||
Done Inline ActionsThis close() is wrong, but harmless. fdopen() "takes" the fd and so fclose() already closes it. jhb: This close() is wrong, but harmless. fdopen() "takes" the fd and so fclose() already closes it. | |||||
/* | /* | ||||
* Detach a memory disk. | * Detach a memory disk. | ||||
*/ | */ | ||||
static void | static void | ||||
do_mdconfig_detach(void) | do_mdconfig_detach(void) | ||||
{ | { | ||||
int rv; | int rv; | ||||
▲ Show 20 Lines • Show All 322 Lines • Show Last 20 Lines |
What if the line is just "md\n", i.e., there's no unit number? I think the second clause won't catch that case because linelen includes the newline character. That is, it looks like that should be linelen <= mdnamelen + 1.