libusbgx: Add error handling to gadget_read_string().
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Mon, 16 Dec 2013 09:45:18 +0000 (10:45 +0100)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 22 Dec 2015 19:33:56 +0000 (20:33 +0100)
Add error handling when gadget_read_buf() returns NULL.
If read of string fails, the string should be set as empty.

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
[Port from libusbg and update description]
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
src/usbg.c

index ca444db..6f752a9 100644 (file)
@@ -123,11 +123,18 @@ static int usbg_read_int(char *path, char *name, char *file, int base)
 
 static void usbg_read_string(char *path, char *name, char *file, char *buf)
 {
-       char *p;
+       char *p = NULL;
+
+       p = usbg_read_buf(path, name, file, buf);
+       /* Check whether read was successful */
+       if (p != NULL) {
+               if ((p = strchr(buf, '\n')) != NULL)
+                               *p = '\0';
+       } else {
+               /* Set this as empty string */
+               *buf = '\0';
+       }
 
-       usbg_read_buf(path, name, file, buf);
-       if ((p = strchr(buf, '\n')) != NULL)
-               *p = '\0';
 }
 
 static void usbg_write_buf(char *path, char *name, char *file, char *buf)
@@ -190,10 +197,14 @@ static void usbg_parse_function_attrs(struct function *f)
        case F_RNDIS:
                usbg_read_string(f->path, f->name, "dev_addr", str_addr);
                addr = ether_aton(str_addr);
-               memcpy(&f->attr.net.dev_addr, addr, 6);
+               if (addr)
+                       memcpy(&f->attr.net.dev_addr, addr, sizeof(struct ether_addr));
+
                usbg_read_string(f->path, f->name, "host_addr", str_addr);
                addr = ether_aton(str_addr);
-               memcpy(&f->attr.net.host_addr, addr, 6);
+               if(addr)
+                       memcpy(&f->attr.net.host_addr, addr, sizeof(struct ether_addr));
+
                usbg_read_string(f->path, f->name, "ifname", f->attr.net.ifname);
                f->attr.net.qmult = usbg_read_dec(f->path, f->name, "qmult");
                break;