FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 11-18-2009, 02:11 AM
David Cantrell
 
Default Clean up initProductInfo() in loader.c.

Use g_file_get_contents() and g_strsplit() to read in .buildstamp data and
split it up. Remove static buffers.
---
loader/loader.c | 82 ++++++++++++++++++++++++++++++-------------------------
1 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/loader/loader.c b/loader/loader.c
index 0ea57cb..270c0e8 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -251,51 +251,59 @@ void stopNewt(void) {
newtRunning = 0;
}

-static char * productName = NULL;
-static char * productPath = NULL;
-static char * productArch = NULL;
-static char * productStamp = NULL;
+static gchar *productName = NULL;
+static gchar *productPath = NULL;
+static gchar *productArch = NULL;

static void initProductInfo(void) {
- FILE *f;
- int i;
+ gchar *contents = NULL;
+ gchar **lines = NULL, **stamp = NULL;
+ GError *fileErr = NULL;
+
+ if (!g_file_get_contents("/.buildstamp", &contents, NULL, &fileErr)) {
+ logMessage(ERROR, "error reading .buildstamp: %s", fileErr->message);
+ g_error_free(fileErr);
+ productName = g_strdup("anaconda");
+ productArch = g_strdup("unknown architecture");
+ productPath = g_strdup("anaconda");
+ return;
+ }

- f = fopen("/.buildstamp", "r");
- if (!f) {
- productName = strdup("anaconda");
- productPath = strdup("anaconda");
- } else {
- productStamp = malloc(256);
- productName = malloc(256);
- productPath = malloc(256);
- productStamp = fgets(productStamp, 256, f); /* stamp time and architecture */
- productArch = strstr(productStamp, "."); /* architecture is separated by dot */
- if(productArch) productArch++;
-
- productName = fgets(productName, 256, f); /* product name */
- productPath = fgets(productPath, 256, f); /* product version */
- productPath = fgets(productPath, 256, f); /* product path */
-
- i = strlen(productName) - 1;
- while (isspace(*(productName + i))) {
- *(productName + i) = '';
- i--;
- }
- i = strlen(productPath) - 1;
- while (isspace(*(productPath + i))) {
- *(productPath + i) = '';
- i--;
+ /* .buildstamp uses the first 3 lines in this format:
+ * STAMP.productArch
+ * productName
+ * productPath
+ */
+ lines = g_strsplit(contents, "
", 0);
+ g_free(contents);
+
+ if (g_strv_length(lines) >= 3) {
+ /* STAMP.productArch */
+ stamp = g_strsplit(lines[0], ".", 0);
+
+ if (g_strv_length(stamp) == 2) {
+ productArch = g_strdup(stamp[1]);
+ } else {
+ productArch = g_strdup("unknown architecture");
}
- i = strlen(productArch) - 1;
- while (isspace(*(productArch + i))) {
- *(productArch + i) = '';
- i--;
+
+ if (stamp) {
+ g_strfreev(stamp);
}
+
+ productName = g_strdup(lines[1]);
+ productPath = g_strdup(lines[2]);
+ } else {
+ productName = g_strdup("anaconda");
+ productArch = g_strdup("unknown architecture");
+ productPath = g_strdup("anaconda");
}

- if(!productArch) productArch = strdup("unknown architecture");
+ if (lines) {
+ g_strfreev(lines);
+ }

- fclose(f);
+ return;
}

char * getProductName(void) {
--
1.6.5.2

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 11-18-2009, 09:53 AM
Hans de Goede
 
Default Clean up initProductInfo() in loader.c.

Hi,

On 11/18/2009 04:11 AM, David Cantrell wrote:

Use g_file_get_contents() and g_strsplit() to read in .buildstamp data and
split it up. Remove static buffers.
---
loader/loader.c | 82 ++++++++++++++++++++++++++++++-------------------------
1 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/loader/loader.c b/loader/loader.c
index 0ea57cb..270c0e8 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -251,51 +251,59 @@ void stopNewt(void) {
newtRunning = 0;
}

-static char * productName = NULL;
-static char * productPath = NULL;
-static char * productArch = NULL;
-static char * productStamp = NULL;
+static gchar *productName = NULL;
+static gchar *productPath = NULL;
+static gchar *productArch = NULL;

static void initProductInfo(void) {
- FILE *f;
- int i;
+ gchar *contents = NULL;
+ gchar **lines = NULL, **stamp = NULL;
+ GError *fileErr = NULL;
+
+ if (!g_file_get_contents("/.buildstamp",&contents, NULL,&fileErr)) {
+ logMessage(ERROR, "error reading .buildstamp: %s", fileErr->message);
+ g_error_free(fileErr);
+ productName = g_strdup("anaconda");
+ productArch = g_strdup("unknown architecture");
+ productPath = g_strdup("anaconda");
+ return;
+ }

- f = fopen("/.buildstamp", "r");
- if (!f) {
- productName = strdup("anaconda");
- productPath = strdup("anaconda");
- } else {
- productStamp = malloc(256);
- productName = malloc(256);
- productPath = malloc(256);
- productStamp = fgets(productStamp, 256, f); /* stamp time and architecture */
- productArch = strstr(productStamp, "."); /* architecture is separated by dot */
- if(productArch) productArch++;
-
- productName = fgets(productName, 256, f); /* product name */
- productPath = fgets(productPath, 256, f); /* product version */
- productPath = fgets(productPath, 256, f); /* product path */
-
- i = strlen(productName) - 1;
- while (isspace(*(productName + i))) {
- *(productName + i) = '';
- i--;
- }
- i = strlen(productPath) - 1;
- while (isspace(*(productPath + i))) {
- *(productPath + i) = '';
- i--;
+ /* .buildstamp uses the first 3 lines in this format:
+ * STAMP.productArch
+ * productName
+ * productPath
+ */
+ lines = g_strsplit(contents, "
", 0);
+ g_free(contents);
+
+ if (g_strv_length(lines)>= 3) {
+ /* STAMP.productArch */
+ stamp = g_strsplit(lines[0], ".", 0);
+
+ if (g_strv_length(stamp) == 2) {
+ productArch = g_strdup(stamp[1]);
+ } else {
+ productArch = g_strdup("unknown architecture");
}
- i = strlen(productArch) - 1;
- while (isspace(*(productArch + i))) {
- *(productArch + i) = '';
- i--;
+
+ if (stamp) {
+ g_strfreev(stamp);
}
+
+ productName = g_strdup(lines[1]);
+ productPath = g_strdup(lines[2]);
+ } else {
+ productName = g_strdup("anaconda");
+ productArch = g_strdup("unknown architecture");
+ productPath = g_strdup("anaconda");
}

- if(!productArch) productArch = strdup("unknown architecture");
+ if (lines) {
+ g_strfreev(lines);
+ }



Hmm, what does the "g_strv_length(lines)" above do if lines == NULL ?

If that will just return 0 (instead of crash), then ack.

Regards,

Hans

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 11-18-2009, 03:35 PM
David Cantrell
 
Default Clean up initProductInfo() in loader.c.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 18 Nov 2009, Hans de Goede wrote:


Hi,

On 11/18/2009 04:11 AM, David Cantrell wrote:

Use g_file_get_contents() and g_strsplit() to read in .buildstamp data and
split it up. Remove static buffers.
---
loader/loader.c | 82
++++++++++++++++++++++++++++++-------------------------

1 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/loader/loader.c b/loader/loader.c
index 0ea57cb..270c0e8 100644
--- a/loader/loader.c
+++ b/loader/loader.c
@@ -251,51 +251,59 @@ void stopNewt(void) {
newtRunning = 0;
}

-static char * productName = NULL;
-static char * productPath = NULL;
-static char * productArch = NULL;
-static char * productStamp = NULL;
+static gchar *productName = NULL;
+static gchar *productPath = NULL;
+static gchar *productArch = NULL;

static void initProductInfo(void) {
- FILE *f;
- int i;
+ gchar *contents = NULL;
+ gchar **lines = NULL, **stamp = NULL;
+ GError *fileErr = NULL;
+
+ if (!g_file_get_contents("/.buildstamp",&contents, NULL,&fileErr)) {
+ logMessage(ERROR, "error reading .buildstamp: %s",
fileErr->message);

+ g_error_free(fileErr);
+ productName = g_strdup("anaconda");
+ productArch = g_strdup("unknown architecture");
+ productPath = g_strdup("anaconda");
+ return;
+ }

- f = fopen("/.buildstamp", "r");
- if (!f) {
- productName = strdup("anaconda");
- productPath = strdup("anaconda");
- } else {
- productStamp = malloc(256);
- productName = malloc(256);
- productPath = malloc(256);
- productStamp = fgets(productStamp, 256, f); /* stamp time and
architecture */
- productArch = strstr(productStamp, "."); /* architecture is
separated by dot */

- if(productArch) productArch++;
-
- productName = fgets(productName, 256, f); /* product name */
- productPath = fgets(productPath, 256, f); /* product version */
- productPath = fgets(productPath, 256, f); /* product path */
-
- i = strlen(productName) - 1;
- while (isspace(*(productName + i))) {
- *(productName + i) = '';
- i--;
- }
- i = strlen(productPath) - 1;
- while (isspace(*(productPath + i))) {
- *(productPath + i) = '';
- i--;
+ /* .buildstamp uses the first 3 lines in this format:
+ * STAMP.productArch
+ * productName
+ * productPath
+ */
+ lines = g_strsplit(contents, "
", 0);
+ g_free(contents);
+
+ if (g_strv_length(lines)>= 3) {
+ /* STAMP.productArch */
+ stamp = g_strsplit(lines[0], ".", 0);
+
+ if (g_strv_length(stamp) == 2) {
+ productArch = g_strdup(stamp[1]);
+ } else {
+ productArch = g_strdup("unknown architecture");
}
- i = strlen(productArch) - 1;
- while (isspace(*(productArch + i))) {
- *(productArch + i) = '';
- i--;
+
+ if (stamp) {
+ g_strfreev(stamp);
}
+
+ productName = g_strdup(lines[1]);
+ productPath = g_strdup(lines[2]);
+ } else {
+ productName = g_strdup("anaconda");
+ productArch = g_strdup("unknown architecture");
+ productPath = g_strdup("anaconda");
}

- if(!productArch) productArch = strdup("unknown architecture");
+ if (lines) {
+ g_strfreev(lines);
+ }



Hmm, what does the "g_strv_length(lines)" above do if lines == NULL ?

If that will just return 0 (instead of crash), then ack.



glib will warn you that you passed NULL to g_strv_length(), but return 0.
Here's some example code:

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>

int main(void) {
gchar **lines = NULL;
printf("# of lines: %d
", g_strv_length(lines));
return EXIT_SUCCESS;
}

Compiled and run:

dcantrel@mitre ~$ gcc -O0 -Wall -g $(pkg-config --cflags glib-2.0) test1.c
$(pkg-config --libs glib-2.0)
dcantrel@mitre ~$ ./a.out

(process:4814): GLib-CRITICAL **: g_strv_length: assertion `str_array != NULL'
failed
# of lines: 0
dcantrel@mitre ~$ ./a.out 2>/dev/null
# of lines: 0

- --
David Cantrell <dcantrell@redhat.com>

Red Hat / Honolulu, HI

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEARECAAYFAksEIlAACgkQ5hsjjIy1VklkywCgusTNDIB7h4 QL1zFzX5Xk8Rml
FVAAoLb+wVDq8wahyECvZMr2gMKWfiS/
=1iiz
-----END PGP SIGNATURE-----

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 

Thread Tools




All times are GMT. The time now is 01:14 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org