@@ -798,45 +798,65 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t mtp_container_add_array(mtp_contain
798798}
799799
800800TU_ATTR_ALWAYS_INLINE static inline uint32_t mtp_container_add_string (mtp_container_info_t * p_container , uint16_t * utf16 ) {
801- uint8_t count = 0 ;
801+ uint32_t count = 0 ;
802802 while (utf16 [count ] != 0u ) {
803803 count ++ ;
804804 }
805- const uint32_t added_len = 1u + (uint32_t ) count * 2u ;
806- TU_ASSERT (p_container -> header -> len + added_len < CFG_TUD_MTP_EP_BUFSIZE , 0 );
805+ // MTP strings store length in a single uint8_t, including trailing null.
806+ TU_ASSERT (count < UINT8_MAX , 0 );
807+ count ++ ;
808+
807809 uint8_t * buf = p_container -> payload + p_container -> header -> len - sizeof (mtp_container_header_t );
808810
809- * buf ++ = count ;
811+ if (count == 1 ) {
812+ // empty string (size only): single zero byte
813+ TU_ASSERT (p_container -> header -> len + 1 < CFG_TUD_MTP_EP_BUFSIZE , 0 );
814+ * buf = 0 ;
815+ p_container -> header -> len ++ ;
816+ return 1u ;
817+ }
818+
819+ const uint32_t added_len = 1u + count * 2u ;
820+ TU_ASSERT (p_container -> header -> len + added_len < CFG_TUD_MTP_EP_BUFSIZE , 0 );
821+
822+ * buf ++ = (uint8_t ) count ;
810823 p_container -> header -> len ++ ;
811824
812- memcpy (buf , utf16 , 2u * ( uint32_t ) count );
825+ memcpy (buf , utf16 , 2u * count );
813826 p_container -> header -> len += 2u * count ;
814827
815828 return added_len ;
816829}
817830
818831TU_ATTR_ALWAYS_INLINE static inline uint32_t mtp_container_add_cstring (mtp_container_info_t * p_container , const char * str ) {
819- const uint8_t len = (uint8_t ) (strlen (str ) + 1 ); // include null
820- TU_ASSERT (p_container -> header -> len + 1 + 2 * len < CFG_TUD_MTP_EP_BUFSIZE , 0 );
832+ const size_t cstr_len = strlen (str );
833+ // MTP strings store length in a single uint8_t, including trailing null.
834+ TU_ASSERT (cstr_len < UINT8_MAX , 0 );
835+
836+ const uint32_t count = (uint32_t ) cstr_len + 1u ; // include null
821837 uint8_t * buf = p_container -> payload + p_container -> header -> len - sizeof (mtp_container_header_t );
822838
823- if (len == 1 ) {
824- // empty string (null only): single zero byte
839+ if (count == 1u ) {
840+ // empty string (size only): single zero byte
841+ TU_ASSERT (p_container -> header -> len + 1 < CFG_TUD_MTP_EP_BUFSIZE , 0 );
825842 * buf = 0 ;
826843 p_container -> header -> len ++ ;
827844 return 1u ;
828- } else {
829- * buf ++ = len ;
830- p_container -> header -> len ++ ;
845+ }
831846
832- for (uint8_t i = 0 ; i < len ; i ++ ) {
833- buf [0 ] = str [i ];
834- buf [1 ] = 0 ;
835- buf += 2 ;
836- p_container -> header -> len += 2 ;
837- }
838- return 1u + 2u * len ;
847+ const uint32_t added_len = 1u + 2u * count ;
848+ TU_ASSERT (p_container -> header -> len + added_len < CFG_TUD_MTP_EP_BUFSIZE , 0 );
849+
850+ * buf ++ = (uint8_t ) count ;
851+ p_container -> header -> len ++ ;
852+
853+ for (uint32_t i = 0 ; i < count ; i ++ ) {
854+ * buf ++ = (uint8_t ) str [i ];
855+ * buf ++ = 0 ;
839856 }
857+ p_container -> header -> len += 2u * count ;
858+
859+ return added_len ;
840860}
841861
842862TU_ATTR_ALWAYS_INLINE static inline uint32_t mtp_container_add_uint8 (mtp_container_info_t * p_container , uint8_t data ) {
0 commit comments