Skip to contentSkip to conversation

Stop docproc segfaulting when SRCTREE isn't set.

PATCH In review Rob Landley 4 participants 9 messages Oct 9, 2007 – Oct 18, 2007 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Stop docproc segfaulting when SRCTREE isn't set.

From: Rob Landley <[email protected]>

Prevent docproc from segfaulting when SRCTREE isn't set.

---

 scripts/basic/docproc.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
scripts/basic/docproc.c +8 -2
diff -r a26a53ed1101 scripts/basic/docproc.c
@@ -64,12 +64,15 @@ FILELINE * entity_system;
 #define FUNCTION      "-function"
 #define NOFUNCTION    "-nofunction"
 
+char *srctree;
+
 void usage (void)
 {
 	fprintf(stderr, "Usage: docproc {doc|depend} file\n");
 	fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
 	fprintf(stderr, "doc: frontend when generating kernel documentation\n");
 	fprintf(stderr, "depend: generate list of files referenced within file\n");
+	fprintf(stderr, "Environment variable SRCTREE: absolute path to kernel source tree.\n");
 }
 
 /*
@@ -88,7 +91,7 @@ void exec_kernel_doc(char **svec)
 			exit(1);
 		case  0:
 			memset(real_filename, 0, sizeof(real_filename));
-			strncat(real_filename, getenv("SRCTREE"), PATH_MAX);
+			strncat(real_filename, srctree, PATH_MAX);
 			strncat(real_filename, KERNELDOCPATH KERNELDOC,
 					PATH_MAX - strlen(real_filename));
 			execvp(real_filename, svec);
@@ -168,7 +171,7 @@ void find_export_symbols(char * filename
 	if (filename_exist(filename) == NULL) {
 		char real_filename[PATH_MAX + 1];
 		memset(real_filename, 0, sizeof(real_filename));
-		strncat(real_filename, getenv("SRCTREE"), PATH_MAX);
+		strncat(real_filename, srctree, PATH_MAX);
 		strncat(real_filename, filename,
 				PATH_MAX - strlen(real_filename));
 		sym = add_new_file(filename);
@@ -335,6 +338,9 @@ int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 	FILE * infile;
+
+	srctree = getenv("SRCTREE");
+	if (!srctree) srctree = getcwd(NULL,0);
 	if (argc != 3) {
 		usage();
 		exit(1);
Signature · 2 lines
"One of my most productive days was throwing away 1000 lines of code."
  - Ken Thompson.
#1 lore raw

Hi Rob,

On Tue, Oct 09, 2007 at 01:25:18AM -0500, Rob Landley wrote:
[...]

scripts/basic/docproc.c int main(int argc, char *argv[]) · line 346

Quoted 7 lines
scripts/basic/docproc.c +3 -0
 	FILE * infile;
+
+	srctree = getenv("SRCTREE");
+	if (!srctree) srctree = getcwd(NULL,0);
 	if (argc != 3) {
 		usage();
 		exit(1);
$ man getcwd

char *getcwd(char *buf, size_t size);

As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call.

Shouldn't "srctree" be free()ed in case getenv("SRCTREE") failed ?

Regards,

Signature · 3 lines
Ahmed S. Darwish
HomePage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
#2 lore raw
Quoted 14 lines · On Tue, 9 Oct 2007 15:03:15 +0200 Ahmed S. Darwish wrote:

Hi Rob,

On Tue, Oct 09, 2007 at 01:25:18AM -0500, Rob Landley wrote:
[...]

scripts/basic/docproc.c int main(int argc, char *argv[]) · line 346

Quoted 7 lines
scripts/basic/docproc.c +3 -0
 	FILE * infile;
+
+	srctree = getenv("SRCTREE");
+	if (!srctree) srctree = getcwd(NULL,0);
 	if (argc != 3) {
 		usage();
 		exit(1);
$ man getcwd
Quoted 6 lines

char *getcwd(char *buf, size_t size);

As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call.

Shouldn't "srctree" be free()ed in case getenv("SRCTREE") failed ?

What is there to free() at that point? If getenv() fails (i.e., the env. variable is not found), it returns NULL.
or do I need another cup of coffee?

---
~Randy

#3 lore raw
Quoted 18 lines · Randy Dunlap
Quoted 16 lines · On Tue, 9 Oct 2007 15:03:15 +0200 Ahmed S. Darwish wrote:

Hi Rob,

On Tue, Oct 09, 2007 at 01:25:18AM -0500, Rob Landley wrote:
[...]

scripts/basic/docproc.c int main(int argc, char *argv[]) · line 346

Quoted 7 lines
scripts/basic/docproc.c +3 -0
 	FILE * infile;
+
+	srctree = getenv("SRCTREE");
+	if (!srctree) srctree = getcwd(NULL,0);
 	if (argc != 3) {
 		usage();
 		exit(1);
$ man getcwd

char *getcwd(char *buf, size_t size);

Quoted 9 lines
Quoted 4 lines

As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call.

Shouldn't "srctree" be free()ed in case getenv("SRCTREE") failed ?

What is there to free() at that point? If getenv() fails (i.e., the env. variable is not found), it returns NULL.
or do I need another cup of coffee?

I meant if getenv() failed, "srctree = getcwd(NULL, 0)" will let "srctree" point to a _ malloc()ed _ buffer representing PWD. As said in the manpage, this buffer needs to be free()ed after usage. Right or I'm the one who needs that cup of coffee :) ?

Regards,

Signature · 3 lines
Ahmed S. Darwish
HomePage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
#4 lore raw
Quoted 21 lines · Ahmed S. Darwish
Quoted 20 lines · Randy Dunlap
Quoted 18 lines · On Tue, 9 Oct 2007 15:03:15 +0200 Ahmed S. Darwish wrote:

Hi Rob,

On Tue, Oct 09, 2007 at 01:25:18AM -0500, Rob Landley wrote:
[...]

scripts/basic/docproc.c int main(int argc, char *argv[]) · line 346

Quoted 7 lines
scripts/basic/docproc.c +3 -0
 	FILE * infile;
+
+	srctree = getenv("SRCTREE");
+	if (!srctree) srctree = getcwd(NULL,0);
 	if (argc != 3) {
 		usage();
 		exit(1);
$ man getcwd

char *getcwd(char *buf, size_t size);

As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Shouldn't "srctree" be free()ed in case getenv("SRCTREE") failed ?

What is there to free() at that point? If getenv() fails (i.e., the env. variable is not found), it returns NULL.
or do I need another cup of coffee?

I meant if getenv() failed, "srctree = getcwd(NULL, 0)" will let "srctree" point to a _ malloc()ed _ buffer representing PWD. As said in the manpage, this buffer needs to be free()ed after usage. Right or I'm the one who needs that cup of coffee :) ?

so it needs to be freed at program termination, is that what you are saying? That will happen automatically (along with any open files being closed, etc.).

Signature · 1 line
~Randy
#5 lore raw
Quoted 29 lines · Randy Dunlap
Quoted 28 lines · Ahmed S. Darwish
Quoted 26 lines · Randy Dunlap
Quoted 13 lines · On Tue, 9 Oct 2007 15:03:15 +0200 Ahmed S. Darwish wrote:

Hi Rob,

On Tue, Oct 09, 2007 at 01:25:18AM -0500, Rob Landley wrote:
[...]

Quoted 7 lines
	FILE * infile;
scripts/basic/docproc.c +3 -0
+
+	srctree = getenv("SRCTREE");
+	if (!srctree) srctree = getcwd(NULL,0);
	if (argc != 3) {
		usage();
		exit(1);
$ man getcwd
Quoted 7 lines

char *getcwd(char *buf, size_t size);

As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call.

Shouldn't "srctree" be free()ed in case getenv("SRCTREE") failed ?

What is there to free() at that point? If getenv() fails (i.e., the env. variable is not found), it returns NULL.
or do I need another cup of coffee?

Quoted 9 lines
Quoted 4 lines

I meant if getenv() failed, "srctree = getcwd(NULL, 0)" will let "srctree" point to a _ malloc()ed _ buffer representing PWD. As said in the manpage, this buffer needs to be free()ed after usage. Right or I'm the one who needs that cup of coffee :) ?

so it needs to be freed at program termination, is that what you are saying? That will happen automatically (along with any open files being closed, etc.).

I didn't know that leaked mem will be automatically freed at program termination. A new info, Thanks!.

Signature · 3 lines
Ahmed S. Darwish
HomePage: http://darwish.07.googlepages.com
Blog: http://darwish-07.blogspot.com
#6 lore raw
In reply to Ahmed S. Darwish
Quoted 9 lines · Ahmed S. Darwish
$ man getcwd

char *getcwd(char *buf, size_t size);

As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc)
getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call.

Shouldn't "srctree" be free()ed in case getenv("SRCTREE") failed ?

If exit() doesn't free all the memory the program allocated, we have bigger problems.

Quoted 1 line

Regards,

Rob

Signature · 2 lines
"One of my most productive days was throwing away 1000 lines of code."
  - Ken Thompson.
Acked-by on v1 1/1
#7 lore raw
In reply to Rob Landley
Quoted 5 lines · On Tue, 9 Oct 2007 01:25:18 -0500 Rob Landley wrote:

From: Rob Landley <[email protected]>

Prevent docproc from segfaulting when SRCTREE isn't set.

but needs 2 coding style fixes... see near end, below.

Quoted 43 lines

---

 scripts/basic/docproc.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff -r a26a53ed1101 scripts/basic/docproc.c

scripts/basic/docproc.c +5 -2
@@ -64,12 +64,15 @@ FILELINE * entity_system;
 #define FUNCTION      "-function"
 #define NOFUNCTION    "-nofunction"
 
+char *srctree;
+
 void usage (void)
 {
 	fprintf(stderr, "Usage: docproc {doc|depend} file\n");
 	fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n");
 	fprintf(stderr, "doc: frontend when generating kernel documentation\n");
 	fprintf(stderr, "depend: generate list of files referenced within file\n");
+	fprintf(stderr, "Environment variable SRCTREE: absolute path to kernel source tree.\n");
 }
 
 /*
@@ -88,7 +91,7 @@ void exec_kernel_doc(char **svec)
 			exit(1);
 		case  0:
 			memset(real_filename, 0, sizeof(real_filename));
-			strncat(real_filename, getenv("SRCTREE"), PATH_MAX);
+			strncat(real_filename, srctree, PATH_MAX);
 			strncat(real_filename, KERNELDOCPATH KERNELDOC,
 					PATH_MAX - strlen(real_filename));
 			execvp(real_filename, svec);
@@ -168,7 +171,7 @@ void find_export_symbols(char * filename
 	if (filename_exist(filename) == NULL) {
 		char real_filename[PATH_MAX + 1];
 		memset(real_filename, 0, sizeof(real_filename));
-		strncat(real_filename, getenv("SRCTREE"), PATH_MAX);
+		strncat(real_filename, srctree, PATH_MAX);
 		strncat(real_filename, filename,
 				PATH_MAX - strlen(real_filename));
 		sym = add_new_file(filename);

scripts/basic/docproc.c int main(int argc, char *argv[]) · line 343

Quoted 6 lines
scripts/basic/docproc.c +3 -0
@@ -335,6 +338,9 @@ int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 	FILE * infile;
+
+	srctree = getenv("SRCTREE");
+	if (!srctree) srctree = getcwd(NULL,0);
	if (!srctree)
		srctree = getcwd(NULL, 0);
Quoted 5 lines
scripts/basic/docproc.c +0 -0
 	if (argc != 3) {
 		usage();
 		exit(1);
Signature · 1 line
-- 

---
~Randy