USB HID Generic example: superflous memset() before memcpy()?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello folks,

I compiled and tested newest ASF example project "USB QTouch Device HID Generic Example - XMEGA-A3BU Xplained".

 

In src/ASF/common/services/usb/cdc/udi_hid_generic.c, the structure udi_hid_generic_report_in is first set 0, then overwritten. What's the reason for it?

 

bool udi_hid_generic_send_report_in(uint8_t *data)
{
	if (!udi_hid_generic_b_report_in_free)
		return false;
	irqflags_t flags = cpu_irq_save();
	// Fill report
	memset(&udi_hid_generic_report_in, 0, sizeof(udi_hid_generic_report_in));  // <<<
	memcpy(&udi_hid_generic_report_in, data, sizeof(udi_hid_generic_report_in)); // <<<< overwritten?
	udi_hid_generic_b_report_in_free =
			!udd_ep_run(UDI_HID_GENERIC_EP_IN,
							false,
							(uint8_t *) & udi_hid_generic_report_in,
							sizeof(udi_hid_generic_report_in),
							udi_hid_generic_report_in_sent);
	cpu_irq_restore(flags);
	return !udi_hid_generic_b_report_in_free;

}

Best regards,

Paule

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess someone just thought that would be "safer".

 

It does no harm - but, as you suggest, it is pointless.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No one ever accused ASF of "efficiency" ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

could be a mistaken belief that it will protect against "uninitialised" fields in the passed-in 'data' struct ... ?