Merge "Better handling of empty ID3 tags"
diff --git a/media/libstagefright/id3/ID3.cpp b/media/libstagefright/id3/ID3.cpp
index 1b5db35..8f83cdb 100644
--- a/media/libstagefright/id3/ID3.cpp
+++ b/media/libstagefright/id3/ID3.cpp
@@ -77,7 +77,10 @@
       mFirstFrameOffset(0),
       mVersion(ID3_UNKNOWN),
       mRawSize(0) {
-    sp<MemorySource> source = new MemorySource(data, size);
+    sp<MemorySource> source = new (std::nothrow) MemorySource(data, size);
+
+    if (source == NULL)
+        return;
 
     mIsValid = parseV2(source, 0);
 
@@ -555,11 +558,15 @@
         const char16_t *framedata = (const char16_t *) (frameData + 1);
         char16_t *framedatacopy = NULL;
 #if BYTE_ORDER == LITTLE_ENDIAN
-        framedatacopy = new char16_t[len];
-        for (int i = 0; i < len; i++) {
-            framedatacopy[i] = bswap_16(framedata[i]);
+        if (len > 0) {
+            framedatacopy = new (std::nothrow) char16_t[len];
+            if (framedatacopy != NULL) {
+                for (int i = 0; i < len; i++) {
+                    framedatacopy[i] = bswap_16(framedata[i]);
+                }
+                framedata = framedatacopy;
+            }
         }
-        framedata = framedatacopy;
 #endif
         id->setTo(framedata, len);
         if (framedatacopy != NULL) {
@@ -573,11 +580,15 @@
         char16_t *framedatacopy = NULL;
         if (*framedata == 0xfffe) {
             // endianness marker doesn't match host endianness, convert
-            framedatacopy = new char16_t[len];
-            for (int i = 0; i < len; i++) {
-                framedatacopy[i] = bswap_16(framedata[i]);
+            if (len > 0) {
+                framedatacopy = new (std::nothrow) char16_t[len];
+                if (framedatacopy != NULL) {
+                    for (int i = 0; i < len; i++) {
+                        framedatacopy[i] = bswap_16(framedata[i]);
+                    }
+                    framedata = framedatacopy;
+                }
             }
-            framedata = framedatacopy;
         }
         // If the string starts with an endianness marker, skip it
         if (*framedata == 0xfeff) {
@@ -593,14 +604,16 @@
                 break;
             }
         }
-        if (eightBit) {
+        if (eightBit && len > 0) {
             // collapse to 8 bit, then let the media scanner client figure out the real encoding
-            char *frame8 = new char[len];
-            for (int i = 0; i < len; i++) {
-                frame8[i] = framedata[i];
+            char *frame8 = new (std::nothrow) char[len];
+            if (frame8 != NULL) {
+                for (int i = 0; i < len; i++) {
+                    frame8[i] = framedata[i];
+                }
+                id->setTo(frame8, len);
+                delete [] frame8;
             }
-            id->setTo(frame8, len);
-            delete [] frame8;
         } else {
             id->setTo(framedata, len);
         }