[mw-devel] [Git][arthur/mw][master] Fix up new -Wformat-truncate and -Wrestrict errors

Andrew Price welshbyte at sucs.org
Sun Oct 7 22:19:21 BST 2018


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
622cbc53 by Andrew Price at 2018-10-07T22:16:47+01:00
Fix up new -Wformat-truncate and -Wrestrict errors

New gcc features finding new issues break the build.

- - - - -


7 changed files:

- src/client/edit.c
- src/client/main.c
- src/client/mesg.c
- src/client/newmain.c
- src/client/read.c
- src/webclient/comms.c
- src/webclient/import.c


Changes:

=====================================
src/client/edit.c
=====================================
--- a/src/client/edit.c
+++ b/src/client/edit.c
@@ -622,7 +622,6 @@ static void users_lastread(int folnum)
 
 void edit_folder(const char *args, const char *name)
 {
-	char fullpath[256];
 	int folnum;
 	int afile;
 	struct folder fold;
@@ -636,6 +635,9 @@ void edit_folder(const char *args, const char *name)
 	if (!get_folder_number(&fold,folnum)) return;
 	if (stringcmp(args,"status",2))
 	{
+		char log[256];
+		int n = 0;
+
 		show_fold_stats(fold.status,tmp,true);
 		printf(_("Folder %s\nCurrent status:-\n"),fold.name);
 		printf(_("User not in group [%s]\n"),tmp);
@@ -647,7 +649,7 @@ void edit_folder(const char *args, const char *name)
 		if (*tmp)
 		{
 			fold.status=folder_stats(tmp,fold.status);
-			snprintf(fullpath, 255, "FOLDER(STATUS) of %s by %s", fold.name, tmp);
+			n = snprintf(log, 255, "FOLDER(STATUS) of %s by %s", fold.name, tmp);
 			show_fold_stats(fold.status,tmp,true);
 			printf(_("Status changed to [%s]\n"),tmp);
 			if(!f_active(fold.status))
@@ -658,13 +660,14 @@ void edit_folder(const char *args, const char *name)
 		if (*tmp)
 		{
 			fold.g_status=folder_stats(tmp,fold.g_status);
-			snprintf(fullpath, 255, "%s %s", fullpath, tmp);
+			snprintf(log + n, 255 - n, " %s", tmp);
 			show_fold_stats(fold.g_status,tmp,true);
 			printf(_("Status changed to [%s]\n"),tmp);
 			if(!f_active(fold.g_status))
 				printf(_("WARNING: folder may get written over by the next folder created.\n"));
 		}
-		mwlog("%s", fullpath);
+		log[255] = '\0';
+		mwlog("%s", log);
 	}else
 	if (stringcmp(args,"groups",2))
 	{


=====================================
src/client/main.c
=====================================
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -1850,22 +1850,22 @@ void devel_msg(const char *func, const char *fmt, ...)
 
 void broadcast_onoffcode(int ocode, int method, const char *sourceuser, const char *reason)
 {
-	char		logofftext[MAXTEXTLENGTH];
-	extern int	talker_logontype;
+	char logofftext[MAXTEXTLENGTH];
+	extern int talker_logontype;
+	const char *usr;
 
-	/* create the broadcast string */
-	snprintf(logofftext, MAXTEXTLENGTH-1, "%d,%d,%d,%s", ocode, method,
-	         talker_logontype & LOGONMASK_QUIET,
-	         (sourceuser)?(sourceuser):(user->record.name));
+	if (sourceuser == NULL)
+		usr = user->record.name;
+	else
+		usr = sourceuser;
 
-	/* add an optional reason */
-	if (reason != NULL)
-	{
-		char	reasontext[MAXTEXTLENGTH];
-		snprintf(reasontext, MAXTEXTLENGTH - 1, "%s,%s", logofftext, reason);
-		snprintf(logofftext, MAXTEXTLENGTH - 1, "%s", reasontext);
-	}
+	if (reason == NULL)
+		snprintf(logofftext, MAXTEXTLENGTH-1, "%d,%d,%d,%s", ocode, method,
+		         talker_logontype & LOGONMASK_QUIET, usr);
+	else
+		snprintf(logofftext, MAXTEXTLENGTH-1, "%d,%d,%d,%s,%s", ocode, method,
+		         talker_logontype & LOGONMASK_QUIET, usr, reason);
 
-	/* send the message */
+	logofftext[MAXTEXTLENGTH-1] = '\0';
 	ipc_send_to_all(IPC_CHECKONOFF, logofftext);
 }


=====================================
src/client/mesg.c
=====================================
--- a/src/client/mesg.c
+++ b/src/client/mesg.c
@@ -5,6 +5,8 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdarg.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include <util.h>
 #include "talker_privs.h"
@@ -50,12 +52,12 @@ void postinfo(struct user *who, struct folder *fol, struct Header *mesg)
 void broadcast(int state, const char *fmt, ...)
 {
 	char buff[MAXTEXTLENGTH];
-	char text[MAXTEXTLENGTH];
+	char text[MAXTEXTLENGTH - 14];
 	va_list va;
 	int israw;
 
 	va_start(va, fmt);
-	vsnprintf(text, MAXTEXTLENGTH-1, fmt, va);
+	vsnprintf(text, MAXTEXTLENGTH - 15, fmt, va);
 	va_end(va);
 
 	israw = (state & 0x100) != 0;
@@ -103,32 +105,28 @@ void broadcast(int state, const char *fmt, ...)
 
 void mwlog(const char *fmt, ...)
 {
+	char detail[LOGLINESIZE];
+	mode_t mask;
 	va_list ap;
-	int file;
-	char outmsg[LOGLINESIZE];
-	char addstr[LOGLINESIZE];
-	char new[LOGLINESIZE];
 	time_t t;
-
-	va_start(ap, fmt);
-
-	if ((file=open(LOGFILE,O_WRONLY|O_CREAT|O_APPEND,0600))<0)
-	{
-		perror("log");
+	char *ts;
+	FILE *fp;
+
+	mask = umask(077);
+	fp = fopen(LOGFILE, "a");
+	umask(mask);
+	if (fp == NULL) {
+		perror(LOGFILE);
 		return;
 	}
 	t=time(0);
-
-	vsnprintf(addstr, LOGLINESIZE-1, fmt, ap);
-	snprintf(outmsg, LOGLINESIZE-1, "%s", asctime(gmtime(&t)));
-	outmsg[strlen(outmsg)-1] = ' ';
-
-	snprintf(new, LOGLINESIZE-2, "%s| %*s | %s", outmsg, NAMESIZE, user->record.name, addstr);
-	strcat(new, "\n");
-
-	write(file,new,strlen(new));
-	close(file);
-
+	ts = asctime(gmtime(&t));
+	if (ts != NULL && *ts != '\0')
+		ts[strlen(ts) - 1] = '\0';
+	va_start(ap, fmt);
+	vsnprintf(detail, LOGLINESIZE-1, fmt, ap);
 	va_end(ap);
+	fprintf(fp, "%s | %*s | %s\n", ts, NAMESIZE, user->record.name, detail);
+	fclose(fp);
 }
 


=====================================
src/client/newmain.c
=====================================
--- a/src/client/newmain.c
+++ b/src/client/newmain.c
@@ -1030,22 +1030,20 @@ void c_quit(CommandList *cm, int argc, const char **argv, char *args)
 
 void c_save(CommandList *cm, int argc, const char **argv, char *args)
 {
+#define SAVEDIR "/tmp"
+#define SAVEFILESIZE (128 + sizeof(SAVEDIR))
 	int msg=atoi(argv[1]);
-	char buff[128],file[128];
+	char file[SAVEFILESIZE];
+	char *c;
 	FILE *f;
+	int x;
 
-	int i,len,x;
+	snprintf(file, SAVEFILESIZE - 1, SAVEDIR "/%s", argv[2]);
+	file[SAVEFILESIZE - 1] = '\0';
 
-	len=strlen(argv[2]);
-	for(i=0;i<len;i++)
-	{
-		if (argv[2][i]=='/') file[i]='_';
-		else
-			file[i]=argv[2][i];
-	}
-	file[len]=0;
-
-	sprintf(buff,"/tmp/%s",file);
+	for(c = file + sizeof(SAVEDIR); *c != '\0'; c++)
+		if (*c == '/')
+			*c = '_';
 
 	x=fork();
 	if (x==-1)
@@ -1056,7 +1054,7 @@ void c_save(CommandList *cm, int argc, const char **argv, char *args)
 	{
 		/* we are child */
 		if (perms_drop()==-1) {perror("setuid");exit(0);}
-		if ((f=fopen(buff,"a"))==NULL)
+		if ((f = fopen(file, "a")) == NULL)
 		{
 			perror(file);
 			exit(0);
@@ -1066,7 +1064,7 @@ void c_save(CommandList *cm, int argc, const char **argv, char *args)
 		perms_restore();
 		read_msg(currentfolder, msg, user);
 		fclose(output);
-		printf("Mesg no %d saved to %s\n",msg,buff);
+		printf("Message %d saved to %s\n", msg, file);
 		output=stdout;
 		exit(0);
 	}else


=====================================
src/client/read.c
=====================================
--- a/src/client/read.c
+++ b/src/client/read.c
@@ -44,7 +44,7 @@ void display_article(struct Header *tmp, int datafile)
 	char title[80];
 	time_t when;
 
-	buff=(char *)malloc(tmp->size);
+	buff = calloc(1, tmp->size + 1);
 
 	if (!remote)
 	{
@@ -115,8 +115,10 @@ int read_msg(int folnum, int msgnum, struct user *user)
 
 	close(file);
 
-	if (!(data.status&1))
-		{printf("That folder does not exist.\n");return(false);}
+	if (!(f_active(data.status))) {
+		printf("Folder %d does not exist.\n", folnum);
+		return false;
+	}
 	if (msgnum>data.last || msgnum<data.first)
 		{printf("There is no message %d.\n",msgnum);return(false);}
 	if (data.last<data.first || data.last==0)


=====================================
src/webclient/comms.c
=====================================
--- a/src/webclient/comms.c
+++ b/src/webclient/comms.c
@@ -619,33 +619,29 @@ void create_user(struct user *me, const char *username, const char *password)
 
 void mwlog(const char *fmt, ...)
 {
+	char detail[LOGLINESIZE];
+	mode_t mask;
 	va_list ap;
-	int file;
-	char outmsg[LOGLINESIZE];
-	char addstr[LOGLINESIZE];
-	char new[LOGLINESIZE];
 	time_t t;
-
-	va_start(ap, fmt);
-
-	if ((file=open(LOGFILE,O_WRONLY|O_CREAT|O_APPEND,0600))<0)
-	{
-		perror("log");
+	char *ts;
+	FILE *fp;
+
+	mask = umask(077);
+	fp = fopen(LOGFILE, "a");
+	umask(mask);
+	if (fp == NULL) {
+		perror(LOGFILE);
 		return;
 	}
 	t=time(0);
-
-	vsnprintf(addstr, LOGLINESIZE-1, fmt, ap);
-	snprintf(outmsg, LOGLINESIZE-1, "%s", asctime(gmtime(&t)));
-	outmsg[strlen(outmsg)-1] = ' ';
-
-	snprintf(new, LOGLINESIZE-2, "%s| %*s | %s", outmsg, NAMESIZE, user->record.name, addstr);
-	strcat(new, "\n");
-
-	write(file,new,strlen(new));
-	close(file);
-
+	ts = asctime(gmtime(&t));
+	if (ts != NULL && *ts != '\0')
+		ts[strlen(ts) - 1] = '\0';
+	va_start(ap, fmt);
+	vsnprintf(detail, LOGLINESIZE-1, fmt, ap);
 	va_end(ap);
+	fprintf(fp, "%s | %*s | %s\n", ts, NAMESIZE, user->record.name, detail);
+	fclose(fp);
 }
 
 int32_t who_find(const char *username)


=====================================
src/webclient/import.c
=====================================
--- a/src/webclient/import.c
+++ b/src/webclient/import.c
@@ -23,20 +23,16 @@ int incoming_pipe = -1;
 
 void broadcast_onoffcode(int code, int method, const char *sourceuser, const char *reason)
 {
-        char            logofftext[MAXTEXTLENGTH];
+        char logofftext[MAXTEXTLENGTH];
 
-        /* create the broadcast string */
-        snprintf(logofftext, MAXTEXTLENGTH-1, "%d,%d,%d,%s", code, method, 0, sourceuser);
+        if (reason == NULL)
+		snprintf(logofftext, MAXTEXTLENGTH - 1, "%d,%d,%d,%s",
+		         code, method, 0, sourceuser);
+	else
+		snprintf(logofftext, MAXTEXTLENGTH - 1, "%d,%d,%d,%s,%s",
+		         code, method, 0, sourceuser, reason);
 
-        /* add an optional reason */
-        if (reason != NULL)
-        {
-                char    reasontext[MAXTEXTLENGTH];
-                snprintf(reasontext, MAXTEXTLENGTH - 1, "%s,%s", logofftext, reason);
-                snprintf(logofftext, MAXTEXTLENGTH - 1, "%s", reasontext);
-        }
-
-        /* send the message */
+	logofftext[MAXTEXTLENGTH - 1] = '\0';
         ipc_send_to_all(IPC_CHECKONOFF, logofftext);
 }
 



View it on GitLab: https://projects.sucs.org/arthur/mw/commit/622cbc53786286544b028f2778d2a6bbd4d2e8e6

-- 
View it on GitLab: https://projects.sucs.org/arthur/mw/commit/622cbc53786286544b028f2778d2a6bbd4d2e8e6
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20181007/4b553e12/attachment-0001.html>


More information about the mw-devel mailing list