libusbgx: Remove static buffers from usbg_f_net_attrs
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Wed, 18 Mar 2015 11:39:24 +0000 (12:39 +0100)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 22 Dec 2015 20:45:35 +0000 (21:45 +0100)
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Reviewed-by: Pawel Szewczyk <p.szewczyk@samsung.com>
Reviewed-by: Philippe De Swert <philippedeswert@gmail.com>
include/usbg/usbg.h
src/usbg.c

index f2b3668..e0a651e 100644 (file)
@@ -49,6 +49,8 @@ extern "C" {
 #define USBG_MAX_NAME_LENGTH 40
 /* Dev name for ffs is a part of function name, we subtracs 4 char for "ffs." */
 #define USBG_MAX_DEV_LENGTH (USBG_MAX_NAME_LENGTH - 4)
+/* ConfigFS just like SysFS uses page size as max size of file content */
+#define USBG_MAX_FILE_SIZE 4096
 
 /**
  * @brief Additional option for usbg_rm_* functions.
@@ -196,7 +198,7 @@ typedef struct {
 typedef struct {
        struct ether_addr dev_addr;
        struct ether_addr host_addr;
-       char ifname[USBG_MAX_STR_LENGTH];
+       char *ifname;
        int qmult;
 } usbg_f_net_attrs;
 
index b3c4c24..20cae9f 100644 (file)
@@ -444,6 +444,28 @@ static int usbg_read_string(const char *path, const char *name,
        return ret;
 }
 
+static int usbg_read_string_alloc(const char *path, const char *name,
+                              const char *file, char **dest)
+{
+       char buf[USBG_MAX_FILE_SIZE];
+       char *new_buf = NULL;
+       int ret = USBG_SUCCESS;
+
+       ret = usbg_read_string(path, name, file, buf);
+       if (ret != USBG_SUCCESS)
+               goto out;
+
+       new_buf = strdup(buf);
+       if (!new_buf) {
+               ret = USBG_ERROR_NO_MEM;
+               goto out;
+       }
+
+       *dest = new_buf;
+out:
+       return ret;
+}
+
 static int usbg_write_buf(const char *path, const char *name, const char *file,
                          const char *buf)
 {
@@ -821,12 +843,12 @@ static int usbg_parse_function_net_attrs(usbg_function *f,
                goto out;
        }
 
-       ret = usbg_read_string(f->path, f->name, "ifname", f_net_attrs->ifname);
+       ret = usbg_read_dec(f->path, f->name, "qmult", &(f_net_attrs->qmult));
        if (ret != USBG_SUCCESS)
                goto out;
 
-       ret = usbg_read_dec(f->path, f->name, "qmult", &(f_net_attrs->qmult));
-
+       ret = usbg_read_string_alloc(f->path, f->name, "ifname",
+                                    &(f_net_attrs->ifname));
 out:
        return ret;
 }
@@ -2591,6 +2613,8 @@ void usbg_cleanup_function_attrs(usbg_function_attrs *f_attrs)
                break;
 
        case USBG_F_ATTRS_NET:
+               free(f_attrs->attrs.net.ifname);
+               f_attrs->attrs.net.ifname = NULL;
                break;
 
        case USBG_F_ATTRS_PHONET:
@@ -2611,7 +2635,7 @@ int usbg_set_function_net_attrs(usbg_function *f, const usbg_f_net_attrs *attrs)
        char *addr;
 
        /* ifname is read only so we accept only empty string for this param */
-       if (attrs->ifname[0]) {
+       if (attrs->ifname && attrs->ifname[0]) {
                ret = USBG_ERROR_INVALID_PARAM;
                goto out;
        }