Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.sbin/sysinst When reading output from a program we don't...
details: https://anonhg.NetBSD.org/src/rev/3762275698b3
branches: trunk
changeset: 984988:3762275698b3
user: martin <martin%NetBSD.org@localhost>
date: Tue Aug 03 13:34:04 2021 +0000
description:
When reading output from a program we don't know how much it will
be - so scale the buffer (within reasonable limits).
Problem pointed out by RVP, triggered e.g. by disks with lots of GPT
partitions.
diffstat:
usr.sbin/sysinst/run.c | 41 +++++++++++++++++++++++++++++++++++------
1 files changed, 35 insertions(+), 6 deletions(-)
diffs (90 lines):
diff -r 01db2528dfc8 -r 3762275698b3 usr.sbin/sysinst/run.c
--- a/usr.sbin/sysinst/run.c Tue Aug 03 11:30:25 2021 +0000
+++ b/usr.sbin/sysinst/run.c Tue Aug 03 13:34:04 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: run.c,v 1.13 2019/11/16 20:26:59 martin Exp $ */
+/* $NetBSD: run.c,v 1.14 2021/08/03 13:34:04 martin Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@@ -176,6 +176,7 @@
{
size_t nbytes; /* Number of bytes in buffer. */
size_t fbytes; /* Number of bytes in file. */
+ size_t abytes; /* allocated size of buffer */
struct stat st; /* stat information. */
int ch;
FILE *f;
@@ -198,6 +199,9 @@
/* Open the file. */
f = fopen(fileorcmd, "r");
if (f == NULL) {
+ if (logfp)
+ fprintf(logfp, "%s: failed to open %s\n",
+ __func__, fileorcmd);
*buffer = NULL;
return -1;
}
@@ -205,25 +209,47 @@
/* Open the program. */
f = popen(fileorcmd, "r");
if (f == NULL) {
+ if (logfp)
+ fprintf(logfp, "%s: failed to open %s\n",
+ __func__, fileorcmd);
*buffer = NULL;
return -1;
}
- fbytes = BUFSIZE;
+ fbytes = 0;
}
if (fbytes == 0)
- fbytes = BUFSIZE;
+ abytes = BUFSIZE;
+ else
+ abytes = fbytes+1;
/* Allocate the buffer size. */
- *buffer = cp = malloc(fbytes + 1);
+ *buffer = cp = malloc(abytes);
if (!cp)
nbytes = -1;
else {
/* Read the buffer. */
nbytes = 0;
- while (nbytes < fbytes && (ch = fgetc(f)) != EOF)
+ while ((ch = fgetc(f)) != EOF) {
+ if (nbytes >= abytes-1) {
+ if (fbytes > 0 || abytes >= 512*BUFSIZE) {
+ free(cp);
+ *buffer = cp = NULL;
+ nbytes = -1;
+ break;
+ }
+ abytes *= 2;
+ *buffer = cp = realloc(cp, abytes);
+ if (!cp) {
+ nbytes = -1;
+ break;
+ }
+
+ }
cp[nbytes++] = ch;
- cp[nbytes] = 0;
+ }
+ if (cp)
+ cp[nbytes] = 0;
}
if (kind == T_FILE)
@@ -231,6 +257,9 @@
else
pclose(f);
+ if (nbytes <= 0 && logfp)
+ fprintf(logfp, "%s: failed for %s\n", __func__, fileorcmd);
+
return nbytes;
}
Home |
Main Index |
Thread Index |
Old Index