Pārlūkot izejas kodu

[PAL] Fix parse_stream_uri and parse_device_uri

Those functions didn't match their documentation (the prefix wasn't separated and a full URI was always returned).
Michał Kowalczyk 6 gadi atpakaļ
vecāks
revīzija
b3ea149df9

+ 20 - 10
Pal/src/db_streams.c

@@ -72,8 +72,8 @@ const struct handle_ops * pal_handle_ops [PAL_HANDLE_TYPE_BOUND] = {
 
 /* parse_stream_uri scan the uri, seperate prefix and search for
    stream handler which will open or access the stream */
-int parse_stream_uri (const char ** uri, const char ** prefix,
-                      struct handle_ops ** ops)
+static int parse_stream_uri(const char ** uri, char ** prefix,
+                            struct handle_ops ** ops)
 {
     const char * p, * u = (*uri);
 
@@ -126,8 +126,12 @@ int parse_stream_uri (const char ** uri, const char ** prefix,
 
     *uri = p + 1;
 
-    if (prefix)
-        *prefix = u;
+    if (prefix) {
+        *prefix = malloc_copy(u, p - u + 1);
+        if (!*prefix)
+            return -PAL_ERROR_NOMEM;
+        (*prefix)[p - u] = '\0';
+    }
 
     if (ops)
         *ops = hops;
@@ -142,7 +146,7 @@ int _DkStreamOpen (PAL_HANDLE * handle, const char * uri,
                    int access, int share, int create, int options)
 {
     struct handle_ops * ops = NULL;
-    const char * type = NULL;
+    char* type = NULL;
 
     log_stream(uri);
 
@@ -152,7 +156,9 @@ int _DkStreamOpen (PAL_HANDLE * handle, const char * uri,
         return ret;
 
     assert(ops && ops->open);
-    return ops->open(handle, type, uri, access, share, create, options);
+    ret = ops->open(handle, type, uri, access, share, create, options);
+    free(type);
+    return ret;
 }
 
 /* PAL call DkStreamOpen: Open stream based on uri, as given access/share/
@@ -370,9 +376,9 @@ DkStreamWrite (PAL_HANDLE handle, PAL_NUM offset, PAL_NUM count,
 int _DkStreamAttributesQuery (const char * uri, PAL_STREAM_ATTR * attr)
 {
     struct handle_ops * ops = NULL;
-    const char * type = NULL;
+    char* type = NULL;
 
-    int ret = parse_stream_uri (&uri, &type, &ops);
+    int ret = parse_stream_uri(&uri, &type, &ops);
 
     if (ret < 0)
         return ret;
@@ -380,7 +386,9 @@ int _DkStreamAttributesQuery (const char * uri, PAL_STREAM_ATTR * attr)
     if (!ops->attrquery)
         return -PAL_ERROR_NOTSUPPORT;
 
-    return ops->attrquery(type, uri, attr);
+    ret = ops->attrquery(type, uri, attr);
+    free(type);
+    return ret;
 }
 
 /* PAL call DkStreamAttributeQuery: query attribute of a stream by its
@@ -759,7 +767,7 @@ PAL_BOL DkStreamChangeName (PAL_HANDLE hdl, PAL_STR uri)
     ENTER_PAL_CALL(DkStreamChangeName);
 
     struct handle_ops * ops = NULL;
-    const char * type = NULL;
+    char* type = NULL;
     int ret;
 
     if (uri) {
@@ -774,11 +782,13 @@ PAL_BOL DkStreamChangeName (PAL_HANDLE hdl, PAL_STR uri)
     const struct handle_ops * hops = HANDLE_OPS(hdl);
 
     if (!hops || !hops->rename || (ops && hops != ops)) {
+        free(type);
         _DkRaiseFailure(PAL_ERROR_NOTSUPPORT);
         LEAVE_PAL_CALL_RETURN(PAL_FALSE);
     }
 
     ret = hops->rename(hdl, type, uri);
+    free(type);
 
     if (ret < 0) {
         _DkRaiseFailure(-ret);

+ 14 - 9
Pal/src/host/FreeBSD/db_devices.c

@@ -57,10 +57,9 @@ static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
             &term_ops,
         };
 
-/* parse-device_uri scan the uri, parse the prefix of the uri and search
-   for stream handler wich will open or access the device */
-static int parse_device_uri (const char ** uri, const char ** type,
-                             struct handle_ops ** ops)
+/* parse_device_uri scans the uri, parses the prefix of the uri and searches
+   for stream handler wich will open or access the device. */
+static int parse_device_uri(const char ** uri, char ** type, struct handle_ops ** ops)
 {
     struct handle_ops * dops = NULL;
     const char * p, * u = (*uri);
@@ -74,8 +73,12 @@ static int parse_device_uri (const char ** uri, const char ** type,
         return -PAL_ERROR_NOTSUPPORT;
 
     *uri = (*p) ? p + 1 : p;
-    if (type)
-        *type = u;
+    if (type) {
+        *type = malloc_copy(u, p - u + 1);
+        if (!*type)
+            return -PAL_ERROR_NOMEM;
+        (*type)[p - u] = '\0';
+    }
     if (ops)
         *ops = dops;
     return 0;
@@ -217,7 +220,7 @@ static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
                      int access, int share, int create, int options)
 {
     struct handle_ops * ops = NULL;
-    const char * dev_type = NULL;
+    char * dev_type = NULL;
     int ret = 0;
 
     ret = parse_device_uri(&uri, &dev_type, &ops);
@@ -233,8 +236,10 @@ static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
     hdl->dev.fd_out = PAL_IDX_POISON;
     *handle = hdl;
 
-    return ops->open(handle, dev_type, uri,
-                     access, share, create, options);
+    ret = ops->open(handle, dev_type, uri,
+                    access, share, create, options);
+    free(dev_type);
+    return ret;
 }
 
 /* 'read' operation for device stream */

+ 3 - 3
Pal/src/host/FreeBSD/db_pipes.c

@@ -306,7 +306,7 @@ static int pipe_private (PAL_HANDLE * handle, int options)
 static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
                       int access, int share, int create, int options)
 {
-    if (strpartcmp_static(type, "pipe:") && !*uri)
+    if (strcmp_static(type, "pipe") && !*uri)
         return pipe_private(handle, options);
 
     char * endptr;
@@ -317,10 +317,10 @@ static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
 
     options = HOST_OPTIONS(options & PAL_OPTION_MASK);
 
-    if (strpartcmp_static(type, "pipe.srv:"))
+    if (strcmp_static(type, "pipe.srv"))
         return pipe_listen(handle, pipeid, options);
 
-    if (strpartcmp_static(type, "pipe:"))
+    if (strcmp_static(type, "pipe"))
         return pipe_connect(handle, pipeid, options);
 
     return -PAL_ERROR_INVAL;

+ 4 - 4
Pal/src/host/FreeBSD/db_sockets.c

@@ -548,10 +548,10 @@ static int tcp_open (PAL_HANDLE *handle, const char * type, const char * uri,
     char uri_buf[PAL_SOCKADDR_SIZE];
     memcpy(uri_buf, uri, uri_len);
 
-    if (strpartcmp_static(type, "tcp.srv:"))
+    if (strcmp_static(type, "tcp.srv"))
         return tcp_listen(handle, uri_buf, options);
 
-    if (strpartcmp_static(type, "tcp:"))
+    if (strcmp_static(type, "tcp"))
         return tcp_connect(handle, uri_buf, options);
 
     return -PAL_ERROR_NOTSUPPORT;
@@ -770,10 +770,10 @@ static int udp_open (PAL_HANDLE *hdl, const char * type, const char * uri,
     memcpy(buf, uri, len + 1);
     options &= PAL_OPTION_MASK;
 
-    if (!strpartcmp_static(type, "udp.srv:"))
+    if (!strcmp_static(type, "udp.srv"))
         return udp_bind(hdl, buf, options);
 
-    if (!strpartcmp_static(type, "udp:"))
+    if (!strcmp_static(type, "udp"))
         return udp_connect(hdl, buf, options);
 
     return -PAL_ERROR_NOTSUPPORT;

+ 18 - 11
Pal/src/host/Linux-SGX/db_devices.c

@@ -58,10 +58,9 @@ static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
             &term_ops,
         };
 
-/* parse-device_uri scan the uri, parse the prefix of the uri and search
-   for stream handler wich will open or access the device */
-static int parse_device_uri (const char ** uri, const char ** type,
-                             struct handle_ops ** ops)
+/* parse_device_uri scans the uri, parses the prefix of the uri and searches
+   for stream handler wich will open or access the device. */
+static int parse_device_uri(const char ** uri, char ** type, struct handle_ops ** ops)
 {
     struct handle_ops * dops = NULL;
     const char * p, * u = (*uri);
@@ -75,8 +74,12 @@ static int parse_device_uri (const char ** uri, const char ** type,
         return -PAL_ERROR_NOTSUPPORT;
 
     *uri = (*p) ? p + 1 : p;
-    if (type)
-        *type = u;
+    if (type) {
+        *type = malloc_copy(u, p - u + 1);
+        if (!*type)
+            return -PAL_ERROR_NOMEM;
+        (*type)[p - u] = '\0';
+    }
     if (ops)
         *ops = dops;
     return 0;
@@ -216,7 +219,7 @@ static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
                      int access, int share, int create, int options)
 {
     struct handle_ops * ops = NULL;
-    const char * dev_type = NULL;
+    char* dev_type = NULL;
     int ret = 0;
 
     ret = parse_device_uri(&uri, &dev_type, &ops);
@@ -233,8 +236,10 @@ static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
     hdl->dev.fd_out = PAL_IDX_POISON;
     *handle = hdl;
 
-    return ops->open(handle, dev_type, uri,
-                     access, share, create, options);
+    ret = ops->open(handle, dev_type, uri,
+                    access, share, create, options);
+    free(dev_type);
+    return ret;
 }
 
 /* 'read' operation for device stream */
@@ -342,7 +347,7 @@ static int dev_attrquery (const char * type, const char * uri,
                           PAL_STREAM_ATTR * attr)
 {
     struct handle_ops * ops = NULL;
-    const char * dev_type = NULL;
+    char* dev_type = NULL;
     int ret = 0;
 
     ret = parse_device_uri(&uri, &dev_type, &ops);
@@ -353,7 +358,9 @@ static int dev_attrquery (const char * type, const char * uri,
     if (!ops || !ops->attrquery)
         return -PAL_ERROR_NOTSUPPORT;
 
-    return ops->attrquery(dev_type, uri, attr);
+    ret = ops->attrquery(dev_type, uri, attr);
+    free(dev_type);
+    return ret;
 }
 
 /* 'attrquerybyhdl' operation for device stream */

+ 3 - 3
Pal/src/host/Linux-SGX/db_pipes.c

@@ -170,7 +170,7 @@ static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
 {
     options &= PAL_OPTION_MASK;
 
-    if (strpartcmp_static(type, "pipe:") && !*uri)
+    if (strcmp_static(type, "pipe") && !*uri)
         return pipe_private(handle, options);
 
     char * endptr;
@@ -179,10 +179,10 @@ static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
     if (*endptr)
         return -PAL_ERROR_INVAL;
 
-    if (strpartcmp_static(type, "pipe.srv:"))
+    if (strcmp_static(type, "pipe.srv"))
         return pipe_listen(handle, pipeid, options);
 
-    if (strpartcmp_static(type, "pipe:"))
+    if (strcmp_static(type, "pipe"))
         return pipe_connect(handle, pipeid, options);
 
     return -PAL_ERROR_INVAL;

+ 4 - 4
Pal/src/host/Linux-SGX/db_sockets.c

@@ -464,10 +464,10 @@ static int tcp_open (PAL_HANDLE *handle, const char * type, const char * uri,
     char uri_buf[PAL_SOCKADDR_SIZE];
     memcpy(uri_buf, uri, uri_len);
 
-    if (strpartcmp_static(type, "tcp.srv:"))
+    if (strcmp_static(type, "tcp.srv"))
         return tcp_listen(handle, uri_buf, options);
 
-    if (strpartcmp_static(type, "tcp:"))
+    if (strcmp_static(type, "tcp"))
         return tcp_connect(handle, uri_buf, options);
 
     return -PAL_ERROR_NOTSUPPORT;
@@ -620,10 +620,10 @@ static int udp_open (PAL_HANDLE *hdl, const char * type, const char * uri,
     memcpy(buf, uri, len + 1);
     options &= PAL_OPTION_MASK;
 
-    if (strpartcmp_static(type, "udp.srv:"))
+    if (strcmp_static(type, "udp.srv"))
         return udp_bind(hdl, buf, options);
 
-    if (strpartcmp_static(type, "udp:"))
+    if (strcmp_static(type, "udp"))
         return udp_connect(hdl, buf, options);
 
     return -PAL_ERROR_NOTSUPPORT;

+ 17 - 9
Pal/src/host/Linux/db_devices.c

@@ -59,9 +59,9 @@ static const struct handle_ops* pal_device_ops[PAL_DEVICE_TYPE_BOUND] = {
     NULL, &term_ops,
 };
 
-/* parse-device_uri scan the uri, parse the prefix of the uri and search
-   for stream handler wich will open or access the device */
-static int parse_device_uri(const char** uri, const char** type, struct handle_ops** ops) {
+/* parse_device_uri scans the uri, parses the prefix of the uri and searches
+   for stream handler wich will open or access the device. */
+static int parse_device_uri(const char** uri, char** type, struct handle_ops** ops) {
     struct handle_ops* dops = NULL;
     const char *p, *u = (*uri);
 
@@ -75,8 +75,12 @@ static int parse_device_uri(const char** uri, const char** type, struct handle_o
         return -PAL_ERROR_NOTSUPPORT;
 
     *uri = (*p) ? p + 1 : p;
-    if (type)
-        *type = u;
+    if (type) {
+        *type = malloc_copy(u, p - u + 1);
+        if (!*type)
+            return -PAL_ERROR_NOMEM;
+        (*type)[p - u] = '\0';
+    }
     if (ops)
         *ops = dops;
     return 0;
@@ -202,7 +206,7 @@ static int64_t char_write(PAL_HANDLE handle, uint64_t offset, uint64_t size, con
 static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
                     int create, int options) {
     struct handle_ops* ops = NULL;
-    const char* dev_type   = NULL;
+    char* dev_type   = NULL;
     int ret                = 0;
 
     ret = parse_device_uri(&uri, &dev_type, &ops);
@@ -219,7 +223,9 @@ static int dev_open(PAL_HANDLE* handle, const char* type, const char* uri, int a
     hdl->dev.fd_out = PAL_IDX_POISON;
     *handle         = hdl;
 
-    return ops->open(handle, dev_type, uri, access, share, create, options);
+    ret = ops->open(handle, dev_type, uri, access, share, create, options);
+    free(dev_type);
+    return ret;
 }
 
 /* 'read' operation for device stream */
@@ -343,7 +349,7 @@ static inline void dev_attrcopy(PAL_STREAM_ATTR* attr, struct stat* stat) {
 /* 'attrquery' operation for device streams */
 static int dev_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
     struct handle_ops* ops = NULL;
-    const char* dev_type   = NULL;
+    char* dev_type   = NULL;
     int ret                = 0;
 
     ret = parse_device_uri(&uri, &dev_type, &ops);
@@ -354,7 +360,9 @@ static int dev_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* att
     if (!ops || !ops->attrquery)
         return -PAL_ERROR_NOTSUPPORT;
 
-    return ops->attrquery(dev_type, uri, attr);
+    ret = ops->attrquery(dev_type, uri, attr);
+    free(dev_type);
+    return ret;
 }
 
 /* 'attrquerybyhdl' operation for device stream */

+ 3 - 3
Pal/src/host/Linux/db_pipes.c

@@ -304,7 +304,7 @@ static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
 {
     options &= PAL_OPTION_MASK;
 
-    if (strpartcmp_static(type, "pipe:") && !*uri)
+    if (strcmp_static(type, "pipe") && !*uri)
         return pipe_private(handle, options);
 
     char * endptr;
@@ -313,10 +313,10 @@ static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
     if (*endptr)
         return -PAL_ERROR_INVAL;
 
-    if (strpartcmp_static(type, "pipe.srv:"))
+    if (strcmp_static(type, "pipe.srv"))
         return pipe_listen(handle, pipeid, options);
 
-    if (strpartcmp_static(type, "pipe:"))
+    if (strcmp_static(type, "pipe"))
         return pipe_connect(handle, pipeid, options);
 
     return -PAL_ERROR_INVAL;

+ 4 - 4
Pal/src/host/Linux/db_sockets.c

@@ -566,10 +566,10 @@ static int tcp_open (PAL_HANDLE *handle, const char * type, const char * uri,
     char uri_buf[PAL_SOCKADDR_SIZE];
     memcpy(uri_buf, uri, uri_len);
 
-    if (strpartcmp_static(type, "tcp.srv:"))
+    if (strcmp_static(type, "tcp.srv"))
         return tcp_listen(handle, uri_buf, options);
 
-    if (strpartcmp_static(type, "tcp:"))
+    if (strcmp_static(type, "tcp"))
         return tcp_connect(handle, uri_buf, options);
 
     return -PAL_ERROR_NOTSUPPORT;
@@ -785,10 +785,10 @@ static int udp_open (PAL_HANDLE *hdl, const char * type, const char * uri,
     memcpy(buf, uri, len + 1);
     options &= PAL_OPTION_MASK;
 
-    if (strpartcmp_static(type, "udp.srv:"))
+    if (strcmp_static(type, "udp.srv"))
         return udp_bind(hdl, buf, options);
 
-    if (strpartcmp_static(type, "udp:"))
+    if (strcmp_static(type, "udp"))
         return udp_connect(hdl, buf, options);
 
     return -PAL_ERROR_NOTSUPPORT;

+ 12 - 7
Pal/src/host/Skeleton/db_devices.c

@@ -50,10 +50,9 @@ static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
             &term_ops,
         };
 
-/* parse-device_uri scan the uri, parse the prefix of the uri and search
-   for stream handler wich will open or access the device */
-static int parse_device_uri (const char ** uri, const char ** type,
-                             struct handle_ops ** ops)
+/* parse_device_uri scans the uri, parses the prefix of the uri and searches
+   for stream handler wich will open or access the device. */
+static int parse_device_uri(const char ** uri, char ** type, struct handle_ops ** ops)
 {
     struct handle_ops * dops = NULL;
     const char * p, * u = (*uri);
@@ -67,8 +66,12 @@ static int parse_device_uri (const char ** uri, const char ** type,
         return -PAL_ERROR_NOTSUPPORT;
 
     *uri = (*p) ? p + 1 : p;
-    if (type)
-        *type = u;
+    if (type) {
+        *type = malloc_copy(u, p - u + 1);
+        if (!*type)
+            return -PAL_ERROR_NOMEM;
+        (*type)[p - u] = '\0';
+    }
     if (ops)
         *ops = dops;
     return 0;
@@ -215,7 +218,9 @@ static int dev_attrquery (const char * type, const char * uri,
     if (!ops || !ops->attrquery)
         return -PAL_ERROR_NOTSUPPORT;
 
-    return ops->attrquery(dev_type, uri, attr);
+    ret = ops->attrquery(dev_type, uri, attr);
+    free(dev_type);
+    return ret;
 }
 
 /* 'attrquerybyhdl' operation for device stream */

+ 3 - 3
Pal/src/host/Skeleton/db_pipes.c

@@ -60,7 +60,7 @@ static int pipe_open (PAL_HANDLE *handle, const char * type,
                       const char * uri, int access, int share,
                       int create, int options)
 {
-    if (strpartcmp_static(type, "pipe:") && !*uri)
+    if (strcmp_static(type, "pipe") && !*uri)
         return pipe_private(handle);
 
     char * endptr;
@@ -77,10 +77,10 @@ static int pipe_open (PAL_HANDLE *handle, const char * type,
     if (*endptr)
         return -PAL_ERROR_INVAL;
 
-    if (strpartcmp_static(type, "pipe.srv:"))
+    if (strcmp_static(type, "pipe.srv"))
         return pipe_listen(handle, pipeid, create);
 
-    if (strpartcmp_static(type, "pipe:"))
+    if (strcmp_static(type, "pipe"))
         return pipe_connect(handle, pipeid, connid, create);
 
     return -PAL_ERROR_INVAL;

+ 4 - 4
Pal/src/host/Skeleton/db_sockets.c

@@ -65,10 +65,10 @@ static int tcp_open (PAL_HANDLE *handle, const char * type, const char * uri,
     char uri_buf[PAL_SOCKADDR_SIZE];
     memcpy(uri_buf, uri, uri_len);
 
-    if (!strpartcmp_static(type, "tcp.srv:"))
+    if (!strcmp_static(type, "tcp.srv"))
         return tcp_listen(handle, uri_buf, create);
 
-    if (!strpartcmp_static(type, "tcp:"))
+    if (!strcmp_static(type, "tcp"))
         return tcp_connect(handle, uri_buf, create);
 
     return -PAL_ERROR_NOTSUPPORT;
@@ -109,10 +109,10 @@ static int udp_open (PAL_HANDLE *hdl, const char * type, const char * uri,
 
     memcpy(buf, uri, len + 1);
 
-    if (strpartcmp_static(type, "udp.srv:"))
+    if (strcmp_static(type, "udp.srv"))
         return udp_bind(hdl, buf, create);
 
-    if (strpartcmp_static(type, "udp:"))
+    if (strcmp_static(type, "udp"))
         return udp_connect(hdl, buf);
 
     return -PAL_ERROR_NOTSUPPORT;