small fixes:
fix xstrdup to not grossly overallocate memory use xopen instean of xopen3 in several places etc.
This commit is contained in:
parent
c1660fea6d
commit
cf749bc10c
@ -851,7 +851,7 @@ int tar_main(int argc, char **argv)
|
|||||||
tar_handle->src_fd = fileno(tar_stream);
|
tar_handle->src_fd = fileno(tar_stream);
|
||||||
tar_handle->seek = seek_by_read;
|
tar_handle->seek = seek_by_read;
|
||||||
} else {
|
} else {
|
||||||
tar_handle->src_fd = xopen3(tar_filename, flags, 0666);
|
tar_handle->src_fd = xopen(tar_filename, flags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ int dd_main(int argc, char **argv)
|
|||||||
if (!seek && (flags & trunc_flag))
|
if (!seek && (flags & trunc_flag))
|
||||||
oflag |= O_TRUNC;
|
oflag |= O_TRUNC;
|
||||||
|
|
||||||
ofd = xopen3(outfile, oflag, 0666);
|
ofd = xopen(outfile, oflag);
|
||||||
|
|
||||||
if (seek && (flags & trunc_flag)) {
|
if (seek && (flags & trunc_flag)) {
|
||||||
if (ftruncate(ofd, seek * obs) < 0) {
|
if (ftruncate(ofd, seek * obs) < 0) {
|
||||||
|
@ -57,7 +57,7 @@ char * xstrdup(const char *s)
|
|||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
t = strdup (s);
|
t = strdup(s);
|
||||||
|
|
||||||
if (t == NULL)
|
if (t == NULL)
|
||||||
bb_error_msg_and_die(bb_msg_memory_exhausted);
|
bb_error_msg_and_die(bb_msg_memory_exhausted);
|
||||||
@ -69,23 +69,33 @@ char * xstrdup(const char *s)
|
|||||||
// the (possibly truncated to length n) string into it.
|
// the (possibly truncated to length n) string into it.
|
||||||
char * xstrndup(const char *s, int n)
|
char * xstrndup(const char *s, int n)
|
||||||
{
|
{
|
||||||
|
int m;
|
||||||
char *t;
|
char *t;
|
||||||
|
|
||||||
if (ENABLE_DEBUG && s == NULL)
|
if (ENABLE_DEBUG && s == NULL)
|
||||||
bb_error_msg_and_die("xstrndup bug");
|
bb_error_msg_and_die("xstrndup bug");
|
||||||
|
|
||||||
/* TODO: think about xstrndup("abc", 10000)!!! */
|
/* We can just xmalloc(n+1) and strncpy into it, */
|
||||||
t = xmalloc(++n);
|
/* but think about xstrndup("abc", 10000) wastage! */
|
||||||
|
m = n;
|
||||||
|
t = (char*) s;
|
||||||
|
while (m) {
|
||||||
|
if (!*t) break;
|
||||||
|
m--; t++;
|
||||||
|
}
|
||||||
|
n = n - m;
|
||||||
|
t = xmalloc(n + 1);
|
||||||
|
t[n] = '\0';
|
||||||
|
|
||||||
return safe_strncpy(t,s,n);
|
return memcpy(t,s,n);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Die if we can't open a file and return a FILE * to it.
|
// Die if we can't open a file and return a FILE * to it.
|
||||||
// Notice we haven't got xfread(), This is for use with fscanf() and friends.
|
// Notice we haven't got xfread(), This is for use with fscanf() and friends.
|
||||||
FILE *xfopen(const char *path, const char *mode)
|
FILE *xfopen(const char *path, const char *mode)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp = fopen(path, mode);
|
||||||
if ((fp = fopen(path, mode)) == NULL)
|
if (fp == NULL)
|
||||||
bb_perror_msg_and_die("%s", path);
|
bb_perror_msg_and_die("%s", path);
|
||||||
return fp;
|
return fp;
|
||||||
}
|
}
|
||||||
@ -93,8 +103,8 @@ FILE *xfopen(const char *path, const char *mode)
|
|||||||
// Die if we can't open an existing file and return an fd.
|
// Die if we can't open an existing file and return an fd.
|
||||||
int xopen(const char *pathname, int flags)
|
int xopen(const char *pathname, int flags)
|
||||||
{
|
{
|
||||||
if (ENABLE_DEBUG && (flags & O_CREAT))
|
//if (ENABLE_DEBUG && (flags & O_CREAT))
|
||||||
bb_error_msg_and_die("xopen() with O_CREAT");
|
// bb_error_msg_and_die("xopen() with O_CREAT");
|
||||||
|
|
||||||
return xopen3(pathname, flags, 0666);
|
return xopen3(pathname, flags, 0666);
|
||||||
}
|
}
|
||||||
@ -142,7 +152,7 @@ off_t xlseek(int fd, off_t offset, int whence)
|
|||||||
return off;
|
return off;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Die with supplied error message if this FILE * has ferror set.
|
// Die with supplied filename if this FILE * has ferror set.
|
||||||
void die_if_ferror(FILE *fp, const char *fn)
|
void die_if_ferror(FILE *fp, const char *fn)
|
||||||
{
|
{
|
||||||
if (ferror(fp)) {
|
if (ferror(fp)) {
|
||||||
@ -214,7 +224,6 @@ void xsetenv(const char *key, const char *value)
|
|||||||
bb_error_msg_and_die(bb_msg_memory_exhausted);
|
bb_error_msg_and_die(bb_msg_memory_exhausted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Converts unsigned long long value into compact 4-char
|
// Converts unsigned long long value into compact 4-char
|
||||||
// representation. Examples: "1234", "1.2k", " 27M", "123T"
|
// representation. Examples: "1234", "1.2k", " 27M", "123T"
|
||||||
// Fifth char is always '\0'
|
// Fifth char is always '\0'
|
||||||
@ -257,7 +266,6 @@ void smart_ulltoa5(unsigned long long ul, char buf[5])
|
|||||||
buf[4] = '\0';
|
buf[4] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Convert unsigned integer to ascii, writing into supplied buffer. A
|
// Convert unsigned integer to ascii, writing into supplied buffer. A
|
||||||
// truncated result is always null terminated (unless buflen is 0), and
|
// truncated result is always null terminated (unless buflen is 0), and
|
||||||
// contains the first few digits of the result ala strncpy.
|
// contains the first few digits of the result ala strncpy.
|
||||||
|
@ -156,6 +156,7 @@ int crontab_main(int ac, char **av)
|
|||||||
break;
|
break;
|
||||||
case EDIT:
|
case EDIT:
|
||||||
{
|
{
|
||||||
|
/* FIXME: messy code here! we have file copying helpers for this! */
|
||||||
FILE *fi;
|
FILE *fi;
|
||||||
int fd;
|
int fd;
|
||||||
int n;
|
int n;
|
||||||
@ -163,11 +164,12 @@ int crontab_main(int ac, char **av)
|
|||||||
|
|
||||||
snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
|
snprintf(tmp, sizeof(tmp), TMPDIR "/crontab.%d", getpid());
|
||||||
fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
|
fd = xopen3(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600);
|
||||||
|
/* race, use fchown */
|
||||||
chown(tmp, getuid(), getgid());
|
chown(tmp, getuid(), getgid());
|
||||||
fi = fopen(pas->pw_name, "r");
|
fi = fopen(pas->pw_name, "r");
|
||||||
if (fi) {
|
if (fi) {
|
||||||
while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
|
while ((n = fread(buf, 1, sizeof(buf), fi)) > 0)
|
||||||
write(fd, buf, n);
|
full_write(fd, buf, n);
|
||||||
}
|
}
|
||||||
EditFile(caller, tmp);
|
EditFile(caller, tmp);
|
||||||
remove(tmp);
|
remove(tmp);
|
||||||
@ -178,6 +180,7 @@ int crontab_main(int ac, char **av)
|
|||||||
/* fall through */
|
/* fall through */
|
||||||
case REPLACE:
|
case REPLACE:
|
||||||
{
|
{
|
||||||
|
/* same here */
|
||||||
char path[1024];
|
char path[1024];
|
||||||
int fd;
|
int fd;
|
||||||
int n;
|
int n;
|
||||||
@ -186,7 +189,7 @@ int crontab_main(int ac, char **av)
|
|||||||
fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
|
fd = open(path, O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0600);
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
while ((n = read(repFd, buf, sizeof(buf))) > 0) {
|
while ((n = read(repFd, buf, sizeof(buf))) > 0) {
|
||||||
write(fd, buf, n);
|
full_write(fd, buf, n);
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
rename(path, pas->pw_name);
|
rename(path, pas->pw_name);
|
||||||
|
@ -263,7 +263,7 @@ int rx_main(int argc, char **argv)
|
|||||||
|
|
||||||
fn = argv[1];
|
fn = argv[1];
|
||||||
ttyfd = xopen(CURRENT_TTY, O_RDWR);
|
ttyfd = xopen(CURRENT_TTY, O_RDWR);
|
||||||
filefd = xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
|
filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
|
||||||
|
|
||||||
if (tcgetattr(ttyfd, &tty) < 0)
|
if (tcgetattr(ttyfd, &tty) < 0)
|
||||||
bb_perror_msg_and_die("tcgetattr");
|
bb_perror_msg_and_die("tcgetattr");
|
||||||
|
@ -166,7 +166,7 @@ int ftp_receive(ftp_host_info_t *server, FILE *control_stream,
|
|||||||
if (do_continue) {
|
if (do_continue) {
|
||||||
fd_local = xopen(local_path, O_APPEND | O_WRONLY);
|
fd_local = xopen(local_path, O_APPEND | O_WRONLY);
|
||||||
} else {
|
} else {
|
||||||
fd_local = xopen3(local_path, O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
fd_local = xopen(local_path, O_CREAT | O_TRUNC | O_WRONLY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -452,8 +452,8 @@ int wget_main(int argc, char **argv)
|
|||||||
|
|
||||||
/* Do it before progressmeter (want to have nice error message) */
|
/* Do it before progressmeter (want to have nice error message) */
|
||||||
if (output_fd < 0)
|
if (output_fd < 0)
|
||||||
output_fd = xopen3(fname_out,
|
output_fd = xopen(fname_out,
|
||||||
O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0666);
|
O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
|
||||||
|
|
||||||
if (!(opt & WGET_OPT_QUIET))
|
if (!(opt & WGET_OPT_QUIET))
|
||||||
progressmeter(-1);
|
progressmeter(-1);
|
||||||
|
@ -148,19 +148,19 @@ static unsigned processorstart(struct logdir *ld)
|
|||||||
if (fd_move(0, fd) == -1)
|
if (fd_move(0, fd) == -1)
|
||||||
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
|
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
|
||||||
ld->fnsave[26] = 't';
|
ld->fnsave[26] = 't';
|
||||||
fd = xopen3(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644);
|
fd = xopen(ld->fnsave, O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
|
||||||
if (fd_move(1, fd) == -1)
|
if (fd_move(1, fd) == -1)
|
||||||
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
|
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
|
||||||
fd = open_read("state");
|
fd = open_read("state");
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
if (errno != ENOENT)
|
if (errno != ENOENT)
|
||||||
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name);
|
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "open state for", ld->name);
|
||||||
close(xopen3("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644));
|
close(xopen("state", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT));
|
||||||
fd = xopen("state", O_RDONLY|O_NDELAY);
|
fd = xopen("state", O_RDONLY|O_NDELAY);
|
||||||
}
|
}
|
||||||
if (fd_move(4, fd) == -1)
|
if (fd_move(4, fd) == -1)
|
||||||
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
|
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
|
||||||
fd = xopen3("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT, 0644);
|
fd = xopen("newstate", O_WRONLY|O_NDELAY|O_TRUNC|O_CREAT);
|
||||||
if (fd_move(5, fd) == -1)
|
if (fd_move(5, fd) == -1)
|
||||||
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
|
bb_perror_msg_and_die(FATAL"cannot %s processor %s", "move filedescriptor for", ld->name);
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ static void make_device(char *path, int delete)
|
|||||||
|
|
||||||
line++;
|
line++;
|
||||||
/* find end of this line */
|
/* find end of this line */
|
||||||
for(end=pos; end-conf<len && *end!='\n'; end++)
|
for (end=pos; end-conf<len && *end!='\n'; end++)
|
||||||
;
|
;
|
||||||
|
|
||||||
/* Three fields: regex, uid:gid, mode */
|
/* Three fields: regex, uid:gid, mode */
|
||||||
@ -111,7 +111,7 @@ static void make_device(char *path, int delete)
|
|||||||
char *s, *s2;
|
char *s, *s2;
|
||||||
|
|
||||||
/* Find : */
|
/* Find : */
|
||||||
for(s=pos; s<end2 && *s!=':'; s++)
|
for (s=pos; s<end2 && *s!=':'; s++)
|
||||||
;
|
;
|
||||||
if (s == end2) break;
|
if (s == end2) break;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user