Skip to contentSkip to conversation

Prepare for mediatext library, cleanups

PATCH In review Sakari Ailus 3 participants 11 messages May 24, 2016 – May 26, 2016 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

cover Prepare for mediatext library, cleanups

Hi,

These two patches prepare for the mediatext library and clean up libmediactl interface a little.

Signature · 2 lines
Kind regards,
Sakari
#1 lore raw

PATCH 1/2 libmediactl: Drop length argument from media_get_entity_by_name()

Recently it was decided that the API dealing with string operations would be better to just receive a nul-terminated string rather than a string the length of which is defined. This change was implemented for v4l2_subdev_string_to_pixelcode() and v4l2_subdev_string_to_field() functions by patch "v4l: libv4l2subdev: Drop length argument from string conversion functions" (commit id 341f4343e6190a7ceb546f7c74fa67e1cc9ae79f).

Do the same change for media_get_entity_by_name() in libmediactl. No other functions using length argument for strings remain in libmediactl.

---
 utils/media-ctl/libmediactl.c | 19 +++++++++----------
 utils/media-ctl/media-ctl.c   |  3 +--
 utils/media-ctl/mediactl.h    |  3 +--
 3 files changed, 11 insertions(+), 14 deletions(-)
utils/media-ctl/libmediactl.c +9 -10
index 89ac11c..78caa7c 100644
@@ -66,21 +66,14 @@ struct media_pad *media_entity_remote_source(struct media_pad *pad)
 }
 
 struct media_entity *media_get_entity_by_name(struct media_device *media,
-					      const char *name, size_t length)
+					      const char *name)
 {
 	unsigned int i;
 
-	/* A match is impossible if the entity name is longer than the maximum
-	 * size we can get from the kernel.
-	 */
-	if (length >= FIELD_SIZEOF(struct media_entity_desc, name))
-		return NULL;
-
 	for (i = 0; i < media->entities_count; ++i) {
 		struct media_entity *entity = &media->entities[i];
 
-		if (strncmp(entity->info.name, name, length) == 0 &&
-		    entity->info.name[length] == '\0')
+		if (strcmp(entity->info.name, name) == 0)
 			return entity;
 	}
 
@@ -804,6 +797,8 @@ struct media_pad *media_parse_pad(struct media_device *media,
 	for (; isspace(*p); ++p);
 
 	if (*p == '"' || *p == '\'') {
+		char *name;
+
 		for (end = (char *)p + 1; *end && *end != '"' && *end != '\''; ++end);
 		if (*end != '"' && *end != '\'') {
 			media_dbg(media, "missing matching '\"'\n");
@@ -811,7 +806,11 @@ struct media_pad *media_parse_pad(struct media_device *media,
 			return NULL;
 		}
 
-		entity = media_get_entity_by_name(media, p + 1, end - p - 1);
+		name = strndup(p + 1, end - p - 1);
+		if (!name)
+			return NULL;
+		entity = media_get_entity_by_name(media, name);
+		free(name);
 		if (entity == NULL) {
 			media_dbg(media, "no such entity \"%.*s\"\n", end - p - 1, p + 1);
 			*endp = (char *)p + 1;
utils/media-ctl/media-ctl.c +1 -2
index f45ca43..2f049c6 100644
@@ -559,8 +559,7 @@ int main(int argc, char **argv)
 	if (media_opts.entity) {
 		struct media_entity *entity;
 
-		entity = media_get_entity_by_name(media, media_opts.entity,
-						  strlen(media_opts.entity));
+		entity = media_get_entity_by_name(media, media_opts.entity);
 		if (entity == NULL) {
 			printf("Entity '%s' not found\n", media_opts.entity);
 			goto out;
utils/media-ctl/mediactl.h +1 -2
index 77ac182..b5a92f5 100644
@@ -245,14 +245,13 @@ static inline unsigned int media_entity_type(struct media_entity *entity)
  * @brief Find an entity by its name.
  * @param media - media device.
  * @param name - entity name.
- * @param length - size of @a name.
  *
  * Search for an entity with a name equal to @a name.
  *
  * @return A pointer to the entity if found, or NULL otherwise.
  */
 struct media_entity *media_get_entity_by_name(struct media_device *media,
-	const char *name, size_t length);
+	const char *name);
 
 /**
  * @brief Find an entity by its ID.
Signature · 1 line
1.9.1
#2 lore raw

Re: [v4l-utils PATCH 1/2] libmediactl: Drop length argument from media_get_entity_by_name()

Hi Sakari,

Thank you for the patch.

Quoted 33 lines · On Tuesday 24 May 2016 15:48:02 Sakari Ailus wrote:

Recently it was decided that the API dealing with string operations would be better to just receive a nul-terminated string rather than a string the length of which is defined. This change was implemented for v4l2_subdev_string_to_pixelcode() and v4l2_subdev_string_to_field() functions by patch "v4l: libv4l2subdev: Drop length argument from string conversion functions" (commit id 341f4343e6190a7ceb546f7c74fa67e1cc9ae79f).

Do the same change for media_get_entity_by_name() in libmediactl. No other functions using length argument for strings remain in libmediactl.

---
 utils/media-ctl/libmediactl.c | 19 +++++++++----------
 utils/media-ctl/media-ctl.c   |  3 +--
 utils/media-ctl/mediactl.h    |  3 +--
 3 files changed, 11 insertions(+), 14 deletions(-)
utils/media-ctl/libmediactl.c +1 -3
index 89ac11c..78caa7c 100644
@@ -66,21 +66,14 @@ struct media_pad *media_entity_remote_source(struct
 media_pad *pad) }
 
 struct media_entity *media_get_entity_by_name(struct media_device *media,
-					      const char *name, size_t length)
+					      const char *name)
 {
 	unsigned int i;
 
-	/* A match is impossible if the entity name is longer than the maximum
-	 * size we can get from the kernel.

utils/media-ctl/libmediactl.c struct media_pad *media_entity_remote_source(struct · line 76

Quoted 10 lines
utils/media-ctl/libmediactl.c +1 -6
-	 */
-	if (length >= FIELD_SIZEOF(struct media_entity_desc, name))
-		return NULL;
-
 	for (i = 0; i < media->entities_count; ++i) {
 		struct media_entity *entity = &media->entities[i];
 
-		if (strncmp(entity->info.name, name, length) == 0 &&
-		    entity->info.name[length] == '\0')
+		if (strcmp(entity->info.name, name) == 0)

While the kernel API guarantees that entity->info.name will be NULL- terminated, wouldn't it be safer to add a safety check here ?

Quoted 59 lines
utils/media-ctl/libmediactl.c +7 -1
 			return entity;
 	}
 
@@ -804,6 +797,8 @@ struct media_pad *media_parse_pad(struct media_device
 *media, for (; isspace(*p); ++p);
 
 	if (*p == '"' || *p == '\'') {
+		char *name;
+
 		for (end = (char *)p + 1; *end && *end != '"' && *end != '\''; ++end);
 		if (*end != '"' && *end != '\'') {
 			media_dbg(media, "missing matching '\"'\n");
@@ -811,7 +806,11 @@ struct media_pad *media_parse_pad(struct media_device
 *media, return NULL;
 		}
 
-		entity = media_get_entity_by_name(media, p + 1, end - p - 1);
+		name = strndup(p + 1, end - p - 1);
+		if (!name)
+			return NULL;
+		entity = media_get_entity_by_name(media, name);
+		free(name);
 		if (entity == NULL) {
 			media_dbg(media, "no such entity \"%.*s\"\n", end - p - 1, p + 1);
 			*endp = (char *)p + 1;
utils/media-ctl/media-ctl.c +1 -2
index f45ca43..2f049c6 100644
@@ -559,8 +559,7 @@ int main(int argc, char **argv)
 	if (media_opts.entity) {
 		struct media_entity *entity;
 
-		entity = media_get_entity_by_name(media, media_opts.entity,
-						  strlen(media_opts.entity));
+		entity = media_get_entity_by_name(media, media_opts.entity);
 		if (entity == NULL) {
 			printf("Entity '%s' not found\n", media_opts.entity);
 			goto out;
utils/media-ctl/mediactl.h +0 -0
index 77ac182..b5a92f5 100644
@@ -245,14 +245,13 @@ static inline unsigned int media_entity_type(struct

media_entity *entity) * @brief Find an entity by its name.
* @param media - media device.
* @param name - entity name.
- * @param length - size of @a name.
*
* Search for an entity with a name equal to @a name.
*
* @return A pointer to the entity if found, or NULL otherwise. */
struct media_entity *media_get_entity_by_name(struct media_device *media,

utils/media-ctl/mediactl.h +1 -1
-	const char *name, size_t length);
+	const char *name);
 
 /**
  * @brief Find an entity by its ID.
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw

Re: [v4l-utils PATCH 1/2] libmediactl: Drop length argument from media_get_entity_by_name()

Hi Laurent,

Thanks for the review!

On Tue, May 24, 2016 at 08:09:37PM +0300, Laurent Pinchart wrote:
...

Quoted 4 lines

utils/media-ctl/libmediactl.c struct media_pad *media_entity_remote_source(struct media_pad *pad) · line 76

Quoted 1 line
utils/media-ctl/libmediactl.c +1 -0
+		if (strcmp(entity->info.name, name) == 0)

While the kernel API guarantees that entity->info.name will be NULL- terminated, wouldn't it be safer to add a safety check here ?

The kernel implementation in media-device.c does use strlcpy() so this is even not about drivers doing this right. If you insist, I'll change it. :-)

Signature · 4 lines
Cheers,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#4 lore raw

Re: [v4l-utils PATCH 1/2] libmediactl: Drop length argument from media_get_entity_by_name()

Hi Sakari,

Quoted 10 lines · On Tuesday 24 May 2016 23:50:44 Sakari Ailus wrote:

On Tue, May 24, 2016 at 08:09:37PM +0300, Laurent Pinchart wrote:
...

Quoted 4 lines

utils/media-ctl/libmediactl.c struct media_pad *media_entity_remote_source(struct media_pad *pad) · line 76

Quoted 1 line
utils/media-ctl/libmediactl.c +1 -0
+		if (strcmp(entity->info.name, name) == 0)

While the kernel API guarantees that entity->info.name will be NULL- terminated, wouldn't it be safer to add a safety check here ?

The kernel implementation in media-device.c does use strlcpy() so this is even not about drivers doing this right. If you insist, I'll change it. :-)

I suppose we'll have other problems if the kernel doesn't behave.

Signature · 3 lines
Regards,

Laurent Pinchart
#5 lore raw

Re: [v4l-utils PATCH 1/2] libmediactl: Drop length argument from media_get_entity_by_name()

Quoted 11 lines · Laurent Pinchart

Hi Sakari,

Quoted 8 lines · On Tuesday 24 May 2016 23:50:44 Sakari Ailus wrote:

On Tue, May 24, 2016 at 08:09:37PM +0300, Laurent Pinchart wrote:
...

Quoted 4 lines

utils/media-ctl/libmediactl.c struct media_pad *media_entity_remote_source(struct media_pad *pad) · line 76

Quoted 1 line
utils/media-ctl/libmediactl.c +1 -0
+		if (strcmp(entity->info.name, name) == 0)

While the kernel API guarantees that entity->info.name will be NULL- terminated, wouldn't it be safer to add a safety check here ?

Quoted 6 lines
Quoted 2 lines

The kernel implementation in media-device.c does use strlcpy() so this is even not about drivers doing this right. If you insist, I'll change it. :-)

I suppose we'll have other problems if the kernel doesn't behave.

Thanks!

I'll then proceed to push the two patches to v4l-utils.

Signature · 4 lines
Kind regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#6 lore raw

PATCH 2/2 mediactl: Separate entity and pad parsing

In reply to Sakari Ailus

Sometimes it's useful to be able to parse the entity independent of the pad. Separate entity parsing into media_parse_entity().

---
 utils/media-ctl/libmediactl.c | 28 ++++++++++++++++++++++++----
 utils/media-ctl/mediactl.h    | 14 ++++++++++++++
 2 files changed, 38 insertions(+), 4 deletions(-)
utils/media-ctl/libmediactl.c +24 -4
index 78caa7c..14b17e6 100644
@@ -781,10 +781,10 @@ int media_device_add_entity(struct media_device *media,
 	return 0;
 }
 
-struct media_pad *media_parse_pad(struct media_device *media,
-				  const char *p, char **endp)
+struct media_entity *media_parse_entity(struct media_device *media,
+					const char *p, char **endp)
 {
-	unsigned int entity_id, pad;
+	unsigned int entity_id;
 	struct media_entity *entity;
 	char *end;
 
@@ -827,7 +827,27 @@ struct media_pad *media_parse_pad(struct media_device *media,
 			return NULL;
 		}
 	}
-	for (; isspace(*end); ++end);
+	for (p = end; isspace(*p); ++p);
+
+	*endp = (char *)p;
+
+	return entity;
+}
+
+struct media_pad *media_parse_pad(struct media_device *media,
+				  const char *p, char **endp)
+{
+	unsigned int pad;
+	struct media_entity *entity;
+	char *end;
+
+	if (endp == NULL)
+		endp = &end;
+
+	entity = media_parse_entity(media, p, &end);
+	if (!entity)
+		return NULL;
+	*endp = end;
 
 	if (*end != ':') {
 		media_dbg(media, "Expected ':'\n", *end);
utils/media-ctl/mediactl.h +14 -0
index b5a92f5..af36051 100644
@@ -367,6 +367,20 @@ int media_setup_link(struct media_device *media,
 int media_reset_links(struct media_device *media);
 
 /**
+ * @brief Parse string to an entity on the media device.
+ * @param media - media device.
+ * @param p - input string
+ * @param endp - pointer to string where parsing ended
+ *
+ * Parse NULL terminated string describing an entity and return its
+ * struct media_entity instance.
+ *
+ * @return Pointer to struct media_entity on success, NULL on failure.
+ */
+struct media_entity *media_parse_entity(struct media_device *media,
+					const char *p, char **endp);
+
+/**
  * @brief Parse string to a pad on the media device.
  * @param media - media device.
  * @param p - input string
Signature · 1 line
1.9.1
#7 lore raw

Re: [v4l-utils PATCH 2/2] mediactl: Separate entity and pad parsing

Hi Sakari,

Thank you for the patch.

Quoted 43 lines · On Tuesday 24 May 2016 15:48:03 Sakari Ailus wrote:

Sometimes it's useful to be able to parse the entity independent of the pad. Separate entity parsing into media_parse_entity().

---
 utils/media-ctl/libmediactl.c | 28 ++++++++++++++++++++++++----
 utils/media-ctl/mediactl.h    | 14 ++++++++++++++
 2 files changed, 38 insertions(+), 4 deletions(-)
utils/media-ctl/libmediactl.c +14 -4
index 78caa7c..14b17e6 100644
@@ -781,10 +781,10 @@ int media_device_add_entity(struct media_device
 *media, return 0;
 }
 
-struct media_pad *media_parse_pad(struct media_device *media,
-				  const char *p, char **endp)
+struct media_entity *media_parse_entity(struct media_device *media,
+					const char *p, char **endp)
 {
-	unsigned int entity_id, pad;
+	unsigned int entity_id;
 	struct media_entity *entity;
 	char *end;
 
@@ -827,7 +827,27 @@ struct media_pad *media_parse_pad(struct media_device
 *media, return NULL;
 		}
 	}
-	for (; isspace(*end); ++end);
+	for (p = end; isspace(*p); ++p);
+
+	*endp = (char *)p;
+
+	return entity;
+}
+
+struct media_pad *media_parse_pad(struct media_device *media,
+				  const char *p, char **endp)
+{
+	unsigned int pad;

utils/media-ctl/libmediactl.c struct media_pad *media_parse_pad(struct media_device · line 850

Quoted 10 lines
utils/media-ctl/libmediactl.c +10 -0
+	struct media_entity *entity;
+	char *end;
+
+	if (endp == NULL)
+		endp = &end;
+
+	entity = media_parse_entity(media, p, &end);
+	if (!entity)
+		return NULL;
+	*endp = end;

Did you mean

	if (!entity) {
		*endp = end;
		return NULL;
	}

? There's no need to assign endp after the check as all return paths below assign it correctly, but it should be set when returning an error here.

Quoted 27 lines
utils/media-ctl/libmediactl.c +0 -0
 	if (*end != ':') {
 		media_dbg(media, "Expected ':'\n", *end);
utils/media-ctl/mediactl.h +14 -0
index b5a92f5..af36051 100644
@@ -367,6 +367,20 @@ int media_setup_link(struct media_device *media,
 int media_reset_links(struct media_device *media);
 
 /**
+ * @brief Parse string to an entity on the media device.
+ * @param media - media device.
+ * @param p - input string
+ * @param endp - pointer to string where parsing ended
+ *
+ * Parse NULL terminated string describing an entity and return its
+ * struct media_entity instance.
+ *
+ * @return Pointer to struct media_entity on success, NULL on failure.
+ */
+struct media_entity *media_parse_entity(struct media_device *media,
+					const char *p, char **endp);
+
+/**
  * @brief Parse string to a pad on the media device.
  * @param media - media device.
  * @param p - input string
Signature · 3 lines
Regards,

Laurent Pinchart
#8 lore raw

Re: [v4l-utils PATCH 2/2] mediactl: Separate entity and pad parsing

Hi Laurent,

On Tue, May 24, 2016 at 08:14:22PM +0300, Laurent Pinchart wrote:
...

Quoted 17 lines
Quoted 4 lines
utils/media-ctl/libmediactl.c +4 -0
+struct media_pad *media_parse_pad(struct media_device *media,
+				  const char *p, char **endp)
+{
+	unsigned int pad;

utils/media-ctl/libmediactl.c struct media_pad *media_parse_pad(struct media_device *media, · line 850

Quoted 10 lines
utils/media-ctl/libmediactl.c +10 -0
+	struct media_entity *entity;
+	char *end;
+
+	if (endp == NULL)
+		endp = &end;
+
+	entity = media_parse_entity(media, p, &end);
+	if (!entity)
+		return NULL;
+	*endp = end;

Did you mean

Quoted 7 lines
	if (!entity) {
		*endp = end;
		return NULL;
	}

? There's no need to assign endp after the check as all return paths below assign it correctly, but it should be set when returning an error here.

Good catch! Yeah, it's a bug, I'll fix that.

Signature · 4 lines
Cheers,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#9 lore raw

PATCH 2/2 mediactl: Separate entity and pad parsing

In reply to Laurent Pinchart

Sometimes it's useful to be able to parse the entity independent of the pad. Separate entity parsing into media_parse_entity().

---
 utils/media-ctl/libmediactl.c | 29 +++++++++++++++++++++++++----
 utils/media-ctl/mediactl.h    | 14 ++++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)
utils/media-ctl/libmediactl.c +25 -4
index 78caa7c..498dfd1 100644
@@ -781,10 +781,10 @@ int media_device_add_entity(struct media_device *media,
 	return 0;
 }
 
-struct media_pad *media_parse_pad(struct media_device *media,
-				  const char *p, char **endp)
+struct media_entity *media_parse_entity(struct media_device *media,
+					const char *p, char **endp)
 {
-	unsigned int entity_id, pad;
+	unsigned int entity_id;
 	struct media_entity *entity;
 	char *end;
 
@@ -827,7 +827,28 @@ struct media_pad *media_parse_pad(struct media_device *media,
 			return NULL;
 		}
 	}
-	for (; isspace(*end); ++end);
+	for (p = end; isspace(*p); ++p);
+
+	*endp = (char *)p;
+
+	return entity;
+}
+
+struct media_pad *media_parse_pad(struct media_device *media,
+				  const char *p, char **endp)
+{
+	unsigned int pad;
+	struct media_entity *entity;
+	char *end;
+
+	if (endp == NULL)
+		endp = &end;
+
+	entity = media_parse_entity(media, p, &end);
+	if (!entity) {
+		*endp = end;
+		return NULL;
+	}
 
 	if (*end != ':') {
 		media_dbg(media, "Expected ':'\n", *end);
utils/media-ctl/mediactl.h +14 -0
index b5a92f5..af36051 100644
@@ -367,6 +367,20 @@ int media_setup_link(struct media_device *media,
 int media_reset_links(struct media_device *media);
 
 /**
+ * @brief Parse string to an entity on the media device.
+ * @param media - media device.
+ * @param p - input string
+ * @param endp - pointer to string where parsing ended
+ *
+ * Parse NULL terminated string describing an entity and return its
+ * struct media_entity instance.
+ *
+ * @return Pointer to struct media_entity on success, NULL on failure.
+ */
+struct media_entity *media_parse_entity(struct media_device *media,
+					const char *p, char **endp);
+
+/**
  * @brief Parse string to a pad on the media device.
  * @param media - media device.
  * @param p - input string
Signature · 1 line
1.9.1
Acked-by on v1 2/2
#10 lore raw

Re: [v4l-utils PATCH v1.1 2/2] mediactl: Separate entity and pad parsing

Hi Sakari,

Thank you for the patch.

Quoted 4 lines · On Tuesday 24 May 2016 23:56:33 Sakari Ailus wrote:

Sometimes it's useful to be able to parse the entity independent of the pad. Separate entity parsing into media_parse_entity().

Quoted 78 lines
---
 utils/media-ctl/libmediactl.c | 29 +++++++++++++++++++++++++----
 utils/media-ctl/mediactl.h    | 14 ++++++++++++++
 2 files changed, 39 insertions(+), 4 deletions(-)
utils/media-ctl/libmediactl.c +25 -4
index 78caa7c..498dfd1 100644
@@ -781,10 +781,10 @@ int media_device_add_entity(struct media_device
 *media, return 0;
 }
 
-struct media_pad *media_parse_pad(struct media_device *media,
-				  const char *p, char **endp)
+struct media_entity *media_parse_entity(struct media_device *media,
+					const char *p, char **endp)
 {
-	unsigned int entity_id, pad;
+	unsigned int entity_id;
 	struct media_entity *entity;
 	char *end;
 
@@ -827,7 +827,28 @@ struct media_pad *media_parse_pad(struct media_device
 *media, return NULL;
 		}
 	}
-	for (; isspace(*end); ++end);
+	for (p = end; isspace(*p); ++p);
+
+	*endp = (char *)p;
+
+	return entity;
+}
+
+struct media_pad *media_parse_pad(struct media_device *media,
+				  const char *p, char **endp)
+{
+	unsigned int pad;
+	struct media_entity *entity;
+	char *end;
+
+	if (endp == NULL)
+		endp = &end;
+
+	entity = media_parse_entity(media, p, &end);
+	if (!entity) {
+		*endp = end;
+		return NULL;
+	}
 
 	if (*end != ':') {
 		media_dbg(media, "Expected ':'\n", *end);
utils/media-ctl/mediactl.h +14 -0
index b5a92f5..af36051 100644
@@ -367,6 +367,20 @@ int media_setup_link(struct media_device *media,
 int media_reset_links(struct media_device *media);
 
 /**
+ * @brief Parse string to an entity on the media device.
+ * @param media - media device.
+ * @param p - input string
+ * @param endp - pointer to string where parsing ended
+ *
+ * Parse NULL terminated string describing an entity and return its
+ * struct media_entity instance.
+ *
+ * @return Pointer to struct media_entity on success, NULL on failure.
+ */
+struct media_entity *media_parse_entity(struct media_device *media,
+					const char *p, char **endp);
+
+/**
  * @brief Parse string to a pad on the media device.
  * @param media - media device.
  * @param p - input string
Signature · 3 lines
Regards,

Laurent Pinchart