v4l: libv4l2subdev: Precisely convert media bus string to code
PATCH 1/1 v4l: libv4l2subdev: Precisely convert media bus string to code
The length of the string was ignored, making it possible for the conversion to fail due to extra characters in the string.
---
This patch should be applied before the set "[v4l-utils PATCH v2 0/3] List supported formats in libv4l2subdev":
<URL:http://www.spinics.net/lists/linux-media/msg95377.html>
utils/media-ctl/libv4l2subdev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
utils/media-ctl/libv4l2subdev.c +4 -6
@@ -769,14 +769,12 @@ enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,unsigned int i;for (i =0 ; i <ARRAY_SIZE (mbus_formats); ++i) { -if (strncmp (mbus_formats[i].name, string, length) ==0 ) -break ; +if (strncmp (mbus_formats[i].name, string, length) ==0 + &&strlen (mbus_formats[i].name) == length) +return mbus_formats[i].code; } -if (i ==ARRAY_SIZE (mbus_formats)) -return (enum v4l2_mbus_pixelcode )-1 ; - -return mbus_formats[i].code; +return (enum v4l2_mbus_pixelcode )-1 ; } static struct {
Signature · 1 line
2.1.0.231.g7484e3b
Hi Sakari,
Thank you for the patch.
Quoted 2 lines · On Wednesday 09 December 2015 17:14:47 Sakari Ailus wrote:
The length of the string was ignored, making it possible for the conversion to fail due to extra characters in the string.
I'm not sure to follow you there. Is the issue that passing a string such as "SBGGR10" would match "SBGGR10_DPCM8" if it was listed before "SBGGR10" ? If that's the case I'd write the commit message as
Any character beyond the fist `length' characters in the mbus_formats strings are ignored, causing incorrect matches if the format entry starts with but isn't equal to the passed format.
Quoted 15 lines
Signed-off-by Sakari Ailus <[email protected]>---
This patch should be applied before the set "[v4l-utils PATCH v2 0/3] List supported formats in libv4l2subdev":<URL:http://www.spinics.net/lists/linux-media/msg95377.html>
utils/media-ctl/libv4l2subdev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)(quoted diff) +0
-0
b/utils/media-ctl/libv4l2subdev.cindex 33c1ee6..cce527d 100644
utils/media-ctl/libv4l2subdev.c enum v4l2_mbus_pixelcode · line 773
Quoted 7 lines
utils/media-ctl/libv4l2subdev.c +2
-2@@ -769,14 +769,12 @@ enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, unsigned int i;for (i =0 ; i <ARRAY_SIZE (mbus_formats); ++i) { -if (strncmp (mbus_formats[i].name, string, length) ==0 ) -break ; +if (strncmp (mbus_formats[i].name, string, length) ==0 + &&strlen (mbus_formats[i].name) == length)
How about mbus_formats[i].name[length] == '\0' instead ? That should be more efficient.
I also wonder whether we shouldn't just get rid of the length argument and force the passed format string to be zero-terminated.
Quoted 11 lines
utils/media-ctl/libv4l2subdev.c +2
-4+return mbus_formats[i].code; } -if (i ==ARRAY_SIZE (mbus_formats)) -return (enum v4l2_mbus_pixelcode )-1 ; - -return mbus_formats[i].code; +return (enum v4l2_mbus_pixelcode )-1 ; } static struct {
Signature · 3 lines
Regards, Laurent Pinchart
Hi Laurent,
Quoted 11 lines · Laurent Pinchart
Hi Sakari,
Thank you for the patch.
Quoted 2 lines · On Wednesday 09 December 2015 17:14:47 Sakari Ailus wrote:
The length of the string was ignored, making it possible for the conversion to fail due to extra characters in the string.
I'm not sure to follow you there. Is the issue that passing a string such as "SBGGR10" would match "
SBGGR10_DPCM8" if it was listed before "SBGGR10" ? If that's the case I'd write the commit message as
Yes, that's the problem.
Quoted 4 lines
Any character beyond the fist `length' characters in the
mbus_formatsstrings are ignored, causing incorrect matches if the format entry starts with but isn't equal to the passed format.
I'll use this commit message then.
Quoted 18 lines
Quoted 18 lines
Signed-off-by Sakari Ailus <[email protected]>---
This patch should be applied before the set "[v4l-utils PATCH v2 0/3] List supported formats in libv4l2subdev":<URL:http://www.spinics.net/lists/linux-media/msg95377.html>
utils/media-ctl/libv4l2subdev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)(quoted diff) +0
-0
b/utils/media-ctl/libv4l2subdev.cindex 33c1ee6..cce527d 100644utils/media-ctl/libv4l2subdev.c +0
-0@@ -769,14 +769,12 @@ enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, unsigned int i;
Quoted 8 lines
utils/media-ctl/libv4l2subdev.c enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, · line 773
Quoted 5 lines
utils/media-ctl/libv4l2subdev.c +2
-2for (i =0 ; i <ARRAY_SIZE (mbus_formats); ++i) { -if (strncmp (mbus_formats[i].name, string, length) ==0 ) -break ; +if (strncmp (mbus_formats[i].name, string, length) ==0 + &&strlen (mbus_formats[i].name) == length)How about mbus_formats[i].name[length] == '\0' instead ? That should be more efficient.
Fine for me.
Quoted 3 lines
I also wonder whether we shouldn't just get rid of the length argument and force the passed format string to be zero-terminated.
I believe the reason is that the current user (media-ctl test program) parses the user input and passes a portion of that to this function to convert the string to a numeric value. That'd be a bit more cumbersome as we'd either require copying the string elsewhere or changing the input by the program. I wouldn't change the behaviour, at least not now.
Quoted 13 lines
Quoted 12 lines
utils/media-ctl/libv4l2subdev.c +2
-4+return mbus_formats[i].code; } -if (i ==ARRAY_SIZE (mbus_formats)) -return (enum v4l2_mbus_pixelcode )-1 ; - -return mbus_formats[i].code; +return (enum v4l2_mbus_pixelcode )-1 ; } static struct {
Signature · 4 lines
Kind regards, Sakari Ailus e-mail: [email protected] XMPP: [email protected]
Hi Sakari,
Quoted 46 lines · On Monday 25 January 2016 13:39:10 Sakari Ailus wrote:
Quoted 7 lines · Laurent Pinchart
Quoted 2 lines · On Wednesday 09 December 2015 17:14:47 Sakari Ailus wrote:
The length of the string was ignored, making it possible for the conversion to fail due to extra characters in the string.
I'm not sure to follow you there. Is the issue that passing a string such as "SBGGR10" would match "
SBGGR10_DPCM8" if it was listed before "SBGGR10" ? If that's the case I'd write the commit message asYes, that's the problem.
Quoted 3 lines
Any character beyond the fist `length' characters in the
mbus_formatsstrings are ignored, causing incorrect matches if the format entry starts with but isn't equal to the passed format.I'll use this commit message then.
Quoted 18 lines
Quoted 18 lines
Signed-off-by Sakari Ailus <[email protected]>---
This patch should be applied before the set "[v4l-utils PATCH v2 0/3] List supported formats in libv4l2subdev":<URL:http://www.spinics.net/lists/linux-media/msg95377.html>
utils/media-ctl/libv4l2subdev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)(quoted diff) +0
-0
b/utils/media-ctl/libv4l2subdev.cindex 33c1ee6..cce527d 100644utils/media-ctl/libv4l2subdev.c +0
-0@@ -769,14 +769,12 @@ enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, unsigned int i;Quoted 8 lines
utils/media-ctl/libv4l2subdev.c enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, · line 773
Quoted 5 lines
utils/media-ctl/libv4l2subdev.c +2
-2for (i =0 ; i <ARRAY_SIZE (mbus_formats); ++i) { -if (strncmp (mbus_formats[i].name, string, length) ==0 ) -break ; +if (strncmp (mbus_formats[i].name, string, length) ==0 + &&strlen (mbus_formats[i].name) == length)How about mbus_formats[i].name[length] == '\0' instead ? That should be more efficient.
Fine for me.
Quoted 8 lines
Quoted 2 lines
I also wonder whether we shouldn't just get rid of the length argument and force the passed format string to be zero-terminated.
I believe the reason is that the current user (media-ctl test program) parses the user input and passes a portion of that to this function to convert the string to a numeric value. That'd be a bit more cumbersome as we'd either require copying the string elsewhere or changing the input by the program. I wouldn't change the behaviour, at least not now.
Yes that's the reason, and I think it's an API design mistake (or just a lack of proper API design :-)). Wouldn't it be better to copy the string in the caller ?
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
utils/media-ctl/libv4l2subdev.c +2
-4+return mbus_formats[i].code; } -if (i ==ARRAY_SIZE (mbus_formats)) -return (enum v4l2_mbus_pixelcode )-1 ; - -return mbus_formats[i].code; +return (enum v4l2_mbus_pixelcode )-1 ; }static struct {
Signature · 3 lines
Regards, Laurent Pinchart
Hi Laurent,
Quoted 58 lines · Laurent Pinchart
Hi Sakari,
Quoted 46 lines · On Monday 25 January 2016 13:39:10 Sakari Ailus wrote:
Quoted 7 lines · Laurent Pinchart
Quoted 2 lines · On Wednesday 09 December 2015 17:14:47 Sakari Ailus wrote:
The length of the string was ignored, making it possible for the conversion to fail due to extra characters in the string.
I'm not sure to follow you there. Is the issue that passing a string such as "SBGGR10" would match "
SBGGR10_DPCM8" if it was listed before "SBGGR10" ? If that's the case I'd write the commit message asYes, that's the problem.
Quoted 3 lines
Any character beyond the fist `length' characters in the
mbus_formatsstrings are ignored, causing incorrect matches if the format entry starts with but isn't equal to the passed format.I'll use this commit message then.
Quoted 18 lines
Quoted 18 lines
Signed-off-by Sakari Ailus <[email protected]>---
This patch should be applied before the set "[v4l-utils PATCH v2 0/3] List supported formats in libv4l2subdev":<URL:http://www.spinics.net/lists/linux-media/msg95377.html>
utils/media-ctl/libv4l2subdev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)(quoted diff) +0
-0
b/utils/media-ctl/libv4l2subdev.cindex 33c1ee6..cce527d 100644utils/media-ctl/libv4l2subdev.c +0
-0@@ -769,14 +769,12 @@ enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, unsigned int i;Quoted 8 lines
utils/media-ctl/libv4l2subdev.c enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, · line 773
Quoted 5 lines
utils/media-ctl/libv4l2subdev.c +2
-2for (i =0 ; i <ARRAY_SIZE (mbus_formats); ++i) { -if (strncmp (mbus_formats[i].name, string, length) ==0 ) -break ; +if (strncmp (mbus_formats[i].name, string, length) ==0 + &&strlen (mbus_formats[i].name) == length)How about mbus_formats[i].name[length] == '\0' instead ? That should be more efficient.
Fine for me.
Quoted 8 lines
Quoted 2 lines
I also wonder whether we shouldn't just get rid of the length argument and force the passed format string to be zero-terminated.
I believe the reason is that the current user (media-ctl test program) parses the user input and passes a portion of that to this function to convert the string to a numeric value. That'd be a bit more cumbersome as we'd either require copying the string elsewhere or changing the input by the program. I wouldn't change the behaviour, at least not now.
Quoted 5 lines
Yes that's the reason, and I think it's an API design mistake (or just a lack of proper API design :-)). Wouldn't it be better to copy the string in the caller ?
From the API point of view, I don't disagree. But I do think this should be
done in a separate patch, not in this one --- this is a bugfix, really.
I'll submit one more patch for that.
Quoted 12 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
utils/media-ctl/libv4l2subdev.c +2
-4+return mbus_formats[i].code; } -if (i ==ARRAY_SIZE (mbus_formats)) -return (enum v4l2_mbus_pixelcode )-1 ; - -return mbus_formats[i].code; +return (enum v4l2_mbus_pixelcode )-1 ; }static struct {
Signature · 4 lines
Regards, Sakari Ailus e-mail: [email protected] XMPP: [email protected]
Hi Sakari,
Quoted 60 lines · On Monday 25 January 2016 22:47:07 Sakari Ailus wrote:
Quoted 59 lines · Laurent Pinchart
Quoted 57 lines · On Monday 25 January 2016 13:39:10 Sakari Ailus wrote:
Quoted 7 lines · Laurent Pinchart
Quoted 2 lines · On Wednesday 09 December 2015 17:14:47 Sakari Ailus wrote:
The length of the string was ignored, making it possible for the conversion to fail due to extra characters in the string.
I'm not sure to follow you there. Is the issue that passing a string such as "SBGGR10" would match "
SBGGR10_DPCM8" if it was listed before "SBGGR10" ? If that's the case I'd write the commit message asYes, that's the problem.
Quoted 4 lines
Any character beyond the fist `length' characters in the
mbus_formatsstrings are ignored, causing incorrect matches if the format entry starts
with but isn't equal to the passed format.I'll use this commit message then.
Quoted 21 lines
Quoted 21 lines
Signed-off-by Sakari Ailus <[email protected]>---
This patch should be applied before the set "[v4l-utils PATCH v2 0/3] List supported formats in libv4l2subdev":<URL:http://www.spinics.net/lists/linux-media/msg95377.html>
utils/media-ctl/libv4l2subdev.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)(quoted diff) +0
-0
b/utils/media-ctl/libv4l2subdev.cindex 33c1ee6..cce527d 100644utils/media-ctl/libv4l2subdev.c +0
-0@@ -769,14 +769,12 @@ enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, unsigned int i;for (i =0 ; i <ARRAY_SIZE (mbus_formats); ++i) {Quoted 7 lines
utils/media-ctl/libv4l2subdev.c enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string, · line 773
Quoted 4 lines
utils/media-ctl/libv4l2subdev.c +2
-2-if (strncmp (mbus_formats[i].name, string, length) ==0 ) -break ; +if (strncmp (mbus_formats[i].name, string, length) ==0 + &&strlen (mbus_formats[i].name) == length)How about mbus_formats[i].name[length] == '\0' instead ? That should be more efficient.
Fine for me.
Quoted 2 lines
I also wonder whether we shouldn't just get rid of the length argument and force the passed format string to be zero-terminated.
I believe the reason is that the current user (media-ctl test program) parses the user input and passes a portion of that to this function to convert the string to a numeric value. That'd be a bit more cumbersome as we'd either require copying the string elsewhere or changing the input by the program. I wouldn't change the behaviour, at least not now.
Quoted 6 lines
Quoted 3 lines
Yes that's the reason, and I think it's an API design mistake (or just a lack of proper API design :-)). Wouldn't it be better to copy the string in the caller ?
From the API point of view, I don't disagree. But I do think this should be done in a separate patch, not in this one --- this is a bugfix, really.
Sure, I agree with that.
Quoted 1 line
I'll submit one more patch for that.
Thank you.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
utils/media-ctl/libv4l2subdev.c +2
-4+return mbus_formats[i].code; } -if (i ==ARRAY_SIZE (mbus_formats)) -return (enum v4l2_mbus_pixelcode )-1 ; - -return mbus_formats[i].code; +return (enum v4l2_mbus_pixelcode )-1 ; } static struct {
Signature · 3 lines
Regards, Laurent Pinchart