add allocation error handling
This commit is contained in:
parent
e653894bce
commit
d2608d5de7
@ -25,7 +25,7 @@ static int debug = 0;
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *device, *addr_mask;
|
||||
char *device, *addr_mask = NULL;
|
||||
int baudrate = 9600;
|
||||
mbus_handle *handle = NULL;
|
||||
mbus_frame *frame = NULL, reply;
|
||||
@ -95,28 +95,38 @@ main(int argc, char **argv)
|
||||
mbus_register_send_event(&mbus_dump_send_event);
|
||||
mbus_register_recv_event(&mbus_dump_recv_event);
|
||||
}
|
||||
|
||||
if (addr_mask == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate address mask.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strlen(addr_mask) != 16)
|
||||
{
|
||||
fprintf(stderr, "Misformatted secondary address mask. Must be 16 character HEX number.\n");
|
||||
free(addr_mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((handle = mbus_context_serial(device)) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
|
||||
free(addr_mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_connect(handle) == -1)
|
||||
{
|
||||
printf("Failed to setup connection to M-bus gateway\n");
|
||||
free(addr_mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (mbus_serial_set_baudrate(handle, baudrate) == -1)
|
||||
{
|
||||
fprintf(stderr, "Failed to set baud rate.\n");
|
||||
free(addr_mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -126,6 +136,7 @@ main(int argc, char **argv)
|
||||
if (frame == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate mbus frame.\n");
|
||||
free(addr_mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ main(int argc, char **argv)
|
||||
{
|
||||
mbus_handle *handle;
|
||||
mbus_frame reply;
|
||||
char *device, *addr;
|
||||
char *device, *addr = NULL;
|
||||
int ret, baudrate = 9600;
|
||||
|
||||
if (argc == 3)
|
||||
@ -46,6 +46,12 @@ main(int argc, char **argv)
|
||||
fprintf(stderr, " optional flag -b for selecting baudrate\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (addr == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate address.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strlen(addr) != 16)
|
||||
{
|
||||
|
@ -25,7 +25,7 @@ static int debug = 0;
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *host, *addr_mask;
|
||||
char *host, *addr_mask = NULL;
|
||||
int port;
|
||||
mbus_handle *handle = NULL;
|
||||
mbus_frame *frame = NULL, reply;
|
||||
@ -50,6 +50,12 @@ main(int argc, char **argv)
|
||||
{
|
||||
addr_mask = strdup("FFFFFFFFFFFFFFFF");
|
||||
}
|
||||
|
||||
if (addr_mask == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate address mask.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strlen(addr_mask) != 16)
|
||||
{
|
||||
@ -97,8 +103,7 @@ main(int argc, char **argv)
|
||||
|
||||
if (mbus_send_frame(handle, frame) == -1)
|
||||
{
|
||||
fprintf(stderr, "Failed to send SND_NKE #2.\n");
|
||||
mbus_frame_free(frame);
|
||||
free(addr_mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ main(int argc, char **argv)
|
||||
{
|
||||
mbus_handle *handle;
|
||||
mbus_frame reply;
|
||||
char *host, *addr;
|
||||
char *host, *addr = NULL;
|
||||
int port, ret;
|
||||
|
||||
if (argc != 4)
|
||||
@ -37,9 +37,14 @@ main(int argc, char **argv)
|
||||
|
||||
host = argv[1];
|
||||
port = atoi(argv[2]);
|
||||
addr = strdup(argv[3]);
|
||||
|
||||
if ((addr = strdup(argv[3])) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate address.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strlen(argv[3]) != 16)
|
||||
if (strlen(addr) != 16)
|
||||
{
|
||||
printf("Misformatted secondary address. Must be 16 character HEX number.\n");
|
||||
return 1;
|
||||
|
@ -791,7 +791,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
|
||||
switch (record->drh.dib.dif & 0x0F)
|
||||
{
|
||||
case 0x00: /* no data */
|
||||
*value_out_str = (char*) malloc(1);
|
||||
if ((*value_out_str = (char*) malloc(1)) == NULL)
|
||||
{
|
||||
MBUS_ERROR("Unable to allocate memory");
|
||||
return -1;
|
||||
}
|
||||
*value_out_str_size = 0;
|
||||
result = 0;
|
||||
break;
|
||||
@ -806,7 +810,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
|
||||
if (vif == 0x6C)
|
||||
{
|
||||
mbus_data_tm_decode(&time, record->data, 2);
|
||||
*value_out_str = (char*) malloc(11);
|
||||
if ((*value_out_str = (char*) malloc(11)) == NULL)
|
||||
{
|
||||
MBUS_ERROR("Unable to allocate memory");
|
||||
return -1;
|
||||
}
|
||||
*value_out_str_size = snprintf(*value_out_str, 11, "%04d-%02d-%02d",
|
||||
(time.tm_year + 2000),
|
||||
(time.tm_mon + 1),
|
||||
@ -834,7 +842,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
|
||||
((record->drh.vib.vif == 0xFD) && (vife == 0x70)))
|
||||
{
|
||||
mbus_data_tm_decode(&time, record->data, 4);
|
||||
*value_out_str = (char*) malloc(20);
|
||||
if ((*value_out_str = (char*) malloc(20)) == NULL)
|
||||
{
|
||||
MBUS_ERROR("Unable to allocate memory");
|
||||
return -1;
|
||||
}
|
||||
*value_out_str_size = snprintf(*value_out_str, 20, "%04d-%02d-%02dT%02d:%02d:%02d",
|
||||
(time.tm_year + 2000),
|
||||
(time.tm_mon + 1),
|
||||
@ -888,7 +900,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
|
||||
case 0x0D: /* variable length */
|
||||
{
|
||||
if (record->data_len <= 0xBF) {
|
||||
*value_out_str = (char*) malloc(record->data_len + 1);
|
||||
if ((*value_out_str = (char*) malloc(record->data_len + 1)) == NULL)
|
||||
{
|
||||
MBUS_ERROR("Unable to allocate memory");
|
||||
return -1;
|
||||
}
|
||||
*value_out_str_size = record->data_len;
|
||||
mbus_data_str_decode((u_char*)(*value_out_str), record->data, record->data_len);
|
||||
result = 0;
|
||||
@ -905,7 +921,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
|
||||
break;
|
||||
|
||||
case 0x0F: /* Special functions */
|
||||
*value_out_str = (char*) malloc(3 * record->data_len + 1);
|
||||
if ((*value_out_str = (char*) malloc(3 * record->data_len + 1)) == NULL)
|
||||
{
|
||||
MBUS_ERROR("Unable to allocate memory");
|
||||
return -1;
|
||||
}
|
||||
*value_out_str_size = 3 * record->data_len;
|
||||
mbus_data_bin_decode((u_char*)(*value_out_str), record->data, record->data_len, (3 * record->data_len + 1));
|
||||
result = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user