Util-Linux package development
 help / color / mirror / Atom feed
* prevent cal segfault
@ 2015-03-12  8:46 Petr Gajdos
  2015-03-12  9:10 ` Ruediger Meier
  2015-03-12 11:05 ` Karel Zak
  0 siblings, 2 replies; 4+ messages in thread
From: Petr Gajdos @ 2015-03-12  8:46 UTC (permalink / raw)
  To: util-linux


[-- Attachment #1.1: Type: text/plain, Size: 273 bytes --]

Hello,

cal utility segfaults in putp() when setupterm() fails as cal
insufficiently checks for setupterm() failure.

$ TERM= cal
Segmentation fault

Please find details in 

https://bugzilla.suse.com/show_bug.cgi?id=903440

especially in comment 4.

Petr


[-- Attachment #1.2: util-linux-cal-no-terminal.patch --]
[-- Type: text/x-patch, Size: 1979 bytes --]

Index: misc-utils/cal.c
===================================================================
--- misc-utils/cal.c.orig	2014-08-26 16:48:45.132654654 +0200
+++ misc-utils/cal.c	2015-03-11 14:16:06.075064939 +0100
@@ -81,14 +81,22 @@
 # endif
 # include <term.h>
 
-static void my_setupterm(const char *term, int fildes, int *errret)
+#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
+static const char	*term="";
+#endif
+
+static int my_setupterm(const char *term, int fildes)
 {
-	setupterm((char *)term, fildes, errret);
+        int errret; /* unused */
+	return setupterm((char *)term, fildes, &errret) == OK;
 }
 
 static void my_putstring(char *s)
 {
-	putp(s);
+	if (term)
+		putp(s);
+	else
+		fputs(s, stdout);
 }
 
 static const char *my_tgetstr(char *s __attribute__((__unused__)), char *ss)
@@ -107,9 +115,9 @@
 static char tcbuffer[4096];
 static char *strbuf = termbuffer;
 
-static void my_setupterm(const char *term, int fildes __attribute__((__unused__)), int *errret)
+static int my_setupterm(const char *term, int fildes __attribute__((__unused__)))
 {
-	*errret = tgetent(tcbuffer, term);
+	return tgetent(tcbuffer, term) == 1;
 }
 
 static void my_putstring(char *s)
@@ -135,10 +143,6 @@
 
 #endif	/* end of LIBTERMCAP / NCURSES */
 
-#if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
-static const char	*term="";
-#endif
-
 static const char	*Senter="", *Sexit="";/* enter and exit standout mode */
 
 #include "widechar.h"
@@ -293,12 +297,14 @@
 
 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
 	if ((term = getenv("TERM"))) {
-		int ret;
-		my_setupterm(term, STDOUT_FILENO, &ret);
-		if (ret > 0) {
+		if (my_setupterm(term, STDOUT_FILENO)) {
 			Senter = my_tgetstr("so","smso");
 			Sexit = my_tgetstr("se","rmso");
 		}
+		else
+		{
+			term = NULL;
+		}
 	}
 #endif
 

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-03-12 12:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-12  8:46 prevent cal segfault Petr Gajdos
2015-03-12  9:10 ` Ruediger Meier
2015-03-12 11:05 ` Karel Zak
2015-03-12 12:00   ` Petr Gajdos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox