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.opas...@samsung.com>
---
Changes since v2:
        - replace // comments with /* */
        - add missing parenthesis in commit message

 src/gadget.c |   23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/gadget.c b/src/gadget.c
index f613c3e..6f0c84a 100644
--- a/src/gadget.c
+++ b/src/gadget.c
@@ -121,11 +121,18 @@ static int gadget_read_int(char *path, char *name, char 
*file, int base)
 
 static void gadget_read_string(char *path, char *name, char *file, char *buf)
 {
-       char *p;
+       char *p = NULL;
+
+       p = gadget_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';
+       }
 
-       gadget_read_buf(path, name, file, buf);
-       if ((p = strchr(buf, '\n')) != NULL)
-               *p = '\0';
 }
 
 static void gadget_write_buf(char *path, char *name, char *file, char *buf)
@@ -185,10 +192,14 @@ static void gadget_parse_function_attrs(struct function 
*f)
        case F_RNDIS:
                gadget_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));
+
                gadget_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));
+
                gadget_read_string(f->path, f->name, "ifname", 
f->attr.net.ifname);
                f->attr.net.qmult = gadget_read_dec(f->path, f->name, "qmult");
                break;
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to