From 1229a0a42ac6087b04cc89309c7fa30f3cb13b41 Mon Sep 17 00:00:00 2001 From: Nate Bargmann Date: Sat, 7 Jan 2012 20:36:50 -0600 Subject: [PATCH] Assure NULL terminated strings in tests programs. Various strncpy operations could result in a port pathname that is not a NULL terminated string as the allowed string length is the same size as the buffer per the strncpy manual page. This is corrected by assuring that the allowed length is FILPATHLEN - 1. --- tests/dumpmem.c | 2 +- tests/example.c | 6 +++--- tests/rig_bench.c | 10 +++++----- tests/rigmem.c | 2 +- tests/rigsmtr.c | 4 ++-- tests/rigswr.c | 4 ++-- tests/rotctl.c | 2 +- tests/rotctld.c | 2 +- tests/testrig.c | 28 ++++++++++++++-------------- tests/testtrn.c | 14 +++++++------- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/tests/dumpmem.c b/tests/dumpmem.c index a1aeba71c..ed842e37e 100644 --- a/tests/dumpmem.c +++ b/tests/dumpmem.c @@ -50,7 +50,7 @@ int main (int argc, char *argv[]) exit(1); /* whoops! something went wrong (mem alloc?) */ } - strncpy(my_rig->state.rigport.pathname, SERIAL_PORT, FILPATHLEN); + strncpy(my_rig->state.rigport.pathname, SERIAL_PORT, FILPATHLEN - 1); if (rig_open(my_rig)) exit(2); diff --git a/tests/example.c b/tests/example.c index 1a6dbcfc2..2ef8a11a3 100644 --- a/tests/example.c +++ b/tests/example.c @@ -1,5 +1,5 @@ /* This is a elementary program calling Hamlib to do some useful things. - * + * * Edit to specify your rig model and serial port, and baud rate * before compiling. * To compile: @@ -29,7 +29,7 @@ int main() { my_rig = rig_init(RIG_MODEL_TT565); // your rig model. /* Set up serial port, baud rate */ rig_file = "/dev/ttyUSB0"; // your serial device - strncpy(my_rig->state.rigport.pathname, rig_file, FILPATHLEN); + strncpy(my_rig->state.rigport.pathname, rig_file, FILPATHLEN - 1); my_rig->state.rigport.parm.serial.rate = 57600; // your baud rate /* Open my rig */ retcode = rig_open(my_rig); @@ -38,7 +38,7 @@ int main() { printf("Rig_info: '%s'\n", info_buf); /* Note: As a general practice, we should check to see if a given - * function is within the rig's capabilities before calling it, but + * function is within the rig's capabilities before calling it, but * we are simplifying here. Also, we should check each call's returned * status in case of error. (That's an inelegant way to catch an unsupported * operation.) diff --git a/tests/rig_bench.c b/tests/rig_bench.c index 4cf9a4377..26b064cf7 100644 --- a/tests/rig_bench.c +++ b/tests/rig_bench.c @@ -1,4 +1,4 @@ -/* +/* * Hamlib rig_bench program */ @@ -15,7 +15,7 @@ #define SERIAL_PORT "/dev/ttyS0" int main (int argc, char *argv[]) -{ +{ RIG *my_rig; /* handle to rig (nstance) */ int retcode; /* generic return code from functions */ rig_model_t myrig_model; @@ -26,7 +26,7 @@ int main (int argc, char *argv[]) rig_set_debug(RIG_DEBUG_ERR); /* - * allocate memory, setup & open port + * allocate memory, setup & open port */ if (argc < 2) { @@ -38,7 +38,7 @@ int main (int argc, char *argv[]) myport.parm.serial.stop_bits = 1; myport.parm.serial.parity = RIG_PARITY_NONE; myport.parm.serial.handshake = RIG_HANDSHAKE_NONE; - strncpy(myport.pathname, SERIAL_PORT, FILPATHLEN); + strncpy(myport.pathname, SERIAL_PORT, FILPATHLEN - 1); rig_load_all_backends(); myrig_model = rig_probe(&myport); @@ -60,7 +60,7 @@ int main (int argc, char *argv[]) my_rig->caps->version, rig_strstatus(my_rig->caps->status)); printf("Serial speed: %d bauds\n", my_rig->state.rigport.parm.serial.rate); - strncpy(my_rig->state.rigport.pathname,SERIAL_PORT,FILPATHLEN); + strncpy(my_rig->state.rigport.pathname,SERIAL_PORT,FILPATHLEN - 1); retcode = rig_open(my_rig); if (retcode != RIG_OK) { diff --git a/tests/rigmem.c b/tests/rigmem.c index 02fdb8d26..7bd720c59 100644 --- a/tests/rigmem.c +++ b/tests/rigmem.c @@ -228,7 +228,7 @@ int main (int argc, char *argv[]) } if (rig_file) - strncpy(rig->state.rigport.pathname, rig_file, FILPATHLEN); + strncpy(rig->state.rigport.pathname, rig_file, FILPATHLEN - 1); /* FIXME: bound checking and port type == serial */ if (serial_rate != 0) diff --git a/tests/rigsmtr.c b/tests/rigsmtr.c index 46c39d466..ab346d60a 100644 --- a/tests/rigsmtr.c +++ b/tests/rigsmtr.c @@ -212,7 +212,7 @@ int main (int argc, char *argv[]) } if (rig_file) - strncpy(rig->state.rigport.pathname, rig_file, FILPATHLEN); + strncpy(rig->state.rigport.pathname, rig_file, FILPATHLEN - 1); /* FIXME: bound checking and port type == serial */ if (serial_rate != 0) @@ -257,7 +257,7 @@ int main (int argc, char *argv[]) } if (rot_file) - strncpy(rot->state.rotport.pathname, rot_file, FILPATHLEN); + strncpy(rot->state.rotport.pathname, rot_file, FILPATHLEN - 1); /* FIXME: bound checking and port type == serial */ if (rot_serial_rate != 0) diff --git a/tests/rigswr.c b/tests/rigswr.c index 80ba56273..ef8326024 100644 --- a/tests/rigswr.c +++ b/tests/rigswr.c @@ -202,10 +202,10 @@ int main (int argc, char *argv[]) if (ptt_type != RIG_PTT_NONE) rig->state.pttport.type.ptt = ptt_type; if (ptt_file) - strncpy(rig->state.pttport.pathname, ptt_file, FILPATHLEN); + strncpy(rig->state.pttport.pathname, ptt_file, FILPATHLEN - 1); if (rig_file) - strncpy(rig->state.rigport.pathname, rig_file, FILPATHLEN); + strncpy(rig->state.rigport.pathname, rig_file, FILPATHLEN - 1); /* FIXME: bound checking and port type == serial */ if (serial_rate != 0) diff --git a/tests/rotctl.c b/tests/rotctl.c index d94fa1182..99c9f4fa8 100644 --- a/tests/rotctl.c +++ b/tests/rotctl.c @@ -193,7 +193,7 @@ int main (int argc, char *argv[]) } if (rot_file) - strncpy(my_rot->state.rotport.pathname, rot_file, FILPATHLEN); + strncpy(my_rot->state.rotport.pathname, rot_file, FILPATHLEN - 1); /* FIXME: bound checking and port type == serial */ if (serial_rate != 0) diff --git a/tests/rotctld.c b/tests/rotctld.c index b995d4e32..402c58e2d 100644 --- a/tests/rotctld.c +++ b/tests/rotctld.c @@ -231,7 +231,7 @@ int main (int argc, char *argv[]) } if (rot_file) - strncpy(my_rot->state.rotport.pathname, rot_file, FILPATHLEN); + strncpy(my_rot->state.rotport.pathname, rot_file, FILPATHLEN - 1); /* FIXME: bound checking and port type == serial */ if (serial_rate != 0) diff --git a/tests/testrig.c b/tests/testrig.c index 6ba6deb18..735b5a6ef 100644 --- a/tests/testrig.c +++ b/tests/testrig.c @@ -1,4 +1,4 @@ -/* +/* * Hamlib sample program */ @@ -11,7 +11,7 @@ #define SERIAL_PORT "/dev/ttyS0" int main (int argc, char *argv[]) -{ +{ RIG *my_rig; /* handle to rig (nstance) */ freq_t freq; /* frequency */ rmode_t rmode; /* radio mode of operation */ @@ -25,7 +25,7 @@ int main (int argc, char *argv[]) printf("testrig:hello, I am your main() !\n"); /* - * allocate memory, setup & open port + * allocate memory, setup & open port */ if (argc < 2) { @@ -37,7 +37,7 @@ int main (int argc, char *argv[]) myport.parm.serial.stop_bits = 1; myport.parm.serial.parity = RIG_PARITY_NONE; myport.parm.serial.handshake = RIG_HANDSHAKE_NONE; - strncpy(myport.pathname, SERIAL_PORT, FILPATHLEN); + strncpy(myport.pathname, SERIAL_PORT, FILPATHLEN - 1); rig_load_all_backends(); myrig_model = rig_probe(&myport); @@ -46,14 +46,14 @@ int main (int argc, char *argv[]) } my_rig = rig_init(myrig_model); - + if (!my_rig) { fprintf(stderr,"Unknown rig num: %d\n", myrig_model); fprintf(stderr,"Please check riglist.h\n"); exit(1); /* whoops! something went wrong (mem alloc?) */ } - strncpy(my_rig->state.rigport.pathname,SERIAL_PORT,FILPATHLEN); + strncpy(my_rig->state.rigport.pathname,SERIAL_PORT,FILPATHLEN - 1); retcode = rig_open(my_rig); if (retcode != RIG_OK) { @@ -87,11 +87,11 @@ int main (int argc, char *argv[]) * Examples of checking return code are further down. * */ - + /* 10m FM Narrow */ retcode = rig_set_freq(my_rig, RIG_VFO_CURR, 28350125); /* 10m */ - retcode = rig_set_mode(my_rig, RIG_VFO_CURR, RIG_MODE_FM, + retcode = rig_set_mode(my_rig, RIG_VFO_CURR, RIG_MODE_FM, rig_passband_narrow(my_rig, RIG_MODE_FM)); sleep(3); /* so you can see it -- FS */ @@ -116,7 +116,7 @@ int main (int argc, char *argv[]) /* 80m AM NArrow */ retcode = rig_set_freq(my_rig, RIG_VFO_CURR, 3980000); /* 80m */ - retcode = rig_set_mode(my_rig, RIG_VFO_CURR, RIG_MODE_AM, + retcode = rig_set_mode(my_rig, RIG_VFO_CURR, RIG_MODE_AM, rig_passband_narrow(my_rig, RIG_MODE_FM)); sleep(3); @@ -154,19 +154,19 @@ int main (int argc, char *argv[]) if (retcode != RIG_OK ) { printf("rig_set_freq: error = %s \n", rigerror(retcode)); - } + } retcode = rig_set_mode(my_rig, RIG_VFO_CURR, RIG_MODE_LSB, RIG_PASSBAND_NORMAL); if (retcode != RIG_OK ) { printf("rig_set_mode: error = %s \n", rigerror(retcode)); - } + } retcode = rig_set_ptt(my_rig, RIG_VFO_A, RIG_PTT_ON ); /* stand back ! */ if (retcode != RIG_OK ) { printf("rig_set_ptt: error = %s \n", rigerror(retcode)); - } + } sleep(1); @@ -174,7 +174,7 @@ int main (int argc, char *argv[]) if (retcode != RIG_OK ) { printf("rig_set_ptt: error = %s \n", rigerror(retcode)); - } + } sleep(1); @@ -192,7 +192,7 @@ int main (int argc, char *argv[]) } retcode = rig_get_freq(my_rig, RIG_VFO_CURR, &freq); - + if (retcode == RIG_OK ) { printf("rig_get_freq: freq = %"PRIfreq"\n", freq); } else { diff --git a/tests/testtrn.c b/tests/testtrn.c index 73288de8e..51291bba4 100644 --- a/tests/testtrn.c +++ b/tests/testtrn.c @@ -1,4 +1,4 @@ -/* +/* * Hamlib sample program to test transceive mode (async event) */ @@ -22,7 +22,7 @@ int myfreq_event(RIG *rig, vfo_t vfo, freq_t freq, rig_ptr_t arg) int main (int argc, char *argv[]) -{ +{ RIG *my_rig; /* handle to rig (nstance) */ int retcode; /* generic return code from functions */ int i, count = 0; @@ -35,7 +35,7 @@ int main (int argc, char *argv[]) printf("testrig:hello, I am your main() !\n"); /* - * allocate memory, setup & open port + * allocate memory, setup & open port */ my_rig = rig_init(atoi(argv[1])); @@ -45,7 +45,7 @@ int main (int argc, char *argv[]) exit(1); /* whoops! something went wrong (mem alloc?) */ } - strncpy(my_rig->state.rigport.pathname, SERIAL_PORT, FILPATHLEN); + strncpy(my_rig->state.rigport.pathname, SERIAL_PORT, FILPATHLEN - 1); if (rig_open(my_rig)) exit(2); @@ -57,13 +57,13 @@ int main (int argc, char *argv[]) * Must add checking of functionality map prior to command execution -- FS * */ - + retcode = rig_set_freq(my_rig, RIG_VFO_CURR, 439700000); if (retcode != RIG_OK ) { printf("rig_set_freq: error = %s \n", rigerror(retcode)); - } + } rig_set_freq_callback(my_rig, myfreq_event, (rig_ptr_t)&count); @@ -71,7 +71,7 @@ int main (int argc, char *argv[]) if (retcode != RIG_OK ) { printf("rig_set_trn: error = %s \n", rigerror(retcode)); - } + } for (i=0;i<12;i++)