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; plist = TT.current_hunk;
buf = NULL; buf = NULL;
if (reverse ? TT.oldlen : TT.newlen) for (;;) { if (reverse ? TT.oldlen : TT.newlen) for (;;) {
char *data = xmalloc_reads(TT.filein, NULL, NULL); char *data = xmalloc_reads(TT.filein, NULL);
TT.linenum++; 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 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, // NB: will return short read on error, not -1,
// if some data was read before error occurred // if some data was read before error occurred
extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC; 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 one line a-la fgets (but doesn't save terminating '\n').
// Reads byte-by-byte. Useful when it is important to not read ahead. // Reads byte-by-byte. Useful when it is important to not read ahead.
// Bytes are appended to pfx (which must be malloced, or NULL). // 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) */ /* Reads block up to *maxsz_p (default: INT_MAX - 4095) */
extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; 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) */ /* 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. * which detects EAGAIN and uses poll() to wait on the fd.
* Thankfully, poll() doesn't care about O_NONBLOCK flag. * 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]; struct pollfd pfd[1];
ssize_t n; 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 one line a-la fgets (but doesn't save terminating '\n').
// Reads byte-by-byte. Useful when it is important to not read ahead. // Reads byte-by-byte. Useful when it is important to not read ahead.
// Bytes are appended to pfx (which must be malloced, or NULL). // 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; 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); size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
goto jump_in; goto jump_in;
while (sz < maxsz) { while (sz < maxsz) {
if ((size_t)(p - buf) == sz) { if ((size_t)(p - buf) == sz) {
jump_in: jump_in:
@ -88,8 +90,8 @@ char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
p = buf + sz; p = buf + sz;
sz += 128; sz += 128;
} }
/* nonblock_safe_read() because we are used by e.g. shells */ if (nonblock_immune_read(fd, p, 1) != 1) {
if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */ /* EOF/error */
if (p == buf) { /* we read nothing */ if (p == buf) { /* we read nothing */
free(buf); free(buf);
return NULL; 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.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: "));
G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: ")); G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: "));
} else { } else {
G.user = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL); G.user = xmalloc_reads(fd, /* maxsize: */ NULL);
G.pass = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL); G.pass = xmalloc_reads(fd, /* maxsize: */ NULL);
} }
if (!G.user || !*G.user || !G.pass) if (!G.user || !*G.user || !G.pass)
bb_error_msg_and_die("no username or password"); bb_error_msg_and_die("no username or password");

View File

@ -102,7 +102,7 @@ static char *xmalloc_read_stdin(void)
{ {
// SECURITY: // SECURITY:
size_t max = 4 * 1024; // more than enough for commands! 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; 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: read:
if (in.fd < 0) if (in.fd < 0)
break; 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)); TRACE(("expbackq: read returns %d\n", i));
if (i <= 0) if (i <= 0)
break; break;
@ -9617,7 +9617,7 @@ preadfd(void)
#if ENABLE_FEATURE_EDITING #if ENABLE_FEATURE_EDITING
retry: retry:
if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) 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 { else {
int timeout = -1; int timeout = -1;
# if ENABLE_ASH_IDLE_TIMEOUT # if ENABLE_ASH_IDLE_TIMEOUT
@ -9663,10 +9663,10 @@ preadfd(void)
} }
} }
#else #else
nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
#endif #endif
#if 0 /* disabled: nonblock_safe_read() handles this problem */ #if 0 /* disabled: nonblock_immune_read() handles this problem */
if (nr < 0) { if (nr < 0) {
if (parsefile->fd == 0 && errno == EWOULDBLOCK) { if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
int flags = fcntl(0, F_GETFL); 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) if ((bufpos & 0xff) == 0)
buffer = xrealloc(buffer, bufpos + 0x100); 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; retval = (const char *)(uintptr_t)1;
break; break;
} }

View File

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