Skip to contentSkip to conversation

kexec.c: workaround getline and fscanf to make it *libc agnostic. Tested against klibc and dietlibc.

PATCH In review Andrea Adami 4 participants 12 messages Nov 21, 2009 – Nov 27, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kexec.c: workaround getline and fscanf to make it *libc agnostic. Tested against klibc and dietlibc.

---
 kexec/kexec.c |   38 ++++++++++++++++++++++++++++++--------
 1 files changed, 30 insertions(+), 8 deletions(-)
kexec/kexec.c +30 -8
index ce105cd..a3ca79c 100644
@@ -933,15 +933,32 @@ void usage(void)
 
 static int kexec_loaded(void)
 {
-	int ret;
+	long ret = -1;
 	FILE *fp;
+	char *p;
+	char line[3];
 
 	fp = fopen("/sys/kernel/kexec_loaded", "r");
 	if (fp == NULL)
 		return -1;
-	fscanf(fp, "%d", &ret);
+/*	fscanf(fp, "%d", &ret); */
+	p = fgets(line, sizeof(line), fp);
 	fclose(fp);
-	return ret;
+
+	if ( NULL == p)
+		return -1;
+
+	ret = strtol(line, &p, 10);
+
+	if (ret > INT_MAX)
+	/* Too long */
+		return -1;
+
+	if (p == line)
+	/* No digits were found */
+		return -1;
+
+	return (int)ret;
 }
 
 /*
@@ -989,18 +1006,23 @@ static void remove_parameter(char *line, const char *param_name)
 char *get_command_line(void)
 {
 	FILE *fp;
-	size_t len;
-	char *line = NULL;
+	const int sizeof_line = 1024;
+	char *line = malloc(sizeof_line); /* according to strdup() later */
 
 	fp = fopen("/proc/cmdline", "r");
 	if (!fp)
-		die("Could not read /proc/cmdline.");
-	getline(&line, &len, fp);
+		die("Could not open /proc/cmdline.");
+
+	if ( NULL == fgets(line, sizeof(line), fp) ) {
+		die("Can't read /proc/cmdline.");
+
+/* 	getline(&line, &len, fp); */
 	fclose(fp);
+	}
 
 	if (line) {
 		/* strip newline */
-		*(line + strlen(line) - 1) = 0;
+		line[strlen(line) - 1] = '\0';
 
 		remove_parameter(line, "BOOT_IMAGE");
 		if (kexec_flags & KEXEC_ON_CRASH)
Signature · 7 lines
1.6.4.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw
Quoted 16 lines · Andrea Adami
---
 kexec/kexec.c |   38 ++++++++++++++++++++++++++++++--------
 1 files changed, 30 insertions(+), 8 deletions(-)
kexec/kexec.c +0 -1
index ce105cd..a3ca79c 100644
@@ -933,15 +933,32 @@ void usage(void)
 
 static int kexec_loaded(void)
 {
-	int ret;

kexec/kexec.c void usage(void) · line 944

Quoted 10 lines
kexec/kexec.c +4 -1
+	long ret = -1;
 	FILE *fp;
+	char *p;
+	char line[3];
 
 	fp = fopen("/sys/kernel/kexec_loaded", "r");
 	if (fp == NULL)
 		return -1;
-	fscanf(fp, "%d", &ret);
+/*	fscanf(fp, "%d", &ret); */

I'm not sure this comment is needed.
If you'd like to keep it, please indent it by one tab and replace the between '/*' and 'fscanf' with a single space.

/* fscanf(fp, "%d", &ret); */

kexec/kexec.c void usage(void) · line 954

Quoted 11 lines
kexec/kexec.c +9 -1
+	p = fgets(line, sizeof(line), fp);
 	fclose(fp);
-	return ret;
+
+	if ( NULL == p)
+		return -1;
+
+	ret = strtol(line, &p, 10);
+
+	if (ret > INT_MAX)
+	/* Too long */

Please remove this comment or move it to immediately above the if statement.

/* Too long */
if (ret > INT_MAX)
...

kexec/kexec.c void usage(void) · line 958

Quoted 4 lines
kexec/kexec.c +4 -0
+		return -1;
+
+	if (p == line)
+	/* No digits were found */

Again, please remove this comment or move it to immediately above the if statement.

kexec/kexec.c static void remove_parameter(char *line, const char *param_name) · line 1009

Quoted 13 lines
kexec/kexec.c +4 -2
+		return -1;
+
+	return (int)ret;
 }
 
 /*
@@ -989,18 +1006,23 @@ static void remove_parameter(char *line, const char *param_name)
 char *get_command_line(void)
 {
 	FILE *fp;
-	size_t len;
-	char *line = NULL;
+	const int sizeof_line = 1024;

Should they type of sizeof_line be const size_t ?

kexec/kexec.c static void remove_parameter(char *line, const char *param_name) · line 1010

Quoted 1 line
kexec/kexec.c +1 -0
+	char *line = malloc(sizeof_line); /* according to strdup() later */

Could line be char line[1024] ?

kexec/kexec.c static void remove_parameter(char *line, const char *param_name) · line 1016

Quoted 8 lines
kexec/kexec.c +3 -2
 	fp = fopen("/proc/cmdline", "r");
 	if (!fp)
-		die("Could not read /proc/cmdline.");
-	getline(&line, &len, fp);
+		die("Could not open /proc/cmdline.");
+
+	if ( NULL == fgets(line, sizeof(line), fp) ) {

* The reverse of this logic makes more sense to me:

if (fgets(line, sizeof(line), fp) == NULL) {

* Should sizeof(line) be sizeof_line?
Or alternatively line changed from a pointer to an array.

As it stands sizeof(line) == 4 (on x86_32, glibc)

kexec/kexec.c static void remove_parameter(char *line, const char *param_name) · line 1019

Quoted 3 lines
kexec/kexec.c +3 -0
+		die("Can't read /proc/cmdline.");
+
+/* 	getline(&line, &len, fp); */

Again, I don't think this comment is needed,
but if you want to keep it please indent it by one tab and replace the between '/*' and 'getline' with a single space.

kexec/kexec.c static void remove_parameter(char *line, const char *param_name) · line 1019

Quoted 1 line
kexec/kexec.c +0 -0
 	fclose(fp);

fclose looks like it needs to be indented by two tabs.

Quoted 17 lines
kexec/kexec.c +2 -1
+	}
 
 	if (line) {
 		/* strip newline */
-		*(line + strlen(line) - 1) = 0;
+		line[strlen(line) - 1] = '\0';
 
 		remove_parameter(line, "BOOT_IMAGE");
 		if (kexec_flags & KEXEC_ON_CRASH)
Signature · 7 lines
1.6.4.4


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#2 lore raw
Quoted 4 lines · Simon Horman

kexec/kexec.c static void remove_parameter(char *line, const char *param_name) · line 1010

Quoted 2 lines
kexec/kexec.c +1 -0
+	char *line = malloc(sizeof_line); /* according to strdup() later */

Could line be char line[1024] ?

Better would be a constant. However, 1024 is too less since the maximum command line size can be 2048 currently on x86 (arch/x86/include/asm/setup.h, #define COMMAND_LINE_SIZE 2048).

Regards,
Bernhard

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#3 lore raw
Quoted 9 lines · Bernhard Walle
Quoted 4 lines · Simon Horman

Review

Quoted 2 lines
(quoted diff) +1 -0
+	char *line = malloc(sizeof_line); /* according to strdup() later */

Could line be char line[1024] ?

Better would be a constant. However, 1024 is too less since the maximum command line size can be 2048 currently on x86 (arch/x86/include/asm/setup.h, #define COMMAND_LINE_SIZE 2048).

Hi,

I have taken a stab at revising this patch:

----------------------------------------------------------------------

kexec.c: workaround getline and fscanf to make it *libc agnostic.
Tested against klibc and dietlibc.

Based on a patch by Andrea Adami and Yuri Bushmelev I have:

* Cleaned up indentation
* Rearranged the logic in get_command_line()
* Increased the buffer for reading the command line from 1024 to 2048 bytes as this is now possible on x86

Only compile tested against glibc.

Cc Andrea Adami <[email protected]>
Cc Yuri Bushmelev <[email protected]>
Cc Bernhard Walle <[email protected]>
kexec/kexec.c +37 -16
Index: kexec-tools/kexec/kexec.c
===================================================================
@@ -933,15 +933,32 @@ void usage(void)
 
 static int kexec_loaded(void)
 {
-	int ret;
+	long ret = -1;
 	FILE *fp;
+	char *p;
+	char line[3];
 
 	fp = fopen("/sys/kernel/kexec_loaded", "r");
 	if (fp == NULL)
 		return -1;
-	fscanf(fp, "%d", &ret);
+
+	p = fgets(line, sizeof(line), fp);
 	fclose(fp);
-	return ret;
+
+	if ( NULL == p)
+		return -1;
+
+	ret = strtol(line, &p, 10);
+
+	/* Too long */
+	if (ret > INT_MAX)
+		return -1;
+
+	/* No digits were found */
+	if (p == line)
+		return -1;
+
+	return (int)ret;
 }
 
 /*
@@ -989,24 +1006,28 @@ static void remove_parameter(char *line,
 char *get_command_line(void)
 {
 	FILE *fp;
-	size_t len;
-	char *line = NULL;
+	char *line;
+	const int sizeof_line = 2048;
+
+	line = malloc(sizeof_line);
+	if (line == NULL)
+		die("Could not allocate memory to read /proc/cmdline.");
 
 	fp = fopen("/proc/cmdline", "r");
 	if (!fp)
-		die("Could not read /proc/cmdline.");
-	getline(&line, &len, fp);
+		die("Could not open /proc/cmdline.");
+
+	if (fgets(line, sizeof_line, fp) == NULL)
+		die("Can't read /proc/cmdline.");
+
 	fclose(fp);
 
-	if (line) {
-		/* strip newline */
-		*(line + strlen(line) - 1) = 0;
-
-		remove_parameter(line, "BOOT_IMAGE");
-		if (kexec_flags & KEXEC_ON_CRASH)
-			remove_parameter(line, "crashkernel");
-	} else
-		line = strdup("");
+	/* strip newline */
+	line[strlen(line) - 1] = '\0';
+
+	remove_parameter(line, "BOOT_IMAGE");
+	if (kexec_flags & KEXEC_ON_CRASH)
+		remove_parameter(line, "crashkernel");
 
 	return line;
 }

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#4 lore raw

kexec/kexec.c void usage(void) · line 949

Quoted 4 lines · Simon Horman
kexec/kexec.c +3 -0
+
+	if ( NULL == p)
+		return -1;

Wouldn't that be better 'p == NULL'?

kexec/kexec.c void usage(void) · line 955

Quoted 5 lines
kexec/kexec.c +5 -0
+	ret = strtol(line, &p, 10);
+
+	/* Too long */
+	if (ret > INT_MAX)
+		return -1;

An integer that can be represented in 2 (strlen("__\0")==3) cannot be larger than INT_MAX on any platform I can imagine. ;-)

Regards,
Bernhard

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#5 lore raw
Quoted 7 lines · Bernhard Walle

Review

Quoted 4 lines · Simon Horman
(quoted diff) +3 -0
+
+	if ( NULL == p)
+		return -1;

Wouldn't that be better 'p == NULL'?

Agreed

Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	ret = strtol(line, &p, 10);
+
+	/* Too long */
+	if (ret > INT_MAX)
+		return -1;

An integer that can be represented in 2 (strlen("__\0")==3) cannot be larger than INT_MAX on any platform I can imagine. ;-)

True, though it doesn't seem outrageous to check anyway. Perhaps the string format will change in future.

----------------------------------------------------------------------

From: Andrea Adami <[email protected]>
To: [email protected]
Subject: [PATCH] kexec.c: workaround getline and fscanf to make it *libc
agnostic. Tested against klibc and dietlibc.

Based on a patch by Andrea Adami and Yuri Bushmelev I have:

* Cleaned up indentation
* Rearranged the logic in get_command_line()
* Increased the buffer for reading the command line from 1024 to 2048 bytes as this is now possible on x86

Only compile tested against glibc.

Cc Andrea Adami <[email protected]>
Cc Yuri Bushmelev <[email protected]>
Cc Bernhard Walle <[email protected]>
kexec/kexec.c +37 -16
Index: kexec-tools/kexec/kexec.c
===================================================================
@@ -933,15 +933,32 @@ void usage(void)
 
 static int kexec_loaded(void)
 {
-	int ret;
+	long ret = -1;
 	FILE *fp;
+	char *p;
+	char line[3];
 
 	fp = fopen("/sys/kernel/kexec_loaded", "r");
 	if (fp == NULL)
 		return -1;
-	fscanf(fp, "%d", &ret);
+
+	p = fgets(line, sizeof(line), fp);
 	fclose(fp);
-	return ret;
+
+	if (p == NULL)
+		return -1;
+
+	ret = strtol(line, &p, 10);
+
+	/* Too long */
+	if (ret > INT_MAX)
+		return -1;
+
+	/* No digits were found */
+	if (p == line)
+		return -1;
+
+	return (int)ret;
 }
 
 /*
@@ -989,24 +1006,28 @@ static void remove_parameter(char *line,
 char *get_command_line(void)
 {
 	FILE *fp;
-	size_t len;
-	char *line = NULL;
+	char *line;
+	const int sizeof_line = 2048;
+
+	line = malloc(sizeof_line);
+	if (line == NULL)
+		die("Could not allocate memory to read /proc/cmdline.");
 
 	fp = fopen("/proc/cmdline", "r");
 	if (!fp)
-		die("Could not read /proc/cmdline.");
-	getline(&line, &len, fp);
+		die("Could not open /proc/cmdline.");
+
+	if (fgets(line, sizeof_line, fp) == NULL)
+		die("Can't read /proc/cmdline.");
+
 	fclose(fp);
 
-	if (line) {
-		/* strip newline */
-		*(line + strlen(line) - 1) = 0;
-
-		remove_parameter(line, "BOOT_IMAGE");
-		if (kexec_flags & KEXEC_ON_CRASH)
-			remove_parameter(line, "crashkernel");
-	} else
-		line = strdup("");
+	/* strip newline */
+	line[strlen(line) - 1] = '\0';
+
+	remove_parameter(line, "BOOT_IMAGE");
+	if (kexec_flags & KEXEC_ON_CRASH)
+		remove_parameter(line, "crashkernel");
 
 	return line;
 }

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#6 lore raw

Hello!

Quoted 9 lines · Bernhard Walle
Quoted 6 lines

kexec/kexec.c void usage(void) · line 949

Quoted 3 lines · Simon Horman
kexec/kexec.c +3 -0
+
+	if ( NULL == p)
+		return -1;

Wouldn't that be better 'p == NULL'?

Agreed

That is form of 'early bug detection' of mistyped '=='. E.g. you can write by mistype

	if (p = NULL) {}
but can't
	if (NULL = p) {}

because you will get compiler error.

Signature · 6 lines
Yuri Bushmelev

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#7 lore raw
Quoted 8 lines · Yuri Bushmelev

That is form of 'early bug detection' of mistyped '=='. E.g. you can write by mistype

	if (p = NULL) {}
but can't
	if (NULL = p) {}

because you will get compiler error.

I know that kind of argument. However, it makes code IMO quite unreadable since it's uncommon at least in the kernel code. And that's the coding style we follow in kexec.

BTW: gcc warns about 'if (p = NULL)' anyways ...

Regards,
Bernhard

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#8 lore raw
Quoted 10 lines · Bernhard Walle
Quoted 8 lines · Yuri Bushmelev

That is form of 'early bug detection' of mistyped '=='. E.g. you can write by mistype

	if (p = NULL) {}
but can't
	if (NULL = p) {}

because you will get compiler error.

Quoted 5 lines

I know that kind of argument. However, it makes code IMO quite unreadable since it's uncommon at least in the kernel code. And that's the coding style we follow in kexec.

BTW: gcc warns about 'if (p = NULL)' anyways ...

I agree with Bernhard, though Yuri's point is a good one.

This aside, is everyone happy with the patch?
I'd like to merge it if possible.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#10 lore raw
In reply to Simon Horman

Hello!

Quoted 9 lines · Bernhard Walle
Quoted 8 lines
Quoted 6 lines · Yuri Bushmelev

That is form of 'early bug detection' of mistyped '=='. E.g. you can write by mistype

	if (p = NULL) {}
but can't
	if (NULL = p) {}

because you will get compiler error.

Quoted 5 lines
Quoted 5 lines

I know that kind of argument. However, it makes code IMO quite unreadable since it's uncommon at least in the kernel code. And that's the coding style we follow in kexec.

BTW: gcc warns about 'if (p = NULL)' anyways ...

Ok, np :)

Quoted 4 lines

I agree with Bernhard, though Yuri's point is a good one.

This aside, is everyone happy with the patch?
I'd like to merge it if possible.

Merge it please!

Signature · 6 lines
Yuri Bushmelev

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#11 lore raw
Quoted 16 lines · Yuri Bushmelev

Hello!

Quoted 9 lines · Bernhard Walle
Quoted 8 lines
Quoted 6 lines · Yuri Bushmelev

That is form of 'early bug detection' of mistyped '=='. E.g. you can write by mistype

	if (p = NULL) {}
but can't
	if (NULL = p) {}

because you will get compiler error.

Quoted 5 lines
Quoted 5 lines

I know that kind of argument. However, it makes code IMO quite unreadable since it's uncommon at least in the kernel code. And that's the coding style we follow in kexec.

BTW: gcc warns about 'if (p = NULL)' anyways ...

Quoted 8 lines

Ok, np :)

Quoted 4 lines

I agree with Bernhard, though Yuri's point is a good one.

This aside, is everyone happy with the patch?
I'd like to merge it if possible.

Merge it please!

Done :-)

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec