add allocation error handling

This commit is contained in:
Stefan Wahren 2012-09-01 22:14:21 +02:00
parent e653894bce
commit d2608d5de7
5 changed files with 60 additions and 13 deletions

View File

@ -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;
@ -96,27 +96,37 @@ main(int argc, char **argv)
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;
}

View File

@ -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)
@ -47,6 +47,12 @@ main(int argc, char **argv)
return 0;
}
if (addr == NULL)
{
fprintf(stderr, "Failed to allocate address.\n");
return 1;
}
if (strlen(addr) != 16)
{
printf("Misformatted secondary address. Must be 16 character HEX number.\n");

View File

@ -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;
@ -51,6 +51,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)
{
fprintf(stderr, "Misformatted secondary address mask. Must be 16 character HEX number.\n");
@ -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;
}

View File

@ -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 (strlen(argv[3]) != 16)
if ((addr = strdup(argv[3])) == NULL)
{
fprintf(stderr, "Failed to allocate address.\n");
return 1;
}
if (strlen(addr) != 16)
{
printf("Misformatted secondary address. Must be 16 character HEX number.\n");
return 1;

View File

@ -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;