Skip to content

Commit 188a42b

Browse files
LuoZhongYaonashif
authored andcommitted
usb: device: fix unaligned memory access in Audio class
Use UNALIGNED_*, sys_get_* to fix unaligned memory access issues Signed-off-by: ZhongYao Luo <[email protected]>
1 parent 04c70d6 commit 188a42b

File tree

3 files changed

+6
-5
lines changed
  • samples/subsys/usb/audio
  • subsys/usb/device/class/audio

3 files changed

+6
-5
lines changed

samples/subsys/usb/audio/headphones_microphone/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void feature_update(const struct device *dev,
5353
case USB_AUDIO_FU_MUTE_CONTROL:
5454
break;
5555
case USB_AUDIO_FU_VOLUME_CONTROL:
56-
volume = *((int16_t *)(evt->val));
56+
volume = UNALIGNED_GET((int16_t *)evt->val);
5757
LOG_INF("set volume: %d", volume);
5858
break;
5959
default:

samples/subsys/usb/audio/headset/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void feature_update(const struct device *dev,
5151
case USB_AUDIO_FU_MUTE_CONTROL:
5252
break;
5353
case USB_AUDIO_FU_VOLUME_CONTROL:
54-
volume = *((int16_t *)(evt->val));
54+
volume = UNALIGNED_GET((int16_t *)evt->val);
5555
LOG_INF("set volume: %d", volume);
5656
break;
5757
default:

subsys/usb/device/class/audio/audio.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ static int handle_fu_volume_req(struct usb_audio_dev_data *audio_dev_data,
577577
return -EINVAL;
578578
}
579579
if (setup->bRequest == USB_AUDIO_SET_CUR) {
580-
target_vol = *((int16_t *)*data);
580+
target_vol = sys_get_le16(*data);
581581
if (!IN_RANGE(target_vol, audio_dev_data->volumes.volume_min,
582582
audio_dev_data->volumes.volume_max)) {
583583
LOG_ERR("Volume out of range: %d", target_vol);
@@ -587,15 +587,16 @@ static int handle_fu_volume_req(struct usb_audio_dev_data *audio_dev_data,
587587
target_vol = ROUND_UP(target_vol,
588588
audio_dev_data->volumes.volume_res);
589589
}
590+
591+
UNALIGNED_PUT(target_vol, (int16_t *)control_val);
590592
evt->val = control_val;
591593
evt->val_len = *len;
592-
*((int16_t *)evt->val) = sys_le16_to_cpu(target_vol);
593594
return 0;
594595
}
595596
} else {
596597
if (setup->bRequest == USB_AUDIO_GET_CUR) {
597598
*len = LEN(ch_cnt, VOLUME);
598-
temp_vol = sys_cpu_to_le16(*(int16_t *)control_val);
599+
temp_vol = sys_cpu_to_le16(UNALIGNED_GET((int16_t *)control_val));
599600
memcpy(*data, &temp_vol, *len);
600601
return 0;
601602
} else if (setup->bRequest == USB_AUDIO_GET_MIN) {

0 commit comments

Comments
 (0)