Skip to content

Commit a2cca43

Browse files
authored
Merge pull request #3570 from IntegraSources/mtp-changes
mtp: fixes in add_string
2 parents 096458d + f9d8693 commit a2cca43

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

examples/device/mtp/src/mtp_fs_example.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,11 @@ int32_t tud_mtp_command_received_cb(tud_mtp_cb_data_t* cb_data) {
275275
resp_code = MTP_RESP_OPERATION_NOT_SUPPORTED;
276276
} else {
277277
resp_code = handler(cb_data);
278-
if (resp_code > MTP_RESP_UNDEFINED) {
279-
// send response if needed
280-
io_container->header->code = (uint16_t)resp_code;
281-
tud_mtp_response_send(io_container);
282-
}
278+
}
279+
if (resp_code > MTP_RESP_UNDEFINED) {
280+
// send response if needed
281+
io_container->header->code = (uint16_t)resp_code;
282+
tud_mtp_response_send(io_container);
283283
}
284284

285285
return resp_code;
@@ -302,11 +302,11 @@ int32_t tud_mtp_data_xfer_cb(tud_mtp_cb_data_t* cb_data) {
302302
resp_code = MTP_RESP_OPERATION_NOT_SUPPORTED;
303303
} else {
304304
resp_code = handler(cb_data);
305-
if (resp_code > MTP_RESP_UNDEFINED) {
306-
// send response if needed
307-
io_container->header->code = (uint16_t)resp_code;
308-
tud_mtp_response_send(io_container);
309-
}
305+
}
306+
if (resp_code > MTP_RESP_UNDEFINED) {
307+
// send response if needed
308+
io_container->header->code = (uint16_t)resp_code;
309+
tud_mtp_response_send(io_container);
310310
}
311311

312312
return 0;

src/class/mtp/mtp.h

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -798,45 +798,65 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t mtp_container_add_array(mtp_contain
798798
}
799799

800800
TU_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

818831
TU_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

842862
TU_ATTR_ALWAYS_INLINE static inline uint32_t mtp_container_add_uint8(mtp_container_info_t* p_container, uint8_t data) {

src/class/mtp/mtp_device.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ typedef struct {
7878
mtp_auint16_t(_capture_count) capture_formats; \
7979
mtp_auint16_t(_playback_count) playback_formats; \
8080
/* string fields will be added using append function */ \
81+
/* mtp_string_t() Manufacturer */ \
82+
/* mtp_string_t() Model */ \
83+
/* mtp_string_t() Device Version */ \
84+
/* mtp_string_t() Serial Number */ \
8185
}
8286

8387
typedef MTP_DEVICE_INFO_STRUCT( //-V2586 [MISRA-C-18.7] Flexible array members should not be declared

0 commit comments

Comments
 (0)