$OpenBSD: patch-p2clib_c,v 1.1 2017/09/09 22:56:49 nigel Exp $
Index: p2clib.c
--- p2clib.c.orig
+++ p2clib.c
@@ -30,8 +30,6 @@ int P_ioresult;
 
 long EXCP_LINE;    /* Used by Pascal workstation system */
 
-Anyptr __MallocTemp__;
-
 __p2c_jmp_buf *__top_jb;
 
 
@@ -417,9 +415,10 @@ register int pos, len;
 
 /* Insert string "src" at index "pos" of "dst". */
 
-void strinsert(src, dst, pos)
+void strinsert(src, dst, pos, size)
 register char *src, *dst;
 register int pos;
+size_t size;
 {
     register int slen, dlen;
 
@@ -429,7 +428,7 @@ register int pos;
     dst += dlen;
     dlen -= pos;
     if (dlen <= 0) {
-        strcpy(dst, src);
+        strlcpy(dst, src, size);
         return;
     }
     slen = strlen(src);
@@ -890,7 +889,7 @@ int *Day, *Month, *Year, *Hour, *Min, *Sec;
 {
 #ifndef NO_TIME
     struct tm *tm;
-    long clock;
+    time_t clock;
 
     time(&clock);
     tm = localtime(&clock);
@@ -908,7 +907,7 @@ int *Day, *Month, *Year, *Hour, *Min, *Sec;
 Void VAXdate(s)
 char *s;
 {
-    long clock;
+    time_t clock;
     char *c;
     int i;
     static int where[] = {8, 9, 0, 4, 5, 6, 0, 20, 21, 22, 23};
@@ -924,7 +923,7 @@ char *s;
 Void VAXtime(s)
 char *s;
 {
-    long clock;
+    time_t clock;
     char *c;
     int i;
 
@@ -959,7 +958,14 @@ register int len, n;
 }
 
 
+void *Malloc(size_t size) {
+    void *p;
 
+    if ( (p = malloc(size)) )
+        return p;
+    _OutMem();
+    return p;
+}
 
 /* if defined(__GNUC__) || defined(HAVE_INTTYPES_H) */
 #ifndef NO_INTTYPES_H
@@ -985,85 +991,87 @@ int _NilCheck()
 /* The following is suitable for the HP Pascal operating system.
    It might want to be revised when emulating another system. */
 
-char *_ShowEscape(buf, code, ior, prefix)
+char *_ShowEscape(buf, code, ior, prefix, size)
 char *buf, *prefix;
-int code, ior;
+int code, ior, size;
 {
     char *bufp;
+    int sizep;
 
     if (prefix && *prefix) {
-        strcpy(buf, prefix);
-	strcat(buf, ": ");
+        strlcpy(buf, prefix, size);
+	strlcat(buf, ": ", size);
         bufp = buf + strlen(buf);
     } else {
         bufp = buf;
     }
+    sizep = size - strlen(buf);
     if (code == -10) {
-        sprintf(bufp, "Pascal system I/O error %d", ior);
+        snprintf(bufp, sizep, "Pascal system I/O error %d", ior);
         switch (ior) {
             case 3:
-                strcat(buf, " (illegal I/O request)");
+                strlcat(buf, " (illegal I/O request)", size);
                 break;
             case 7:
-                strcat(buf, " (bad file name)");
+                strlcat(buf, " (bad file name)", size);
                 break;
             case FileNotFound:   /*10*/
-                strcat(buf, " (file not found)");
+                strlcat(buf, " (file not found)", size);
                 break;
             case FileNotOpen:    /*13*/
-                strcat(buf, " (file not open)");
+                strlcat(buf, " (file not open)", size);
                 break;
             case BadInputFormat: /*14*/
-                strcat(buf, " (bad input format)");
+                strlcat(buf, " (bad input format)", size);
                 break;
             case 24:
-                strcat(buf, " (not open for reading)");
+                strlcat(buf, " (not open for reading)", size);
                 break;
             case 25:
-                strcat(buf, " (not open for writing)");
+                strlcat(buf, " (not open for writing)", size);
                 break;
             case 26:
-                strcat(buf, " (not open for direct access)");
+                strlcat(buf, " (not open for direct access)", size);
                 break;
             case 28:
-                strcat(buf, " (string subscript out of range)");
+                strlcat(buf, " (string subscript out of range)", size);
                 break;
             case EndOfFile:      /*30*/
-                strcat(buf, " (end-of-file)");
+                strlcat(buf, " (end-of-file)", size);
                 break;
             case FileWriteError: /*38*/
-		strcat(buf, " (file write error)");
+		strlcat(buf, " (file write error)", size);
 		break;
         }
     } else {
-        sprintf(bufp, "Pascal system error %d", code);
+        snprintf(bufp, sizep, "Pascal system error %d", code);
         switch (code) {
             case -2:
-                strcat(buf, " (out of memory)");
+                strlcat(buf, " (out of memory)", size);
                 break;
             case -3:
-                strcat(buf, " (reference to NIL pointer)");
+                strlcat(buf, " (reference to NIL pointer)", size);
                 break;
             case -4:
-                strcat(buf, " (integer overflow)");
+                strlcat(buf, " (integer overflow)", size);
                 break;
             case -5:
-                strcat(buf, " (divide by zero)");
+                strlcat(buf, " (divide by zero)", size);
                 break;
             case -6:
-                strcat(buf, " (real math overflow)");
+                strlcat(buf, " (real math overflow)", size);
                 break;
             case -8:
-                strcat(buf, " (value range error)");
+                strlcat(buf, " (value range error)", size);
                 break;
             case -9:
-                strcat(buf, " (CASE value range error)");
+                strlcat(buf, " (CASE value range error)", size);
                 break;
             case -12:
-                strcat(buf, " (bus error)");
+                strlcat(buf, " (bus error)", size);
                 break;
             case -20:
-                strcat(buf, " (stopped by user)");
+                strlcat(buf, " (stopped by user)", size);
                 break;
         }
     }
@@ -1086,7 +1094,7 @@ int code;
         exit(EXIT_SUCCESS);
     if (code == -1)
         exit(EXIT_FAILURE);
-    fprintf(stderr, "%s\n", _ShowEscape(buf, P_escapecode, P_ioresult, ""));
+    fprintf(stderr, "%s\n", _ShowEscape(buf, P_escapecode, P_ioresult, "", sizeof(buf)));
     exit(EXIT_FAILURE);
 }
 
@@ -1105,7 +1113,7 @@ char *name;
     if (!__top_jb && name && *name) {
 	char buf[100];
 	fprintf(stderr, "%s: %s\n",
-		name, _ShowEscape(buf, P_escapecode, P_ioresult, ""));
+		name, _ShowEscape(buf, P_escapecode, P_ioresult, "", sizeof(buf)));
 	exit(EXIT_FAILURE);
     }
     return _Escape(-10);
