Commit 55bd5054e6964f8242d02b30e56b27d76ae786ce
1 parent
92266137
Initialize device argument to NULL on malloc (closes #184)
Showing
2 changed files
with
11 additions
and
0 deletions
src/modbus-rtu.c
| @@ -1151,6 +1151,7 @@ modbus_t* modbus_new_rtu(const char *device, | @@ -1151,6 +1151,7 @@ modbus_t* modbus_new_rtu(const char *device, | ||
| 1151 | ctx->backend = &_modbus_rtu_backend; | 1151 | ctx->backend = &_modbus_rtu_backend; |
| 1152 | ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t)); | 1152 | ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t)); |
| 1153 | ctx_rtu = (modbus_rtu_t *)ctx->backend_data; | 1153 | ctx_rtu = (modbus_rtu_t *)ctx->backend_data; |
| 1154 | + ctx_rtu->device = NULL; | ||
| 1154 | 1155 | ||
| 1155 | /* Check device argument */ | 1156 | /* Check device argument */ |
| 1156 | if (device == NULL || (*device) == 0) { | 1157 | if (device == NULL || (*device) == 0) { |
tests/unit-test-client.c
| @@ -835,6 +835,16 @@ int main(int argc, char *argv[]) | @@ -835,6 +835,16 @@ int main(int argc, char *argv[]) | ||
| 835 | goto close; | 835 | goto close; |
| 836 | } | 836 | } |
| 837 | 837 | ||
| 838 | + /* Test init functions */ | ||
| 839 | + printf("\nTEST INVALID INITIALIZATION:\n"); | ||
| 840 | + ctx = modbus_new_rtu(NULL, 0, 'A', 0, 0); | ||
| 841 | + if (ctx == NULL && errno == EINVAL) { | ||
| 842 | + printf("OK\n"); | ||
| 843 | + } else { | ||
| 844 | + printf("FAILED\n"); | ||
| 845 | + goto close; | ||
| 846 | + } | ||
| 847 | + | ||
| 838 | printf("\nALL TESTS PASS WITH SUCCESS.\n"); | 848 | printf("\nALL TESTS PASS WITH SUCCESS.\n"); |
| 839 | 849 | ||
| 840 | close: | 850 | close: |