libbb: nonblock_safe_read->nonblock_immune_read, remove unused param of xmalloc_reads

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2011-05-08 21:21:10 +02:00
parent b8709032a3
commit 80c5b6893d
8 changed files with 19 additions and 17 deletions

View File

@ -239,7 +239,7 @@ static int apply_one_hunk(void)
plist = TT.current_hunk;
buf = NULL;
if (reverse ? TT.oldlen : TT.newlen) for (;;) {
char *data = xmalloc_reads(TT.filein, NULL, NULL);
char *data = xmalloc_reads(TT.filein, NULL);
TT.linenum++;

View File

@ -672,7 +672,7 @@ void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) F
extern ssize_t safe_read(int fd, void *buf, size_t count) FAST_FUNC;
extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count) FAST_FUNC;
extern ssize_t nonblock_immune_read(int fd, void *buf, size_t count) FAST_FUNC;
// NB: will return short read on error, not -1,
// if some data was read before error occurred
extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC;
@ -683,7 +683,7 @@ extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FA
// Reads one line a-la fgets (but doesn't save terminating '\n').
// Reads byte-by-byte. Useful when it is important to not read ahead.
// Bytes are appended to pfx (which must be malloced, or NULL).
extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC;
extern char *xmalloc_reads(int fd, size_t *maxsz_p) FAST_FUNC;
/* Reads block up to *maxsz_p (default: INT_MAX - 4095) */
extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
/* Returns NULL if file can't be opened (default max size: INT_MAX - 4095) */

View File

@ -55,7 +55,7 @@
* which detects EAGAIN and uses poll() to wait on the fd.
* Thankfully, poll() doesn't care about O_NONBLOCK flag.
*/
ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count)
{
struct pollfd pfd[1];
ssize_t n;
@ -74,13 +74,15 @@ ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
// Reads one line a-la fgets (but doesn't save terminating '\n').
// Reads byte-by-byte. Useful when it is important to not read ahead.
// Bytes are appended to pfx (which must be malloced, or NULL).
char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p)
{
char *p;
size_t sz = buf ? strlen(buf) : 0;
char *buf = NULL;
size_t sz = 0;
size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
goto jump_in;
while (sz < maxsz) {
if ((size_t)(p - buf) == sz) {
jump_in:
@ -88,8 +90,8 @@ char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
p = buf + sz;
sz += 128;
}
/* nonblock_safe_read() because we are used by e.g. shells */
if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
if (nonblock_immune_read(fd, p, 1) != 1) {
/* EOF/error */
if (p == buf) { /* we read nothing */
free(buf);
return NULL;

View File

@ -172,8 +172,8 @@ void FAST_FUNC get_cred_or_die(int fd)
G.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: "));
G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: "));
} else {
G.user = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL);
G.pass = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL);
G.user = xmalloc_reads(fd, /* maxsize: */ NULL);
G.pass = xmalloc_reads(fd, /* maxsize: */ NULL);
}
if (!G.user || !*G.user || !G.pass)
bb_error_msg_and_die("no username or password");

View File

@ -102,7 +102,7 @@ static char *xmalloc_read_stdin(void)
{
// SECURITY:
size_t max = 4 * 1024; // more than enough for commands!
return xmalloc_reads(STDIN_FILENO, NULL, &max);
return xmalloc_reads(STDIN_FILENO, &max);
}
int lpd_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;

View File

@ -5918,7 +5918,7 @@ expbackq(union node *cmd, int quoted, int quotes)
read:
if (in.fd < 0)
break;
i = nonblock_safe_read(in.fd, buf, sizeof(buf));
i = nonblock_immune_read(in.fd, buf, sizeof(buf));
TRACE(("expbackq: read returns %d\n", i));
if (i <= 0)
break;
@ -9617,7 +9617,7 @@ preadfd(void)
#if ENABLE_FEATURE_EDITING
retry:
if (!iflag || g_parsefile->pf_fd != STDIN_FILENO)
nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
else {
int timeout = -1;
# if ENABLE_ASH_IDLE_TIMEOUT
@ -9663,10 +9663,10 @@ preadfd(void)
}
}
#else
nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
#endif
#if 0 /* disabled: nonblock_safe_read() handles this problem */
#if 0 /* disabled: nonblock_immune_read() handles this problem */
if (nr < 0) {
if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
int flags = fcntl(0, F_GETFL);

View File

@ -170,7 +170,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
if ((bufpos & 0xff) == 0)
buffer = xrealloc(buffer, bufpos + 0x100);
if (nonblock_safe_read(fd, &buffer[bufpos], 1) != 1) {
if (nonblock_immune_read(fd, &buffer[bufpos], 1) != 1) {
retval = (const char *)(uintptr_t)1;
break;
}

View File

@ -283,7 +283,7 @@ int acpid_main(int argc UNUSED_PARAM, char **argv)
char *buf;
int len;
buf = xmalloc_reads(pfd[i].fd, NULL, NULL);
buf = xmalloc_reads(pfd[i].fd, NULL);
/* buf = "button/power PWRB 00000080 00000000" */
len = strlen(buf) - 9;
if (len >= 0)