[meego-commits] 24012: Changes to devel:resourcepolicy/boardname

tuukka.makinen no_reply at build.meego.com
Tue Sep 20 06:47:12 UTC 2011


Hi,
I have made the following changes to boardname in project devel:resourcepolicy. Please review and accept ASAP.

Thank You,
tuukka.makinen

[This message was auto-generated]

---

Request #24012:

  submit:   home:tuukka.makinen:branches:devel:resourcepolicy/boardname(cleanup) -> devel:resourcepolicy/boardname


Message:
    Boardname file should have just one line so read just one line. Reading in a loop resulted in BMC#22871 anyway. Also prefer static memory to dynamic and malloc only as necessary.

State:   new          2011-09-19T23:36:27 tuukka.makinen
Comment: None



changes files:
--------------
--- boardname.changes
+++ boardname.changes
@@ -0,0 +1,5 @@
+* Thu Sep 08 2011 Tuukka Mäkinen <tuukka.makinen at cybercom.com> - 0.7
+- (BMC#22871) Don't read boardname file in a while loop as it results in error being printed even
+  when there were no problems.
+- Use stack for read buffer and malloc only the actual amount needed.
+

new:
----
  fix_reading.patch
  malloc_as_necessary.patch

spec files:
-----------
--- boardname.spec
+++ boardname.spec
@@ -15,6 +15,8 @@
 Source0:    boardname-%{version}.tar.gz
 Source1:    boardname.service
 Source100:  boardname.yaml
+Patch0:     fix_reading.patch
+Patch1:     malloc_as_necessary
 Requires:   coreutils
 Requires:   grep
 Requires(post): /sbin/ldconfig
@@ -43,6 +45,10 @@
 %prep
 %setup -q -n %{name}-%{version}
 
+# fix_reading.patch
+%patch0 -p1
+# malloc_as_necessary
+%patch1 -p1
 # >> setup
 # << setup
 

other changes:
--------------

++++++ boardname.yaml
--- boardname.yaml
+++ boardname.yaml
@@ -12,7 +12,9 @@
 Sources    : 
     - boardname-%{version}.tar.gz
     - boardname.service
-
+Patches:
+    - fix_reading.patch
+    - malloc_as_necessary
 Configure: none
 Builder: make
 License: GPLv2

++++++ fix_reading.patch (new)
--- fix_reading.patch
+++ fix_reading.patch
+--- a/boardname.c	2011-07-28 00:02:20.000000000 +0300
++++ b/boardname.c	2011-09-08 13:47:04.000000000 +0300
+@@ -15,24 +15,28 @@
+ 
+ #define MAXSTRING 256
+ 
++#define BOARDFILE "/etc/boardname"
++
+ char* getboardname()
+ {
+ 	char *buffer;
+ 	FILE *fd;
+ 
+-	fd = fopen("/etc/boardname", "r");
++	fd = fopen(BOARDFILE, "r");
+ 	if (!fd) {
+-		fprintf(stderr, "Unable to open /etc/boardname.\n");
++		fprintf(stderr, "Unable to open %s\n", BOARDFILE);
+ 
+ 		return NULL;
+ 	}
+ 
+ 	buffer = malloc(MAXSTRING);
+-	while (!feof(fd))
+-		if (fgets(buffer, MAXSTRING, fd) <= 0)
+-			fprintf(stderr, "Unable to read from /etc/boardname.\n");
+ 
+-	fclose(fd);
++	if (fgets(buffer, MAXSTRING, fd) <= 0) {
++		fprintf(stderr, "Unable to read from %s.\n", BOARDFILE);
++		free(buffer);
++		buffer = NULL;
++	}
+ 
++	fclose(fd);
+ 	return buffer;
+ }

++++++ malloc_as_necessary.patch (new)
--- malloc_as_necessary.patch
+++ malloc_as_necessary.patch
+--- a/boardname.c	2011-09-20 09:25:24.000000000 +0300
++++ b/boardname.c	2011-09-20 09:33:18.000000000 +0300
+@@ -10,8 +10,9 @@
+ * of the License.
+ */
+ 
+-#include "stdio.h"
+-#include "stdlib.h"
++#include <stdio.h>
++#include <stdlib.h>
++#include <string.h>
+ 
+ #define MAXSTRING 256
+ 
+@@ -19,7 +20,8 @@
+ 
+ char* getboardname()
+ {
+-	char *buffer;
++	char buffer[MAXSTRING];
++	char *pbuffer = NULL;
+ 	FILE *fd;
+ 
+ 	fd = fopen(BOARDFILE, "r");
+@@ -29,14 +31,12 @@
+ 		return NULL;
+ 	}
+ 
+-	buffer = malloc(MAXSTRING);
+-
+-	if (fgets(buffer, MAXSTRING, fd) <= 0) {
++	if (fgets(&buffer[0], MAXSTRING, fd) ) {
++		pbuffer = strdup(&buffer[0]);
++	} else {
+ 		fprintf(stderr, "Unable to read from %s.\n", BOARDFILE);
+-		free(buffer);
+-		buffer = NULL;
+ 	}
+ 
+ 	fclose(fd);
+-	return buffer;
++	return pbuffer;
+ }



More information about the MeeGo-commits mailing list