Skip to content

Commit

Permalink
Don't use sprintf in cfuns-c.cxx (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielDosReis authored Dec 25, 2023
1 parent 467b6fb commit bbba2f2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
3 changes: 3 additions & 0 deletions config/open-axiom.m4
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ dnl -- OPENAXIOM_FILE_EXTENSIONS --
dnl -------------------------------
dnl Compute various file extensions used by the build system.
AC_DEFUN([OPENAXIOM_FILE_EXTENSIONS],[
# Path separator
AC_DEFINE_UNQUOTED([OPENAXIOM_PATH_SEPARATOR], ["$PATH_SEPARATOR"],
[Path separator on the host filesystem.])
# What is the extension of object and executable files on this platform?
AC_OBJEXT
AC_DEFINE_UNQUOTED([OPENAXIOM_EXEEXT], ["$ac_cv_exeext"],
Expand Down
18 changes: 9 additions & 9 deletions config/openaxiom-c-macros.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@
/* Define to 1 if you support file names longer than 14 characters. */
#undef HAVE_LONG_FILE_NAMES

/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H

/* Define to 1 if you have the <pty.h> header file. */
#undef HAVE_PTY_H

Expand All @@ -100,6 +97,9 @@
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H

/* Define to 1 if you have the <stdio.h> header file. */
#undef HAVE_STDIO_H

/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H

Expand Down Expand Up @@ -164,6 +164,9 @@
/* mmap anonymous flag */
#undef OPENAXIOM_MM_ANONYMOUS_MAP_FLAG

/* Path separator on the host filesystem. */
#undef OPENAXIOM_PATH_SEPARATOR

/* Whether to use the QT-based GUI interface as driver. */
#undef OPENAXIOM_USE_GUI

Expand Down Expand Up @@ -194,7 +197,9 @@
/* The size of `void*', as computed by sizeof. */
#undef SIZEOF_VOIDP

/* Define to 1 if you have the ANSI C header files. */
/* Define to 1 if all of the C90 standard headers exist (not just the ones
required in a freestanding environment). This macro is provided for
backward compatibility; new code need not use it. */
#undef STDC_HEADERS

/* Version number of package */
Expand All @@ -215,11 +220,6 @@
/* Define to 1 if the X Window System is missing or not being used. */
#undef X_DISPLAY_MISSING

/* Enable large inode numbers on Mac OS X 10.5. */
#ifndef _DARWIN_USE_64_BIT_INODE
# define _DARWIN_USE_64_BIT_INODE 1
#endif

/* Number of bits in a file offset, on hosts where this is settable. */
#undef _FILE_OFFSET_BITS

Expand Down
4 changes: 4 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -18156,6 +18156,10 @@ case $oa_lisp_flavor in
esac


# Path separator

printf "%s\n" "#define OPENAXIOM_PATH_SEPARATOR \"$PATH_SEPARATOR\"" >>confdefs.h

# What is the extension of object and executable files on this platform?


Expand Down
18 changes: 5 additions & 13 deletions src/lib/cfuns-c.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,11 @@ namespace OpenAxiom {
OPENAXIOM_C_EXPORT int
addtopath(char *dir)
{
char *path, *newpath;

path = oa_getenv("PATH");
if (path == NULL)
return -1;

newpath = (char *) malloc(1 + strlen(path) + strlen(dir) + strlen("PATH=:"));
if (newpath == NULL)
return -1;

sprintf(newpath, "PATH=%s:%s", path, dir);

return putenv(newpath);
std::string newpath = oa_getenv("PATH");
if (not newpath.empty())
newpath += OPENAXIOM_PATH_SEPARATOR;
newpath += dir;
return setenv("PATH", copy_c_str(newpath), 1);
}


Expand Down
16 changes: 10 additions & 6 deletions src/lib/sockio-c.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,18 @@ wait_for_client_write(openaxiom_sio* sock, const Byte* buf,
}
}

// Prefix a message with a given string for diagnostic purposes.
static std::string diagnostic(const char* prefix, const char* msg)
{
std::string s = prefix;
s += msg;
return s;
}

OPENAXIOM_C_EXPORT int
sread(openaxiom_sio* sock, Byte* buf, int buf_size, const char *msg)
{
int ret_val;
char err_msg[256];
errno = 0;
do {
ret_val = oa_socket_read(sock->socket, buf, buf_size);
Expand All @@ -507,8 +514,7 @@ sread(openaxiom_sio* sock, Byte* buf, int buf_size, const char *msg)
}
if (ret_val == -1) {
if (msg) {
sprintf(err_msg, "reading: %s", msg);
perror(err_msg);
perror(diagnostic("reading: ", msg).c_str());
}
return -1;
}
Expand All @@ -520,7 +526,6 @@ swrite(openaxiom_sio* sock, const Byte* buf, int buf_size,
const char* msg)
{
int ret_val;
char err_msg[256];
errno = 0;
socket_closed = 0;
ret_val = oa_socket_write(sock->socket, buf, buf_size);
Expand All @@ -533,8 +538,7 @@ swrite(openaxiom_sio* sock, const Byte* buf, int buf_size,
return wait_for_client_write(sock, buf, buf_size, msg);
} else {
if (msg) {
sprintf(err_msg, "writing: %s", msg);
perror(err_msg);
perror(diagnostic("writing: ", msg).c_str());
}
return -1;
}
Expand Down

0 comments on commit bbba2f2

Please sign in to comment.