소스 검색

Fix memory errors

Several struct members were left uninitialized in some paths of the
code. Fix this by initializing all struct members to 0 in the affected
places.

Also fix an incorrectly sized malloc, where the size of a pointer
instead of the struct size was supplied to malloc.
Christoph Böhmwalder 6 년 전
부모
커밋
05fbbd427b
2개의 변경된 파일4개의 추가작업 그리고 1개의 파일을 삭제
  1. 3
    0
      src/impl/sysfs.c
  2. 1
    1
      src/light.c

+ 3
- 0
src/impl/sysfs.c 파일 보기

@@ -5,12 +5,14 @@
5 5
 
6 6
 #include <stdio.h> //snprintf
7 7
 #include <stdlib.h> // malloc, free
8
+#include <string.h> // memset
8 9
 #include <dirent.h> // opendir, readdir
9 10
 
10 11
 static bool _impl_sysfs_init_leds(light_device_enumerator_t *enumerator)
11 12
 {
12 13
     // Create a new backlight device
13 14
     light_device_t *leds_device = malloc(sizeof(light_device_t));
15
+    memset(leds_device, 0, sizeof(light_device_t));
14 16
     snprintf(leds_device->name, sizeof(leds_device->name), "%s", "leds");
15 17
     
16 18
     // Add it to the enumerator 
@@ -63,6 +65,7 @@ static bool _impl_sysfs_init_backlight(light_device_enumerator_t *enumerator)
63 65
 {
64 66
     // Create a new backlight device
65 67
     light_device_t *backlight_device = malloc(sizeof(light_device_t));
68
+    memset(backlight_device, 0, sizeof(light_device_t));
66 69
     snprintf(backlight_device->name, sizeof(backlight_device->name), "%s", "backlight");
67 70
     
68 71
     // Add it to the enumerator 

+ 1
- 1
src/light.c 파일 보기

@@ -435,7 +435,7 @@ light_device_enumerator_t * light_create_enumerator(light_context_t *ctx, char c
435 435
     }
436 436
     
437 437
     // Allocate the new enumerator
438
-    new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t*));
438
+    new_enumerators[ctx->num_enumerators] = malloc(sizeof(light_device_enumerator_t));
439 439
     light_device_enumerator_t *returner = new_enumerators[ctx->num_enumerators];
440 440
     
441 441
     returner->devices = NULL;