Re: [PATCH 2/3] Clean up initProductInfo() in loader.c.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-----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) = '\0';
-            i--;
-        }
-        i = strlen(productPath) - 1;
-        while (isspace(*(productPath + i))) {
-            *(productPath + i) = '\0';
-            i--;
+    /* .buildstamp uses the first 3 lines in this format:
+     *     STAMP.productArch
+     *     productName
+     *     productPath
+     */
+    lines = g_strsplit(contents, "\n", 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) = '\0';
-            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\n", 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@xxxxxxxxxx>
Red Hat / Honolulu, HI

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

iEYEARECAAYFAksEIlAACgkQ5hsjjIy1VklkywCgusTNDIB7h4QL1zFzX5Xk8Rml
FVAAoLb+wVDq8wahyECvZMr2gMKWfiS/
=1iiz
-----END PGP SIGNATURE-----

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

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux