[libcamera-devel] android: jpeg: exif: Sanitize ASCII strings with utils::toAscii()

Message ID 20201005131137.50124-1-email@uajain.com
State Accepted
Headers show
Series
  • [libcamera-devel] android: jpeg: exif: Sanitize ASCII strings with utils::toAscii()
Related show

Commit Message

Umang Jain Oct. 5, 2020, 1:11 p.m. UTC
Use the newly introduced utils::toAscii() utility to remove all
non-ASCII characters for EXIF_FORMAT_ASCII strings.

Signed-off-by: Umang Jain <email@uajain.com>
---
 src/android/jpeg/exif.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

Comments

Kieran Bingham Oct. 5, 2020, 3:39 p.m. UTC | #1
Hi Umang,

On 05/10/2020 14:11, Umang Jain wrote:
> Use the newly introduced utils::toAscii() utility to remove all
> non-ASCII characters for EXIF_FORMAT_ASCII strings.
> 
> Signed-off-by: Umang Jain <email@uajain.com>
> ---
>  src/android/jpeg/exif.cpp | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp
> index 3fd5d55..bd528a7 100644
> --- a/src/android/jpeg/exif.cpp
> +++ b/src/android/jpeg/exif.cpp
> @@ -8,6 +8,7 @@
>  #include "exif.h"
>  
>  #include "libcamera/internal/log.h"
> +#include "libcamera/internal/utils.h"
>  
>  using namespace libcamera;
>  
> @@ -171,15 +172,19 @@ void Exif::setRational(ExifIfd ifd, ExifTag tag, ExifRational item)
>  
>  void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item)
>  {
> -	/* Pad 1 extra byte for null-terminated string in ASCII format. */
> -	size_t length = format == EXIF_FORMAT_ASCII ?
> -			item.length() + 1 : item.length();
> +	size_t length = item.length();
> +	std::string str = item;
> +	if (format == EXIF_FORMAT_ASCII) {
> +		str = utils::toAscii(str);
> +		/* Pad 1 extra byte to null-terminate the ASCII string. */
> +		length += 1;

This could of course also be
		length++;

But += 1 is also quite readable here so I don't mind it staying.

Sanitizing the Ascii strings seems to make some sense, especially as we
do it elsewhere - so here seems reasonable to me.

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>


> +	}
>  
>  	ExifEntry *entry = createEntry(ifd, tag, format, length, length);
>  	if (!entry)
>  		return;
>  
> -	memcpy(entry->data, item.c_str(), length);
> +	memcpy(entry->data, str.c_str(), length);
>  	exif_entry_unref(entry);
>  }
>  
>
Laurent Pinchart Oct. 5, 2020, 3:53 p.m. UTC | #2
On Mon, Oct 05, 2020 at 04:39:53PM +0100, Kieran Bingham wrote:
> Hi Umang,
> 
> On 05/10/2020 14:11, Umang Jain wrote:
> > Use the newly introduced utils::toAscii() utility to remove all
> > non-ASCII characters for EXIF_FORMAT_ASCII strings.
> > 
> > Signed-off-by: Umang Jain <email@uajain.com>
> > ---
> >  src/android/jpeg/exif.cpp | 13 +++++++++----
> >  1 file changed, 9 insertions(+), 4 deletions(-)
> > 
> > diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp
> > index 3fd5d55..bd528a7 100644
> > --- a/src/android/jpeg/exif.cpp
> > +++ b/src/android/jpeg/exif.cpp
> > @@ -8,6 +8,7 @@
> >  #include "exif.h"
> >  
> >  #include "libcamera/internal/log.h"
> > +#include "libcamera/internal/utils.h"
> >  
> >  using namespace libcamera;
> >  
> > @@ -171,15 +172,19 @@ void Exif::setRational(ExifIfd ifd, ExifTag tag, ExifRational item)
> >  
> >  void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item)
> >  {
> > -	/* Pad 1 extra byte for null-terminated string in ASCII format. */
> > -	size_t length = format == EXIF_FORMAT_ASCII ?
> > -			item.length() + 1 : item.length();
> > +	size_t length = item.length();
> > +	std::string str = item;
> > +	if (format == EXIF_FORMAT_ASCII) {
> > +		str = utils::toAscii(str);
> > +		/* Pad 1 extra byte to null-terminate the ASCII string. */
> > +		length += 1;

length may have changed after the call to toAscii()

> 
> This could of course also be
> 		length++;
> 
> But += 1 is also quite readable here so I don't mind it staying.
> 
> Sanitizing the Ascii strings seems to make some sense, especially as we
> do it elsewhere - so here seems reasonable to me.
> 
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> 
> 
> > +	}
> >  
> >  	ExifEntry *entry = createEntry(ifd, tag, format, length, length);
> >  	if (!entry)
> >  		return;
> >  
> > -	memcpy(entry->data, item.c_str(), length);
> > +	memcpy(entry->data, str.c_str(), length);
> >  	exif_entry_unref(entry);
> >  }
> >
Umang Jain Oct. 5, 2020, 5:03 p.m. UTC | #3
Hi Laurent,

On 10/5/20 9:23 PM, Laurent Pinchart wrote:
> On Mon, Oct 05, 2020 at 04:39:53PM +0100, Kieran Bingham wrote:
>> Hi Umang,
>>
>> On 05/10/2020 14:11, Umang Jain wrote:
>>> Use the newly introduced utils::toAscii() utility to remove all
>>> non-ASCII characters for EXIF_FORMAT_ASCII strings.
>>>
>>> Signed-off-by: Umang Jain <email@uajain.com>
>>> ---
>>>   src/android/jpeg/exif.cpp | 13 +++++++++----
>>>   1 file changed, 9 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp
>>> index 3fd5d55..bd528a7 100644
>>> --- a/src/android/jpeg/exif.cpp
>>> +++ b/src/android/jpeg/exif.cpp
>>> @@ -8,6 +8,7 @@
>>>   #include "exif.h"
>>>   
>>>   #include "libcamera/internal/log.h"
>>> +#include "libcamera/internal/utils.h"
>>>   
>>>   using namespace libcamera;
>>>   
>>> @@ -171,15 +172,19 @@ void Exif::setRational(ExifIfd ifd, ExifTag tag, ExifRational item)
>>>   
>>>   void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item)
>>>   {
>>> -	/* Pad 1 extra byte for null-terminated string in ASCII format. */
>>> -	size_t length = format == EXIF_FORMAT_ASCII ?
>>> -			item.length() + 1 : item.length();
>>> +	size_t length = item.length();
>>> +	std::string str = item;
>>> +	if (format == EXIF_FORMAT_ASCII) {
>>> +		str = utils::toAscii(str);
>>> +		/* Pad 1 extra byte to null-terminate the ASCII string. */
>>> +		length += 1;
> length may have changed after the call to toAscii()
Oops, great catch. Submitted v2.
>
>> This could of course also be
>> 		length++;
>>
>> But += 1 is also quite readable here so I don't mind it staying.
>>
>> Sanitizing the Ascii strings seems to make some sense, especially as we
>> do it elsewhere - so here seems reasonable to me.
>>
>> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
>>
>>
>>> +	}
>>>   
>>>   	ExifEntry *entry = createEntry(ifd, tag, format, length, length);
>>>   	if (!entry)
>>>   		return;
>>>   
>>> -	memcpy(entry->data, item.c_str(), length);
>>> +	memcpy(entry->data, str.c_str(), length);
>>>   	exif_entry_unref(entry);
>>>   }
>>>

Patch

diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp
index 3fd5d55..bd528a7 100644
--- a/src/android/jpeg/exif.cpp
+++ b/src/android/jpeg/exif.cpp
@@ -8,6 +8,7 @@ 
 #include "exif.h"
 
 #include "libcamera/internal/log.h"
+#include "libcamera/internal/utils.h"
 
 using namespace libcamera;
 
@@ -171,15 +172,19 @@  void Exif::setRational(ExifIfd ifd, ExifTag tag, ExifRational item)
 
 void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item)
 {
-	/* Pad 1 extra byte for null-terminated string in ASCII format. */
-	size_t length = format == EXIF_FORMAT_ASCII ?
-			item.length() + 1 : item.length();
+	size_t length = item.length();
+	std::string str = item;
+	if (format == EXIF_FORMAT_ASCII) {
+		str = utils::toAscii(str);
+		/* Pad 1 extra byte to null-terminate the ASCII string. */
+		length += 1;
+	}
 
 	ExifEntry *entry = createEntry(ifd, tag, format, length, length);
 	if (!entry)
 		return;
 
-	memcpy(entry->data, item.c_str(), length);
+	memcpy(entry->data, str.c_str(), length);
 	exif_entry_unref(entry);
 }