aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jim-aio.c7
-rw-r--r--jim-array.c1
-rw-r--r--jim-exec.c1
-rw-r--r--jim-interactive.c8
-rw-r--r--jim-load.c1
-rw-r--r--jim-package.c3
-rw-r--r--jim-posix.c12
-rw-r--r--jim-sqlite.c2
-rw-r--r--jim-sqlite3.c2
-rw-r--r--jim.c6
-rw-r--r--jimsh.c61
11 files changed, 66 insertions, 38 deletions
diff --git a/jim-aio.c b/jim-aio.c
index a6412ef..4cb06ea 100644
--- a/jim-aio.c
+++ b/jim-aio.c
@@ -60,8 +60,8 @@
#include "jim-subcmd.h"
-#define AIO_CMD_LEN 128
-#define AIO_BUF_LEN 1024
+#define AIO_CMD_LEN 32 /* e.g. aio.handleXXXXXX */
+#define AIO_BUF_LEN 256 /* Can keep this small and rely on stdio buffering */
#define AIO_KEEPOPEN 1
@@ -449,7 +449,8 @@ static int aio_cmd_recvfrom(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, rlen));
if (argc > 1) {
- char addrbuf[100];
+ /* INET6_ADDRSTRLEN is 46. Add some for [] and port */
+ char addrbuf[60];
#if IPV6
if (sa.sa.sa_family == AF_INET6) {
diff --git a/jim-array.c b/jim-array.c
index fa5bd74..d077cc8 100644
--- a/jim-array.c
+++ b/jim-array.c
@@ -101,7 +101,6 @@ static int array_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Jim_SetResult(interp, dictObj);
}
else {
- /* REVISIT: We could create a dictionary rather than a list ... */
/* Only return the matching values */
resultObj = Jim_NewListObj(interp, NULL, 0);
diff --git a/jim-exec.c b/jim-exec.c
index 8780298..92ec45b 100644
--- a/jim-exec.c
+++ b/jim-exec.c
@@ -27,7 +27,6 @@
#include "jim-subcmd.h"
#include "jim-signal.h"
-
/* These two could be moved into the Tcl core */
static void Jim_SetResultErrno(Jim_Interp *interp, const char *msg)
{
diff --git a/jim-interactive.c b/jim-interactive.c
index 2846309..4aa2923 100644
--- a/jim-interactive.c
+++ b/jim-interactive.c
@@ -1,16 +1,18 @@
#include "jim.h"
#include <errno.h>
+#define MAX_LINE_LEN 512
+
int Jim_InteractivePrompt(Jim_Interp *interp)
{
int retcode = JIM_OK;
Jim_Obj *scriptObjPtr;
+ char *buf = Jim_Alloc(MAX_LINE_LEN);
printf("Welcome to Jim version %d.%d, "
"Copyright (c) 2005-8 Salvatore Sanfilippo" JIM_NL, JIM_VERSION / 100, JIM_VERSION % 100);
Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, "1");
while (1) {
- char buf[1024];
const char *result;
int reslen;
@@ -35,7 +37,7 @@ int Jim_InteractivePrompt(Jim_Interp *interp)
int len;
errno = 0;
- if (fgets(buf, 1024, stdin) == NULL) {
+ if (fgets(buf, MAX_LINE_LEN, stdin) == NULL) {
if (errno == EINTR) {
continue;
}
@@ -56,6 +58,7 @@ int Jim_InteractivePrompt(Jim_Interp *interp)
Jim_PrintErrorMessage(interp);
}
else if (retcode == JIM_EXIT) {
+ Jim_Free(buf);
exit(Jim_GetExitCode(interp));
}
else {
@@ -65,5 +68,6 @@ int Jim_InteractivePrompt(Jim_Interp *interp)
}
}
out:
+ Jim_Free(buf);
return 0;
}
diff --git a/jim-load.c b/jim-load.c
index 7ce1fe2..f853a5e 100644
--- a/jim-load.c
+++ b/jim-load.c
@@ -32,6 +32,7 @@ int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName)
}
else {
FILE *fp;
+ /* REVISIT: Move off stack */
char buf[JIM_PATH_LEN];
const char *prefix;
int prefixlen;
diff --git a/jim-package.c b/jim-package.c
index e7f86ba..fd8314d 100644
--- a/jim-package.c
+++ b/jim-package.c
@@ -26,6 +26,7 @@ static char *JimFindPackage(Jim_Interp *interp, char **prefixes, int prefixc, co
int i;
for (i = 0; i < prefixc; i++) {
+ /* REVISIT: Move off stack */
char buf[JIM_PATH_LEN];
if (prefixes[i] == NULL)
@@ -42,10 +43,12 @@ static char *JimFindPackage(Jim_Interp *interp, char **prefixes, int prefixc, co
return Jim_StrDup(buf);
}
+#ifdef jim_ext_load
snprintf(buf, sizeof(buf), "%s/%s.so", prefixes[i], pkgName);
if (access(buf, R_OK) == 0) {
return Jim_StrDup(buf);
}
+#endif
}
return NULL;
}
diff --git a/jim-posix.c b/jim-posix.c
index 61f5af8..1013121 100644
--- a/jim-posix.c
+++ b/jim-posix.c
@@ -162,18 +162,22 @@ static int Jim_PosixGetidsCommand(Jim_Interp *interp, int argc, Jim_Obj *const *
#define JIM_HOST_NAME_MAX 1024
static int Jim_PosixGethostnameCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
- char buf[JIM_HOST_NAME_MAX];
+ char *buf;
+ int rc = JIM_OK;
if (argc != 1) {
Jim_WrongNumArgs(interp, 1, argv, "");
return JIM_ERR;
}
+ buf = Jim_Alloc(JIM_HOST_NAME_MAX);
if (gethostname(buf, JIM_HOST_NAME_MAX) == -1) {
Jim_PosixSetError(interp);
- return JIM_ERR;
+ rc = JIM_ERR;
}
- Jim_SetResultString(interp, buf, -1);
- return JIM_OK;
+ else {
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, -1));
+ }
+ return rc;
}
static int Jim_PosixUptimeCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
diff --git a/jim-sqlite.c b/jim-sqlite.c
index baa49b0..075b325 100644
--- a/jim-sqlite.c
+++ b/jim-sqlite.c
@@ -238,7 +238,7 @@ static int JimSqliteOpenCommand(Jim_Interp *interp, int argc, Jim_Obj *const *ar
{
sqlite *db;
JimSqliteHandle *sh;
- char buf[128], *errMsg;
+ char buf[60], *errMsg;
if (argc != 2) {
Jim_WrongNumArgs(interp, 1, argv, "dbname");
diff --git a/jim-sqlite3.c b/jim-sqlite3.c
index 369e3a5..8f603df 100644
--- a/jim-sqlite3.c
+++ b/jim-sqlite3.c
@@ -254,7 +254,7 @@ static int JimSqliteOpenCommand(Jim_Interp *interp, int argc, Jim_Obj *const *ar
{
sqlite3 *db;
JimSqliteHandle *sh;
- char buf[128];
+ char buf[60];
int r;
if (argc != 2) {
diff --git a/jim.c b/jim.c
index 5637e05..ac0599b 100644
--- a/jim.c
+++ b/jim.c
@@ -9057,6 +9057,7 @@ static void JimPrngSeed(Jim_Interp *interp, const unsigned char *seed, int seedL
static void JimPrngInit(Jim_Interp *interp)
{
int i;
+ /* REVISIT: Move off stack */
unsigned int seed[256];
interp->prngState = Jim_Alloc(sizeof(Jim_PrngState));
@@ -9092,6 +9093,7 @@ static void JimRandomBytes(Jim_Interp *interp, void *dest, unsigned int len)
static void JimPrngSeed(Jim_Interp *interp, const unsigned char *seed, int seedLen)
{
int i;
+ /* REVISIT: Move off stack */
unsigned char buf[256];
Jim_PrngState *prng;
@@ -11636,6 +11638,7 @@ static int Jim_DebugCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *ar
}
else if (option == OPT_OBJCOUNT) {
int freeobj = 0, liveobj = 0;
+ /* REVISIT: Move off stack */
char buf[256];
Jim_Obj *objPtr;
@@ -11667,6 +11670,7 @@ static int Jim_DebugCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *ar
objPtr = interp->liveList;
listObjPtr = Jim_NewListObj(interp, NULL, 0);
while (objPtr) {
+ /* REVISIT: Move off stack */
char buf[128];
const char *type = objPtr->typePtr ? objPtr->typePtr->name : "";
@@ -12458,7 +12462,7 @@ static int Jim_TimeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *arg
{
long i, count = 1;
jim_wide start, elapsed;
- char buf[256];
+ char buf[60];
const char *fmt = "%" JIM_WIDE_MODIFIER " microseconds per iteration";
if (argc < 2) {
diff --git a/jimsh.c b/jimsh.c
index a688fd8..9ce6ccf 100644
--- a/jimsh.c
+++ b/jimsh.c
@@ -25,7 +25,6 @@
#include "jim.h"
-
/* JimGetExePath try to get the absolute path of the directory
* of the jim binary, in order to add this path to the library path.
* Likely shipped libraries are in the same path too. */
@@ -35,39 +34,52 @@
#include <unistd.h>
static Jim_Obj *JimGetExePath(Jim_Interp *interp, const char *argv0)
{
- char path[JIM_PATH_LEN + 1];
+ char *p;
/* Check if the executable was called with an absolute pathname */
if (argv0[0] == '/') {
- char *p;
-
- strncpy(path, argv0, JIM_PATH_LEN);
- p = strrchr(path, '/');
- *(p + 1) = '\0';
- return Jim_NewStringObj(interp, path, -1);
+ p = strrchr(argv0, '/');
+ return Jim_NewStringObj(interp, argv0, (p == argv0) ? 1 : p - argv0);
}
else {
- char cwd[JIM_PATH_LEN + 1];
- char base[JIM_PATH_LEN + 1], *p;
int l;
+ char *path = Jim_Alloc(JIM_PATH_LEN + 1);
- strncpy(base, argv0, JIM_PATH_LEN);
- if (getcwd(cwd, JIM_PATH_LEN) == NULL) {
+ if (getcwd(path, JIM_PATH_LEN) == NULL) {
+default_path:
+ Jim_Free(path);
return Jim_NewStringObj(interp, "/usr/local/lib/jim/", -1);
}
- l = strlen(cwd);
- if (l > 0 && cwd[l - 1] == '/')
- cwd[l - 1] = '\0';
- p = strrchr(base, '/');
- if (p == NULL)
- base[0] = '\0';
- else if (p != base)
- *p = '\0';
- sprintf(path, "%s/%s", cwd, base);
+
+ /* Need to add the directory component of argv0 to pwd (path) */
+
+ /* Strip any leading "./" off argv0 for cleanliness */
+ while (argv0[0] == '.' && argv0[1] == '/') {
+ argv0 += 2;
+ }
+
l = strlen(path);
- if (l > 2 && path[l - 2] == '/' && path[l - 1] == '.')
- path[l - 1] = '\0';
- return Jim_NewStringObj(interp, path, -1);
+
+ /* Strip the last component from argv0 */
+ p = strrchr(argv0, '/');
+ if (p) {
+ int argv0len = p - argv0;
+
+ /* Need a trailing / on pwd */
+ if (l > 0 && path[l - 1] != '/')
+ path[l++] = '/';
+
+ /* And append it to 'path' */
+ if (l + argv0len > JIM_PATH_LEN) {
+ /* It won't fit. Don't both trying to realloc. */
+ goto default_path;
+ }
+ memcpy(path + l, argv0, argv0len);
+ l += argv0len;
+ path[l] = '\0';
+ }
+
+ return Jim_NewStringObjNoAlloc(interp, path, l);
}
}
#else /* JIM_ANSIC */
@@ -83,6 +95,7 @@ static Jim_Obj *JimGetExePath(Jim_Interp *interp, const char *argv0)
static void JimLoadJimRc(Jim_Interp *interp)
{
const char *home;
+ /* REVISIT: Move off stack */
char buf[JIM_PATH_LEN + 1];
const char *names[] = { ".jimrc", "jimrc.tcl", NULL };
int i;