$NetBSD: patch-ae,v 1.6 2011/10/03 07:21:15 dholland Exp $

- use stdlib.h instead of customized wrong malloc declaration
- disable varargs code
- use snprintf where obvious
- ...?

--- lib/reader.c.orig	1994-04-05 19:14:55.000000000 +0000
+++ lib/reader.c
@@ -43,10 +43,11 @@
 # define STRING_H <string.h>
 # endif
 
+#include <stdlib.h>
 # include	<stdio.h>
 # include	<ctype.h>
 # include	STRING_H
-# ifdef STDARG
+# if 1
 # include	<stdarg.h>
 # else
 # ifdef	VARARGS
@@ -78,12 +79,6 @@
 # define	maxCSStack		10
 
 
-#ifndef THINK_C
-extern char	*malloc ();
-#else
-extern void	*malloc(size_t);
-#endif
-
 static void	_RTFGetToken ();
 static void	_RTFGetToken2 ();
 static int	GetChar ();
@@ -137,7 +132,7 @@ static RTFColor	*colorList = (RTFColor *
 static RTFStyle	*styleList = (RTFStyle *) NULL;
 
 
-static FILE	*rtffp = stdin;
+static FILE	*rtffp;
 
 static char	*inputName = (char *) NULL;
 static char	*outputName = (char *) NULL;
@@ -207,6 +202,9 @@ RTFFont		*fp;
 RTFStyle	*sp;
 RTFStyleElt	*eltList, *ep;
 
+	if (rtffp == NULL)
+		rtffp = stdin;
+
 	if (rtfTextBuf == (char *) NULL)	/* initialize the text buffers */
 	{
 		rtfTextBuf = RTFAlloc (rtfBufSiz);
@@ -587,38 +585,45 @@ RTFFont	*fp;
 	if (autoCharSetFlags == 0)
 		return;
 
-	if ((autoCharSetFlags & rtfReadCharSet)
-		&& RTFCheckCM (rtfControl, rtfCharSet))
-	{
-		ReadCharSetMaps ();
-	}
-	else if ((autoCharSetFlags & rtfSwitchCharSet)
-		&& RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
+	if (autoCharSetFlags & rtfReadCharSet)
 	{
-		if ((fp = RTFGetFont (rtfParam)) != (RTFFont *) NULL)
+		if (RTFCheckCM (rtfControl, rtfCharSet))
 		{
-			if (strncmp (fp->rtfFName, "Symbol", 6) == 0)
-				curCharSet = rtfCSSymbol;
-			else
-				curCharSet = rtfCSGeneral;
-			RTFSetCharSet (curCharSet);
+			ReadCharSetMaps ();
 		}
-	}
-	else if ((autoCharSetFlags & rtfSwitchCharSet) && rtfClass == rtfGroup)
-	{
-		switch (rtfMajor)
+		else if (RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
 		{
-		case rtfBeginGroup:
-			if (csTop >= maxCSStack)
-				RTFPanic ("_RTFGetToken: stack overflow");
-			csStack[csTop++] = curCharSet;
-			break;
-		case rtfEndGroup:
-			if (csTop <= 0)
-				RTFPanic ("_RTFGetToken: stack underflow");
-			curCharSet = csStack[--csTop];
+			if ((fp = RTFGetFont (rtfParam)) != (RTFFont *) NULL)
+			{
+				if (strncmp (fp->rtfFName, "Symbol", 6) == 0)
+					curCharSet = rtfCSSymbol;
+				else
+					curCharSet = rtfCSGeneral;
+				RTFSetCharSet (curCharSet);
+			}
+		}
+		/* so \plain will revert to normal character set -Ben */
+		else if (RTFCheckCMM (rtfControl, rtfCharAttr, rtfPlain))
+		{
+			curCharSet = rtfCSGeneral;
 			RTFSetCharSet (curCharSet);
-			break;
+		}
+		else if (rtfClass == rtfGroup)
+		{
+			switch (rtfMajor)
+			{
+			case rtfBeginGroup:
+				if (csTop >= maxCSStack)
+					RTFPanic ("_RTFGetToken: stack overflow");
+				csStack[csTop++] = curCharSet;
+				break;
+			case rtfEndGroup:
+				if (csTop <= 0)
+					RTFPanic ("_RTFGetToken: stack underflow");
+				curCharSet = csStack[--csTop];
+				RTFSetCharSet (curCharSet);
+				break;
+			}
 		}
 	}
 }
@@ -1194,6 +1199,7 @@ RTFFont	*fp;
 char	buf[rtfBufSiz], *bp;
 int	old = -1;
 char	*fn = "ReadFontTbl";
+int	i;
 
 	for (;;)
 	{
@@ -1311,11 +1317,30 @@ char	*fn = "ReadFontTbl";
 				RTFPanic ("%s: missing \"}\"", fn);
 		}
 	}
-	if (fp->rtfFNum == -1)
-		RTFPanic ("%s: missing font number", fn);
+
 /*
  * Could check other pieces of structure here, too, I suppose.
  */
+
+/*
+ * I think that would be a good idea because I ran across a program that
+ * generates incorrect RTF that specifies a font family but not a font
+ * name.  This was ignored and caused rtf2xxx to coredump when it tried
+ * to strncmp() the NULL name.
+ *
+ * Better to leave no doubt about who's at fault.  -Ben
+ */
+	i = 0;
+	fp = fontList;
+	while (fp != (RTFFont *)NULL) {
+		if (fp->rtfFNum == -1)
+			RTFPanic ("%s: missing font number, entry %d in font table", fn, i);
+		if (fp->rtfFName == (char *) NULL)
+			RTFPanic ("%s: missing font name, font number %d", fn, fp->rtfFNum);
+		fp = fp->rtfNextFont;
+		i++;
+	}
+
 	RTFRouteToken ();	/* feed "}" back to router */
 }
 
@@ -1375,6 +1400,7 @@ ReadStyleSheet ()
 RTFStyle	*sp;
 RTFStyleElt	*sep, *sepLast;
 char		buf[rtfBufSiz], *bp;
+char		*bs;
 char		*fn = "ReadStyleSheet";
 
 	for (;;)
@@ -1507,8 +1533,10 @@ char		*fn = "ReadStyleSheet";
 			RTFPanic ("%s: missing style name", fn);
 		if (sp->rtfSNum < 0)
 		{
-			if (strncmp (buf, "Normal", 6) != 0
-				&& strncmp (buf, "Standard", 8) != 0)
+			/* skip leading spaces */
+			for (bs = buf; *bs == ' '; bs++);
+			if (strncmp (bs, "Normal", 6) != 0
+				&& strncmp (bs, "Standard", 8) != 0)
 				RTFPanic ("%s: missing style number", fn);
 			sp->rtfSNum = rtfNormalStyleNum;
 		}
@@ -2260,6 +2288,7 @@ static RTFKey	rtfKey[] =
 
 	rtfCharSet,	rtfMacCharSet,		"mac",		0,
 	rtfCharSet,	rtfAnsiCharSet,		"ansi",		0,
+	rtfCharSet,	rtfAnsiCpCharSet,	"ansicpg",	0,
 	rtfCharSet,	rtfPcCharSet,		"pc",		0,
 	rtfCharSet,	rtfPcaCharSet,		"pca",		0,
 
@@ -2896,20 +2925,20 @@ RTFFuncPtr	proc;
 }
 
 
-# ifdef STDARG
+# if 1
 
 /*
  * This version is for systems with stdarg
  */
 
 void
-RTFMsg (char *fmt, ...)
+RTFMsg (const char *fmt, ...)
 {
 char	buf[rtfBufSiz];
 
 	va_list args;
 	va_start (args,fmt);
-	vsprintf (buf, fmt, args);
+	vsnprintf (buf, sizeof(buf), fmt, args);
 	va_end (args);
 	(*msgProc) (buf);
 }
@@ -2989,25 +3018,25 @@ RTFFuncPtr	proc;
 }
 
 
-# ifdef STDARG
+# if 1
 
 /*
  * This version is for systems with stdarg
  */
 
 void
-RTFPanic (char *fmt, ...)
+RTFPanic (const char *fmt, ...)
 {
 char	buf[rtfBufSiz];
 
 	va_list args;
 	va_start (args,fmt);
-	vsprintf (buf, fmt, args);
+	vsnprintf (buf, sizeof(buf), fmt, args);
 	va_end (args);
-	(void) strcat (buf, "\n");
+	(void) snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "\n");
 	if (prevChar != EOF && rtfTextBuf != (char *) NULL)
 	{
-		sprintf (buf + strlen (buf),
+		snprintf(buf + strlen (buf), sizeof(buf) - strlen(buf),
 			"Last token read was \"%s\" near line %ld, position %d.\n",
 			rtfTextBuf, rtfLineNum, rtfLinePos);
 	}
