[Mit-cadr-cvs] r375 - in trunk/emulator/usim: . CADR/CADR.xcodeproj

ggilley at common-lisp.net ggilley at common-lisp.net
Thu Nov 29 18:54:53 UTC 2012


Author: ggilley
Date: Thu Nov 29 10:54:52 2012
New Revision: 375

Log:
clean up some leaks and some error handling

Modified:
   trunk/emulator/usim/CADR/CADR.xcodeproj/project.pbxproj
   trunk/emulator/usim/Files.c
   trunk/emulator/usim/chaos.c
   trunk/emulator/usim/disk.c
   trunk/emulator/usim/glob.c
   trunk/emulator/usim/glob.h
   trunk/emulator/usim/ucode.c

Modified: trunk/emulator/usim/CADR/CADR.xcodeproj/project.pbxproj
==============================================================================
--- trunk/emulator/usim/CADR/CADR.xcodeproj/project.pbxproj	Wed Nov 28 23:17:24 2012	(r374)
+++ trunk/emulator/usim/CADR/CADR.xcodeproj/project.pbxproj	Thu Nov 29 10:54:52 2012	(r375)
@@ -271,12 +271,12 @@
 				DD308D051665E71F00361F6D /* chaos.h */,
 				DD308D061665E71F00361F6D /* ether.c */,
 				DD308D071665E71F00361F6D /* Files.c */,
+				DD308D0E1665E71F00361F6D /* Files.h */,
 				DD308D081665E71F00361F6D /* iob.c */,
 				DD308D091665E71F00361F6D /* kbd_old.c */,
 				DD73006D16671C8300706ED9 /* cadr icon.icns */,
 				DD308D0C1665E71F00361F6D /* glob.c */,
 				DD308D0D1665E71F00361F6D /* glob.h */,
-				DD308D0E1665E71F00361F6D /* Files.h */,
 				DD308D101665E71F00361F6D /* ucode.c */,
 				DD308D121665E71F00361F6D /* ucode.h */,
 				DD308D111665E71F00361F6D /* disk.c */,

Modified: trunk/emulator/usim/Files.c
==============================================================================
--- trunk/emulator/usim/Files.c	Wed Nov 28 23:17:24 2012	(r374)
+++ trunk/emulator/usim/Files.c	Thu Nov 29 10:54:52 2012	(r375)
@@ -2255,7 +2255,8 @@
 	char *wild;
 	size_t len = strlen(x->x_realname);
 
-	realname = malloc(len + 1);
+    // make sure we have room for trailing /*
+	realname = malloc(len + 3);
 	strcpy(realname, x->x_realname);
     
 	// lisp keeps appending .wild
@@ -2268,13 +2269,16 @@
                     if (wild != realname && *(wild - 1) == '.')
                         *(wild - 1) = '\0';
 
-                    len = strlen(realname);
-                    if (realname[len-1] != '/')
-                        strcat(realname, "/");
-                    strcat(realname, "*");
                 }
              }
-	log(LOG_INFO, "diropen: %s\n", x->x_realname);
+
+    // glob wants a regex
+    len = strlen(realname);
+    if (realname[len-1] != '/')
+        strcat(realname, "/");
+    strcat(realname, "*");
+
+	log(LOG_INFO, "diropen: %s -> %s\n", x->x_realname, realname);
 
 	x->x_glob = glob(realname);
 	free(realname);
@@ -2373,8 +2377,16 @@
 		return;
 	}
 derror:
+    printf("diropen error: %d\n", errcode);
 	error(t, t->t_fh->f_name, errcode);
 	x->x_state = X_DERROR;
+#if defined(OSX)
+    dispatch_semaphore_signal(x->x_hangsem);
+#else
+    pthread_mutex_lock(&x->x_hangsem);
+    pthread_cond_signal(&x->x_hangcond);
+    pthread_mutex_unlock(&x->x_hangsem);
+#endif
 #ifndef SELECT
 //	(void)write(ctlpipe[1], (char *)&ax, sizeof(x));
 #endif
@@ -2538,8 +2550,9 @@
         free(x->x_tempname);
     if (x->x_dirname)
         free(x->x_dirname);
-//    if (x->x_glob)		// FIXME: a leak here, but i'm seeing a crash
-//	blkfree(x->x_glob);
+    if (x->x_glob)
+        gfree(x->x_glob);
+
 #if defined(OSX)
     dispatch_release(x->x_hangsem);
 #else

Modified: trunk/emulator/usim/chaos.c
==============================================================================
--- trunk/emulator/usim/chaos.c	Wed Nov 28 23:17:24 2012	(r374)
+++ trunk/emulator/usim/chaos.c	Thu Nov 29 10:54:52 2012	(r375)
@@ -689,30 +689,28 @@
                     if (lastpacket)
                     {
                         printf("re-transmit last packet\n");
-//                        dumpbuffer(lastpacket, CHAOS_PACKET_HEADER_SIZE + lastpacket->length);
                         chaos_packet *retransmit = lastpacket;
                         lastpacket = 0;
                         chaos_queue(retransmit);
                     }
                 }
 #else // !defined(OSX)
-		pthread_mutex_lock(&conn->twsem);
-		struct timespec ts;
+                pthread_mutex_lock(&conn->twsem);
+                struct timespec ts;
 
-		clock_gettime(CLOCK_REALTIME, &ts);
-		ts.tv_sec += 5;
-		if (pthread_cond_timedwait(&conn->twcond, &conn->twsem, &ts))
-		{
+                clock_gettime(CLOCK_REALTIME, &ts);
+                ts.tv_sec += 5;
+                if (pthread_cond_timedwait(&conn->twcond, &conn->twsem, &ts))
+                {
                     if (lastpacket)
                     {
                         printf("re-transmit last packet\n");
-//                        dumpbuffer(lastpacket, CHAOS_PACKET_HEADER_SIZE + lastpacket->length);
                         chaos_packet *retransmit = lastpacket;
                         lastpacket = 0;
                         chaos_queue(retransmit);
                     }
-		}
-		pthread_mutex_unlock(&conn->twsem);
+                }
+                pthread_mutex_unlock(&conn->twsem);
 #endif // defined(OSX)
             }
             else
@@ -1124,13 +1122,6 @@
                 void processmini(chaos_connection *conn);
                 chaos_packet *answer;
 
-//                if (packet->data[4] == ' ' && packet->data[5] >= '0' && packet->data[5] <= '9')
-//                {
-//                    extern int protocol;
-//                    protocol = packet->data[5] - '0';
-//                    printf("FILE protocol version=%d\n", protocol);
-//                }
-                
                 packet->data[packet->length] = '\0';
                 printf("MINI: '%s'\n", &packet->data[4]);
 
@@ -1166,7 +1157,7 @@
                 t = time.tv_sec;
                 t += 60UL*60*24*((1970-1900)*365L + 1970/4 - 1900/4);
                 
-                printf("timerfc: answering");
+                printf("time-rfc: answering\n");
                 
                 answer->opcode = CHAOS_OPCODE_ANS << 8;
                 answer->length = sizeof(long);
@@ -1185,7 +1176,7 @@
             {
                 chaos_packet *answer = malloc(CHAOS_PACKET_HEADER_SIZE + sizeof(long));
                 
-                printf("uptime: answering");
+                printf("uptime: answering\n");
                 
                 answer->opcode = CHAOS_OPCODE_ANS << 8;
                 answer->length = sizeof(long);
@@ -1226,6 +1217,13 @@
 #endif
                 return 0;
             }
+            
+            char buffer[512];
+            
+            strncpy(buffer, (char *)packet->data, packet->length);
+            buffer[packet->length] = '\0';
+            printf("unknown RFC: '%s'\n", packet->data);
+
             return 0;
         }
         if (((packet->opcode & 0xff00) >> 8) == CHAOS_OPCODE_SNS && conn)

Modified: trunk/emulator/usim/disk.c
==============================================================================
--- trunk/emulator/usim/disk.c	Wed Nov 28 23:17:24 2012	(r374)
+++ trunk/emulator/usim/disk.c	Thu Nov 29 10:54:52 2012	(r375)
@@ -311,7 +311,11 @@
 		(head * blocks_per_track) + block;
 
 	if (disk_fd) {
-		_disk_read(block_no, buffer);
+		if (_disk_read(block_no, buffer) < 0)
+        {
+            printf("disk_read_block: error reading block_no %d\n", block_no);
+            return -1;
+        }
 #if 0
 		if (block_no == 10312)
 		for (i = 0; i < 32; i++) {
@@ -687,6 +691,9 @@
 disk_init(char *filename)
 {
 	unsigned int label[256];
+    int ret;
+
+    label[0] = 0;
 
 #ifdef __BIG_ENDIAN__
 	disk_set_byteswap(1);
@@ -701,13 +708,14 @@
 		exit(1);
 	}
 
-	_disk_read(0, label);
+	ret = _disk_read(0, label);
 
-	if (label[0] != LABEL_LABL) {
+	if (ret < 0 || label[0] != LABEL_LABL) {
 		printf("disk: invalid pack label - disk image ignored\n");
 		printf("label %o\n", label[0]);
 		close(disk_fd);
 		disk_fd = 0;
+        return -1;
 	}
 
 	cyls = label[2];

Modified: trunk/emulator/usim/glob.c
==============================================================================
--- trunk/emulator/usim/glob.c	Wed Nov 28 23:17:24 2012	(r374)
+++ trunk/emulator/usim/glob.c	Thu Nov 29 10:54:52 2012	(r375)
@@ -27,7 +27,7 @@
 static int	gargc;				/* Number args in gargv */
 static int	gargmax;
 static short	gflag;
-static int	tglob();
+static int	tglob(register char c);
 char **glob();
 int globerr;
 char *home;
@@ -46,8 +46,8 @@
 static int match();
 static int amatch();
 static void Gcat(char *s1, char *s2);
-static void addpath();
-static void rscan(char **t, int (*f)());
+static void addpath(char);
+static void rscan(char **t, int (*f)(char));
 static char *strspl(char *cp, char *dp);
 
 int letter(register char c);
@@ -59,6 +59,7 @@
 int blklen(register char **av);
 char **blkcpy(char **oav, register char **bv);
 char **copyblk(register char **v);
+void blkfree(char **av0);
 void fatal(char *fmt, ...);
 
 static int	globcnt;
@@ -103,6 +104,11 @@
     return (gargv = copyblk(gargv));
 }
 
+void gfree(char **glob)
+{
+    free(glob);
+}
+
 static void
 ginit()
 {
@@ -118,8 +124,7 @@
 }
 
 static void
-collect(as)
-register char *as;
+collect(register char *as)
 {
 	if (eq(as, "{") || eq(as, "{}")) {
 		Gcat(as, "");
@@ -129,8 +134,7 @@
 }
 
 static void
-acollect(as)
-register char *as;
+acollect(register char *as)
 {
 	register int ogargc = gargc;
     
@@ -158,8 +162,7 @@
 }
 
 static void
-expand(as)
-char *as;
+expand(char *as)
 {
 	register char *cs;
 	register char *sgpathp, *oldcs;
@@ -215,8 +218,7 @@
 }
 
 static void
-matchdir(pattern)
-char *pattern;
+matchdir(char *pattern)
 {
 	struct stat stb;
 	register int dirf;
@@ -299,8 +301,7 @@
             }
 
 static int
-execbrc(p, s)
-char *p, *s;
+execbrc(char *p, char *s)
 {
     char restbuf[BUFSIZ + 2];
     register char *pe, *pm, *pl;
@@ -391,8 +392,7 @@
 }
 
 static int
-match(s, p)
-char *s, *p;
+match(char *s, char *p)
 {
     register int c;
     register char *sentp;
@@ -411,8 +411,7 @@
 }
 
 static int
-amatch(s, p)
-register char *s, *p;
+amatch(register char *s, register char *p)
 {
     register int scc;
     int ok, lc;
@@ -502,8 +501,7 @@
 }
 
 static int
-Gmatch(s, p)
-register char *s, *p;
+Gmatch(register char *s, register char *p)
 {
     register int scc;
     int ok, lc;
@@ -562,8 +560,7 @@
 }
 
 static void
-Gcat(s1, s2)
-register char *s1, *s2;
+Gcat(register char *s1, register char *s2)
 {
     if (gargc == gargmax) {
         char **newvec;
@@ -584,8 +581,7 @@
 }
 
 static void
-addpath(c)
-char c;
+addpath(char c)
 {
     
     if (gpathp >= lastgpathp) {
@@ -598,9 +594,7 @@
 }
 
 static void
-rscan(t, f)
-register char **t;
-int (*f)();
+rscan(register char **t, int (*f)(char))
 {
     register char *p, c;
     
@@ -629,8 +623,7 @@
  }
  */
 static int
-tglob(c)
-register char c;
+tglob(register char c)
 {
     
     if (any(c, globchars))

Modified: trunk/emulator/usim/glob.h
==============================================================================
--- trunk/emulator/usim/glob.h	Wed Nov 28 23:17:24 2012	(r374)
+++ trunk/emulator/usim/glob.h	Thu Nov 29 10:54:52 2012	(r375)
@@ -5,7 +5,7 @@
  */
 
 char **glob(register char *v);
-void blkfree(char **av0);
+void gfree(char **glob);
 
 
 

Modified: trunk/emulator/usim/ucode.c
==============================================================================
--- trunk/emulator/usim/ucode.c	Wed Nov 28 23:17:24 2012	(r374)
+++ trunk/emulator/usim/ucode.c	Thu Nov 29 10:54:52 2012	(r375)
@@ -1670,7 +1670,7 @@
 	  return -1;
 
 	ret = read(fd, version, 2);
-	if (version[0] != 0 || version[1] != 1) {
+	if (ret < 0 || version[0] != 0 || version[1] != 1) {
 	  close(fd);
 	  return -1;
 	}
@@ -1678,6 +1678,11 @@
 	for (i = 0; i < PAGES_TO_SAVE; i++) {
 	  add_new_page_no(i);
 	  ret = read(fd, (char *)phy_pages[i], sizeof(struct page_s));
+      if (ret < 0)
+      {
+          close(fd);
+          return -1;
+      }
 #ifdef __BIG_ENDIAN__
 	  _swaplongbytes((unsigned int *)phy_pages[i], 256);
 #endif
@@ -1704,12 +1709,22 @@
 	version[0] = 0;
 	version[1] = 1;
 	ret = write(fd, version, 2);
+    if (ret < 0)
+    {
+        close(fd);
+        return -1;
+    }
 
 	for (i = 0; i < PAGES_TO_SAVE; i++) {
 #ifdef __BIG_ENDIAN__
 	  _swaplongbytes((unsigned int *)phy_pages[i], 256);
 #endif
 	  ret = write(fd, (char *)phy_pages[i], sizeof(struct page_s));
+      if (ret < 0)
+      {
+          close(fd);
+          return -1;
+      }
 	}
 
 	close(fd);




More information about the mit-cadr-cvs mailing list