Revision: 45543
http://sourceforge.net/p/vice-emu/code/45543
Author: compyx
Date: 2025-03-18 06:11:47 +0000 (Tue, 18 Mar 2025)
Log Message:
-----------
Joystick: set limit on number of axes, buttons and hats per device
Set arbitrary (1024) limit on number of axes, buttons and hats that can be
added to a device when calling `joystick_device_add_[axis|button|hat]()`.
Log a warning when the maximum is reached, so we can identify what's happening
rather than VICE eating memory and swap space.
Modified Paths:
--------------
branches/compyx/joymap-001/vice/src/joyport/joystick.c
Modified: branches/compyx/joymap-001/vice/src/joyport/joystick.c
===================================================================
--- branches/compyx/joymap-001/vice/src/joyport/joystick.c 2025-03-17 21:29:53 UTC (rev 45542)
+++ branches/compyx/joymap-001/vice/src/joyport/joystick.c 2025-03-18 06:11:47 UTC (rev 45543)
@@ -3253,13 +3253,37 @@
/* NEW shit */
+/** \brief Initial size of the axes array of a joystick device */
+#define NUM_AXES_INITIAL 8
-#define NUM_AXES_INITIAL 8
+/** \brief Initial size of the buttons array of a joystick device */
#define NUM_BUTTONS_INITIAL 32
+
+/** \brief Initial size of the hats array of a joystick device */
#define NUM_HATS_INITIAL 4
+/** \brief Maximum number of axes allowed
+ *
+ * When this number of axes for a device is reached further axes added via
+ * #joystick_device_add_axis() will be ignored (and freed).
+ */
+#define NUM_AXES_MAX 1024
+/** \brief Maximum number of buttons allowed
+ *
+ * When this number of buttons for a device is reached further buttons added via
+ * #joystick_device_add_button() will be ignored (and freed).
+ */
+#define NUM_BUTTONS_MAX 1024
+/** \brief Maximum number of hat allowed
+ *
+ * When this number of hat for a device is reached further hats added via
+ * #joystick_device_add_hat() will be ignored (and freed).
+ */
+#define NUM_HATS_MAX 1024
+
+
void joystick_driver_register(const joystick_driver_t *driver)
{
joy_driver = *driver;
@@ -3651,6 +3675,14 @@
void joystick_device_add_axis(joystick_device_t *joydev,
joystick_axis_t *axis)
{
+ if (joydev->max_axes >= NUM_AXES_MAX) {
+ log_warning(joy_log,
+ "maximum (%d) number of axes reached, ignoring axis '%s'",
+ NUM_AXES_MAX, axis->name);
+ joystick_axis_free(axis);
+ return;
+ }
+
if (joydev->num_axes == joydev->max_axes) {
joydev->max_axes *= 2;
joydev->axes = lib_realloc(joydev->axes,
@@ -3671,6 +3703,14 @@
void joystick_device_add_button(joystick_device_t *joydev,
joystick_button_t *button)
{
+ if (joydev->max_buttons >= NUM_BUTTONS_MAX) {
+ log_warning(joy_log,
+ "maximum (%d) number of buttons reached, ignoring button '%s'",
+ NUM_BUTTONS_MAX, button->name);
+ joystick_button_free(button);
+ return;
+ }
+
if (joydev->num_buttons == joydev->max_buttons) {
joydev->max_buttons *= 2;
joydev->buttons = lib_realloc(joydev->buttons,
@@ -3691,6 +3731,14 @@
void joystick_device_add_hat(joystick_device_t *joydev,
joystick_hat_t *hat)
{
+ if (joydev->max_hats >= NUM_HATS_MAX) {
+ log_warning(joy_log,
+ "maximum (%d) number of hats reached, ignoring hat '%s'",
+ NUM_HATS_MAX, hat->name);
+ joystick_hat_free(hat);
+ return;
+ }
+
if (joydev->num_hats == joydev->max_hats) {
joydev->num_hats *= 2;
joydev->hats = lib_realloc(joydev->hats,
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|