bugfix: was overwriting short constant string or argv

r3633 | dcl | 2012-07-11 11:56:25 +1000 (Wed, 11 Jul 2012) | 1 line
This commit is contained in:
Douglas Clowes
2012-07-11 11:56:25 +10:00
parent ff2657c5e3
commit 556b69063c

View File

@@ -21,7 +21,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
#include <libiberty.h> /* #include <libiberty.h> */
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
@@ -43,7 +43,7 @@ int main(int argc, char* argv[])
char errBuff[2048]={'\0'}; char errBuff[2048]={'\0'};
struct timeval now; struct timeval now;
uint64 last_poll; uint64 last_poll;
char* device = DEFAULT_DEVICE; char device_name[132] = DEFAULT_DEVICE;
uint dev_no; uint dev_no;
int port = DEFAULT_LISTEN_PORT; int port = DEFAULT_LISTEN_PORT;
int idx = 1; int idx = 1;
@@ -70,7 +70,8 @@ int main(int argc, char* argv[])
tolower(argv[idx][2]) == 'v' && tolower(argv[idx][2]) == 'v' &&
isdigit(argv[idx][3])) isdigit(argv[idx][3]))
{ {
device = argv[idx]; strncpy(device_name, argv[idx], sizeof(device_name) - 1);
device_name[sizeof(device_name) - 1] = '\0';
++idx; ++idx;
} }
else if (tolower(argv[idx][0]) == 'p' && else if (tolower(argv[idx][0]) == 'p' &&
@@ -78,7 +79,8 @@ int main(int argc, char* argv[])
tolower(argv[idx][2]) == 'i' && tolower(argv[idx][2]) == 'i' &&
isdigit(argv[idx][3])) isdigit(argv[idx][3]))
{ {
device = argv[idx]; strncpy(device_name, argv[idx], sizeof(device_name) - 1);
device_name[sizeof(device_name) - 1] = '\0';
++idx; ++idx;
} }
else else
@@ -94,13 +96,13 @@ int main(int argc, char* argv[])
else else
return usage(argc, argv, "bad port"); return usage(argc, argv, "bad port");
dev_no = atoi(&device[3]); dev_no = atoi(&device_name[3]);
sock_init(); sock_init();
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
for (idx = 0; idx < MAX_DEVICES; ++idx) for (idx = 0; idx < MAX_DEVICES; ++idx)
{ {
sprintf(device, "dev%d/ctr%d", dev_no, idx); sprintf(device_name, "dev%d/ctr%d", dev_no, idx);
DEVICE_CHK(device_init(&devices[idx], device)); DEVICE_CHK(device_init(&devices[idx], device_name));
sock_listen(port + idx, device_command, devices[idx]); sock_listen(port + idx, device_command, devices[idx]);
devices[idx]->current_time = now; devices[idx]->current_time = now;
} }