diff --git a/C/alloc.c b/C/alloc.c index b172e01c7..2fa818ab6 100644 --- a/C/alloc.c +++ b/C/alloc.c @@ -39,6 +39,12 @@ static char SccsId[] = "%W% %G%"; #if HAVE_UNISTD_H #include #endif +#if HAVE_FCNTL_H +#include +#endif +#if HAVE_SYS_STAT_H +#include +#endif #include #include @@ -807,7 +813,7 @@ InitWorkSpace(Int s) return(NULL); } #else - fd = open("/dev/zero", O_RDWR); + fd = open("/dev/zero", O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd < 0) { #if HAVE_MKSTEMP char file[256]; @@ -829,7 +835,7 @@ InitWorkSpace(Int s) itos(getpid(), &file[12]); #endif /* HAVE_TMPNAM */ #endif /* HAVE_MKSTEMP */ - fd = open(file, O_CREAT|O_RDWR); + fd = open(file, O_CREAT|O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd < 0) { Yap_Error(FATAL_ERROR, TermNil, "mmap could not open %s", file); return NULL; @@ -942,7 +948,7 @@ ExtendWorkSpace(Int s, int fixed_allocation) itos(getpid(), &file[12]); #endif /* HAVE_TMPNAM */ #endif /* HAVE_MKSTEMP */ - fd = open(file, O_CREAT|O_RDWR); + fd = open(file, O_CREAT|O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd < 0) { Yap_ErrorMessage = Yap_ErrorSay; snprintf4(Yap_ErrorMessage, MAX_ERROR_MSG_SIZE, diff --git a/C/corout.c b/C/corout.c index 67f36c398..6f02ee060 100644 --- a/C/corout.c +++ b/C/corout.c @@ -531,7 +531,7 @@ static Int p_can_unify(void) static Int p_non_ground(void) { #ifdef COROUTINING - Term r; + Term r = TermNil; if (!non_ground(ARG1, &r)) return(FALSE); return (Yap_unify(ARG2, r)); diff --git a/C/globals.c b/C/globals.c index dc7edd164..0092c87b9 100644 --- a/C/globals.c +++ b/C/globals.c @@ -293,7 +293,7 @@ GrowArena(Term arena, CELL *pt, UInt old_size, UInt size, UInt arity) if (H+size > ASP-1024) { XREGS[arity+1] = arena; - if (!Yap_gcl(size*sizeof(CELL), arity+1, ENV, P)) { + if (!Yap_gcl(size*sizeof(CELL), arity+1, ENV, gc_P(P,CP))) { Yap_Error(OUT_OF_STACK_ERROR, TermNil, Yap_ErrorMessage); return FALSE; } @@ -308,7 +308,7 @@ GrowArena(Term arena, CELL *pt, UInt old_size, UInt size, UInt arity) XREGS[arity+1] = arena; /* try to recover some room */ if (arena == GlobalArena && 10*(pt-H0) > 8*(H-H0)) { - if (!Yap_gcl(size*sizeof(CELL), arity+1, ENV, P)) { + if (!Yap_gcl(size*sizeof(CELL), arity+1, ENV, gc_P(P,CP))) { Yap_Error(OUT_OF_STACK_ERROR,TermNil,Yap_ErrorMessage); return FALSE; } diff --git a/C/iopreds.c b/C/iopreds.c index b5be01515..cfd17406f 100644 --- a/C/iopreds.c +++ b/C/iopreds.c @@ -817,7 +817,19 @@ SocketPutc (int sno, int ch) #if _MSC_VER || defined(__MINGW32__) send(s->u.socket.fd, &c, sizeof(c), 0); #else - write(s->u.socket.fd, &c, sizeof(c)); + { + int out = 0; + while (!out) { + out = write(s->u.socket.fd, &c, sizeof(c)); + if (out <0) { +#if HAVE_STRERROR + Yap_Error(PERMISSION_ERROR_INPUT_STREAM, TermNil, "error writing stream socket: %s", strerror(errno)); +#else + Yap_Error(PERMISSION_ERROR_INPUT_STREAM, TermNil, "error writing stream socket"); +#endif + } + } + } #endif console_count_output_char(ch,s); return ((int) ch); @@ -846,7 +858,19 @@ ConsolePipePutc (int sno, int ch) } } #else - write(s->u.pipe.fd, &c, sizeof(c)); + { + int out = 0; + while (!out) { + out = write(s->u.pipe.fd, &c, sizeof(c)); + if (out <0) { +#if HAVE_STRERROR + Yap_Error(PERMISSION_ERROR_INPUT_STREAM, TermNil, "error writing stream pipe: %s", strerror(errno)); +#else + Yap_Error(PERMISSION_ERROR_INPUT_STREAM, TermNil, "error writing stream pipe"); +#endif + } + } + } #endif count_output_char(ch,s); return ((int) ch); @@ -872,7 +896,19 @@ PipePutc (int sno, int ch) } } #else - write(s->u.pipe.fd, &c, sizeof(c)); + { + int out = 0; + while (!out) { + out = write(s->u.pipe.fd, &c, sizeof(c)); + if (out <0) { +#if HAVE_STRERROR + Yap_Error(PERMISSION_ERROR_INPUT_STREAM, TermNil, "error writing stream pipe: %s", strerror(errno)); +#else + Yap_Error(PERMISSION_ERROR_INPUT_STREAM, TermNil, "error writing stream pipe"); +#endif + } + } + } #endif console_count_output_char(ch,s); return ((int) ch); @@ -3599,7 +3635,7 @@ syntax_error (TokEntry * tokptr, int sno) Term info; int count = 0, out = 0; Int start, err = 0, end; - Term tf[6]; + Term tf[7]; Term *error = tf+3; CELL *Hi = H; diff --git a/C/save.c b/C/save.c index 29c039c5e..c7fbaf7ed 100644 --- a/C/save.c +++ b/C/save.c @@ -1434,7 +1434,7 @@ cat_file_name(char *s, char *prefix, char *name, unsigned int max_length) #else strncat(s,"/", max_length); #endif - strncat(s, name, max_length); + strncat(s, name, max_length-1); } static int @@ -1459,8 +1459,8 @@ OpenRestore(char *inpf, char *YapLibDir, CELL *Astate, CELL *ATrail, CELL *AStac if (getwd (Yap_FileNameBuf) == NULL) Yap_FileNameBuf[0] = '\0'; #endif - strncat(Yap_FileNameBuf, "/", YAP_FILENAME_MAX); - strncat(Yap_FileNameBuf, inpf, YAP_FILENAME_MAX); + strncat(Yap_FileNameBuf, "/", YAP_FILENAME_MAX-1); + strncat(Yap_FileNameBuf, inpf, YAP_FILENAME_MAX-1); if (inpf != NULL && (splfild = open_file(inpf, O_RDONLY)) > 0) { if ((mode = commit_to_saved_state(inpf,Astate,ATrail,AStack,AHeap)) != FAIL_RESTORE) { Yap_ErrorMessage = NULL; @@ -1561,8 +1561,8 @@ OpenRestore(char *inpf, char *YapLibDir, CELL *Astate, CELL *ATrail, CELL *AStac if (getwd (Yap_FileNameBuf) == NULL) Yap_FileNameBuf[0] = '\0'; #endif - strncat(Yap_FileNameBuf, "/", YAP_FILENAME_MAX); - strncat(Yap_FileNameBuf, inpf, YAP_FILENAME_MAX); + strncat(Yap_FileNameBuf, "/", YAP_FILENAME_MAX-1); + strncat(Yap_FileNameBuf, inpf, YAP_FILENAME_MAX-1); do_system_error(PERMISSION_ERROR_OPEN_SOURCE_SINK,"could not open saved state"); } return FAIL_RESTORE; diff --git a/C/stdpreds.c b/C/stdpreds.c index 958982947..e3aee8fbf 100644 --- a/C/stdpreds.c +++ b/C/stdpreds.c @@ -1881,7 +1881,7 @@ p_atom_split(void) static Term gen_syntax_error(Atom InpAtom, char *s) { - Term ts[6], ti[2]; + Term ts[7], ti[2]; ti[0] = ARG1; ti[1] = ARG2; ts[0] = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom(s),2),2,ti); diff --git a/C/sysbits.c b/C/sysbits.c index 3be43a0f4..2def99659 100644 --- a/C/sysbits.c +++ b/C/sysbits.c @@ -1715,17 +1715,17 @@ TrueFileName (char *source, char *root, char *result, int in_lib) #endif if (root) { if (!dir_separator(root[0]) && !volume_header(root)) { - strncat(ares1, root, YAP_FILENAME_MAX); + strncat(ares1, root, YAP_FILENAME_MAX-1); } else { - strncpy(ares1, root, YAP_FILENAME_MAX); + strncpy(ares1, root, YAP_FILENAME_MAX-1); } #if _MSC_VER || defined(__MINGW32__) - strncat (ares1, "\\", YAP_FILENAME_MAX); + strncat (ares1, "\\", YAP_FILENAME_MAX-1); #else - strncat (ares1, "/", YAP_FILENAME_MAX); + strncat (ares1, "/", YAP_FILENAME_MAX-1); #endif } - strncat (ares1, result, YAP_FILENAME_MAX); + strncat (ares1, result, YAP_FILENAME_MAX-1); if (in_lib) { int tmpf; if ((tmpf = open(ares1, O_RDONLY)) < 0) { @@ -1750,11 +1750,11 @@ TrueFileName (char *source, char *root, char *result, int in_lib) } #endif #if _MSC_VER || defined(__MINGW32__) - strncat(ares1,"\\", YAP_FILENAME_MAX); + strncat(ares1,"\\", YAP_FILENAME_MAX-1); #else - strncat(ares1,"/", YAP_FILENAME_MAX); + strncat(ares1,"/", YAP_FILENAME_MAX-1); #endif - strncat(ares1,result, YAP_FILENAME_MAX); + strncat(ares1,result, YAP_FILENAME_MAX-1); if ((tmpf = open(ares1, O_RDONLY)) >= 0) { close(tmpf); strncpy (result, ares1, YAP_FILENAME_MAX); diff --git a/C/ypsocks.c b/C/ypsocks.c index 0089cc003..210ce8330 100644 --- a/C/ypsocks.c +++ b/C/ypsocks.c @@ -202,7 +202,7 @@ Yap_init_socks(char *host, long interface_port) return; } - (void) memset((char *) &adr, '\0', sizeof(struct sockaddr_in)); + (void) memset((void *) &soadr, '\0', sizeof(struct sockaddr_in)); soadr.sin_family = AF_INET; soadr.sin_port = htons((short) interface_port); @@ -887,13 +887,11 @@ p_socket_accept(void) domain = Yap_GetSocketDomain(sno); #if HAVE_SYS_UN_H if (domain == af_unix) { - char tmp[sizeof(struct sockaddr_un)+107]; /* hit me with a broomstick */ - struct sockaddr_in caddr; + struct sockaddr_un caddr; unsigned int len; - len = sizeof(struct sockaddr_un)+107; - memset((void *)&caddr,(int) 0, len); - if ((fd=accept(ofd, (struct sockaddr *)tmp, &len)) < 0) { + memset((void *)&caddr,(int) 0, sizeof(caddr)); + if ((fd=accept(ofd, (struct sockaddr *)&caddr, &len)) < 0) { #if HAVE_STRERROR Yap_Error(SYSTEM_ERROR, TermNil, "socket_accept/3 (accept: %s)", strerror(socket_errno)); diff --git a/console/yap.c b/console/yap.c index 6a268675e..b0257d4e2 100644 --- a/console/yap.c +++ b/console/yap.c @@ -205,7 +205,7 @@ do_bootfile (char *bootfilename) { char *ErrorMessage = YAP_CompileClause(t); if (ErrorMessage) - fprintf(stderr, ErrorMessage); + fprintf(stderr, "%s", ErrorMessage); } /* do backtrack */ YAP_Reset(); @@ -708,7 +708,7 @@ init_standard_system(int argc, char *argv[], YAP_init_args *iap) strcpy(boot_file, PL_SRC_DIR); #endif #if HAVE_STRNCAT - strncat(boot_file, BootFile, 256); + strncat(boot_file, BootFile, 255); #else strcat(boot_file, BootFile); #endif @@ -762,7 +762,7 @@ exec_top_level(int BootMode, YAP_init_args *iap) strcpy(init_file, PL_SRC_DIR); #endif #if HAVE_STRNCAT - strncat(init_file, InitFile, 256); + strncat(init_file, InitFile, 255); #else strcat(init_file, InitFile); #endif