Fix initialization of mapping structs

This commit is contained in:
Cameron Gutman 2021-08-07 18:07:17 -05:00
parent 469b2ed866
commit 7ff0bcd8fd
2 changed files with 7 additions and 2 deletions

View File

@ -22,6 +22,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
struct mapping* mapping_parse(char* mapping) {
@ -31,7 +32,7 @@ struct mapping* mapping_parse(char* mapping) {
if (guid == NULL || name == NULL)
return NULL;
struct mapping* map = malloc(sizeof(struct mapping));
struct mapping* map = calloc(sizeof(struct mapping), 1);
if (map == NULL) {
fprintf(stderr, "Not enough memory");
exit(EXIT_FAILURE);
@ -39,7 +40,9 @@ struct mapping* mapping_parse(char* mapping) {
strncpy(map->guid, guid, sizeof(map->guid));
strncpy(map->name, name, sizeof(map->name));
memset(&map->abs_leftx, -1, sizeof(short) * 31);
/* Initialize all mapping indices to -1 to ensure they won't match anything */
memset(&map->abs_leftx, -1, offsetof(struct mapping, next) - offsetof(struct mapping, abs_leftx));
char* option;
while ((option = strtok_r(NULL, ",", &strpoint)) != NULL) {

View File

@ -32,6 +32,7 @@ struct mapping {
bool halfaxis_dpright, halfaxis_dpleft;
bool halfaxis_dpup, halfaxis_dpdown;
/* abs_leftx must be the first member of the list of mapping indices! */
short abs_leftx, abs_lefty;
short abs_rightx, abs_righty;
@ -48,6 +49,7 @@ struct mapping {
short abs_lefttrigger, abs_righttrigger;
short btn_lefttrigger, btn_righttrigger;
/* next must be the last member after the list of mapping indices! */
struct mapping* next;
};