diff mbox

lib: fwts_alloc: only parse mem info once we have a valid line read in

Message ID 20170605134040.6611-1-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King June 5, 2017, 1:40 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

It has been observed that the sscanf without the expected input format can
make the scanning loop spin forever. The easiest way around is to read
in entire lines and sscan each line so that way we at least hit the end of
file marker no matter what.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_alloc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Alex Hung June 6, 2017, 6:11 a.m. UTC | #1
On 2017-06-05 06:40 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> It has been observed that the sscanf without the expected input format can
> make the scanning loop spin forever. The easiest way around is to read
> in entire lines and sscan each line so that way we at least hit the end of
> file marker no matter what.
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_alloc.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/src/lib/src/fwts_alloc.c b/src/lib/src/fwts_alloc.c
> index c0d5e555..e65b181e 100644
> --- a/src/lib/src/fwts_alloc.c
> +++ b/src/lib/src/fwts_alloc.c
> @@ -234,6 +234,7 @@ static void *fwts_low_mmap(const size_t requested_size)
>   {
>   	FILE *fp;
>   	char pathname[1024];
> +	char buffer[4096];
>   	void *addr_start;
>   	void *addr_end;
>   	void *last_addr_end = NULL;
> @@ -251,8 +252,8 @@ static void *fwts_low_mmap(const size_t requested_size)
>   	if (!fp)
>   		return fwts_low_mmap_walkdown(requested_size);
>   
> -	while (!feof(fp)) {
> -		if (fscanf(fp, "%p-%p %*s %*x %*s %*u %1023s\n",
> +	while (fgets(buffer, sizeof(buffer) - 1, fp)) {
> +		if (sscanf(buffer, "%p-%p %*s %*x %*s %*u %1023s\n",
>   		    &addr_start, &addr_end, pathname) != 3)
>   			continue;
>   		/*
> 

Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu June 9, 2017, 9:26 a.m. UTC | #2
On 06/05/2017 09:40 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> It has been observed that the sscanf without the expected input format can
> make the scanning loop spin forever. The easiest way around is to read
> in entire lines and sscan each line so that way we at least hit the end of
> file marker no matter what.
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_alloc.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/src/lib/src/fwts_alloc.c b/src/lib/src/fwts_alloc.c
> index c0d5e555..e65b181e 100644
> --- a/src/lib/src/fwts_alloc.c
> +++ b/src/lib/src/fwts_alloc.c
> @@ -234,6 +234,7 @@ static void *fwts_low_mmap(const size_t requested_size)
>   {
>   	FILE *fp;
>   	char pathname[1024];
> +	char buffer[4096];
>   	void *addr_start;
>   	void *addr_end;
>   	void *last_addr_end = NULL;
> @@ -251,8 +252,8 @@ static void *fwts_low_mmap(const size_t requested_size)
>   	if (!fp)
>   		return fwts_low_mmap_walkdown(requested_size);
>   
> -	while (!feof(fp)) {
> -		if (fscanf(fp, "%p-%p %*s %*x %*s %*u %1023s\n",
> +	while (fgets(buffer, sizeof(buffer) - 1, fp)) {
> +		if (sscanf(buffer, "%p-%p %*s %*x %*s %*u %1023s\n",
>   		    &addr_start, &addr_end, pathname) != 3)
>   			continue;
>   		/*
> 

Acked-by: Ivan Hu <ivan.hu@canonical.com>
diff mbox

Patch

diff --git a/src/lib/src/fwts_alloc.c b/src/lib/src/fwts_alloc.c
index c0d5e555..e65b181e 100644
--- a/src/lib/src/fwts_alloc.c
+++ b/src/lib/src/fwts_alloc.c
@@ -234,6 +234,7 @@  static void *fwts_low_mmap(const size_t requested_size)
 {
 	FILE *fp;
 	char pathname[1024];
+	char buffer[4096];
 	void *addr_start;
 	void *addr_end;
 	void *last_addr_end = NULL;
@@ -251,8 +252,8 @@  static void *fwts_low_mmap(const size_t requested_size)
 	if (!fp)
 		return fwts_low_mmap_walkdown(requested_size);
 
-	while (!feof(fp)) {
-		if (fscanf(fp, "%p-%p %*s %*x %*s %*u %1023s\n",
+	while (fgets(buffer, sizeof(buffer) - 1, fp)) {
+		if (sscanf(buffer, "%p-%p %*s %*x %*s %*u %1023s\n",
 		    &addr_start, &addr_end, pathname) != 3)
 			continue;
 		/*