summaryrefslogtreecommitdiff
path: root/sysutils/screen/files/patch-src_attacher.c
blob: 3dd7d179b3c46de2924ac46c3a09609171a3b049 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
diff --git attacher.c attacher.c
index f2b60f0..9138176 100644
--- attacher.c
+++ attacher.c
@@ -431,53 +431,62 @@ void SendCmdMessage(char *sty, char *match, char **av, int query)
 			sty[FILENAME_MAX] = 0;
 		if (strlen(sty) > 2 * MAXSTR - 1)
 			sty[2 * MAXSTR - 1] = 0;
-		sprintf(SocketPath + strlen(SocketPath), "/%s", sty);
+
+		snprintf(SocketPath + strlen(SocketPath),
+			sizeof(SocketPath) - strlen(SocketPath), "/%s", sty);
+
 		if ((s = MakeClientSocket(1)) == -1)
 			exit(1);
 	}
-	memset((char *)&m, 0, sizeof(Message));
+
+	memset(&m, 0, sizeof(Message));
 	m.type = query ? MSG_QUERY : MSG_COMMAND;
+
 	if (attach_tty) {
-		strncpy(m.m_tty, attach_tty_is_in_new_ns ? attach_tty_name_in_ns : attach_tty, ARRAY_SIZE(m.m_tty) - 1);
-		m.m_tty[ARRAY_SIZE(m.m_tty) - 1] = 0;
+		snprintf(m.m_tty, sizeof(m.m_tty), "%s",
+			attach_tty_is_in_new_ns ? attach_tty_name_in_ns : attach_tty);
 	}
+
 	p = m.m.command.cmd;
 	n = 0;
-	size_t space_left = ARRAY_SIZE(m.m.command.cmd);
+	size_t space_left = sizeof(m.m.command.cmd);
 
 	for (; *av && n < MAXARGS - 1; ++av, ++n) {
-               int printed = snprintf(p, space_left, "%s", *av);
-               if (printed < 0 || (size_t)printed >= space_left)
-                       Panic(0, "Total length of the command to send too large.\n");
+		int printed = snprintf(p, space_left, "%s", *av);
+		if (printed < 0 || (size_t)printed >= space_left)
+			Panic(0, "Total length of the command to send too large.\n");
 
-               printed += 1; // add null terminator
-               p += printed;
-               space_left -= printed;
+		/* move past string and null terminator */
+		p += printed + 1;
+		space_left -= printed + 1;
 	}
-	*p = 0;
+
+	*p = '\0';
 	m.m.command.nargs = n;
-	strncpy(m.m.attach.auser, LoginName, ARRAY_SIZE(m.m.attach.auser) - 1);
-	m.m.command.auser[ARRAY_SIZE(m.m.command.auser) - 1] = 0;
+
+	snprintf(m.m.command.auser, sizeof(m.m.command.auser), "%s", LoginName);
 	m.protocol_revision = MSG_REVISION;
-	strncpy(m.m.command.preselect, preselect ? preselect : "", ARRAY_SIZE(m.m.command.preselect) - 1);
-	m.m.command.preselect[ARRAY_SIZE(m.m.command.preselect) - 1] = 0;
+	snprintf(m.m.command.preselect, sizeof(m.m.command.preselect), "%s",
+		preselect ? preselect : "");
 	m.m.command.apid = getpid();
+
 	if (query) {
 		/* Create a server socket so we can get back the result */
 		char *sp = SocketPath + strlen(SocketPath);
-		char query[] = "-queryX";
 		char c;
 		int r = -1;
+
 		for (c = 'A'; c <= 'Z'; c++) {
-			query[6] = c;
-			strncpy(sp, query, strlen(SocketPath));
+			snprintf(sp, sizeof(SocketPath) - strlen(SocketPath),
+				"-query%c", c);
 			if ((r = MakeServerSocket()) >= 0)
 				break;
 		}
+
 		if (r < 0) {
 			for (c = '0'; c <= '9'; c++) {
-				query[6] = c;
-				strncpy(sp, query, strlen(SocketPath));
+				snprintf(sp, sizeof(SocketPath) - strlen(SocketPath),
+					"-query%c", c);
 				if ((r = MakeServerSocket()) >= 0)
 					break;
 			}
@@ -486,8 +495,8 @@ void SendCmdMessage(char *sty, char *match, char **av, int query)
 		if (r < 0)
 			Panic(0, "Could not create a listening socket to read the results.");
 
-		strncpy(m.m.command.writeback, SocketPath, ARRAY_SIZE(m.m.command.writeback) - 1);
-		m.m.command.writeback[ARRAY_SIZE(m.m.command.writeback) - 1] = '\0';
+		snprintf(m.m.command.writeback, sizeof(m.m.command.writeback),
+			"%s", SocketPath);
 
 		/* Send the message, then wait for a response */
 		xsignal(SIGCONT, QueryResultSuccess);
@@ -504,7 +513,7 @@ void SendCmdMessage(char *sty, char *match, char **av, int query)
 		ReceiveRaw(r);
 		close(r);
 		unlink(SocketPath);
-		if (QueryResult == 2)	/* An error happened */
+		if (QueryResult == 2)   /* An error happened */
 			exit(1);
 	} else {
 		if (WriteMessage(s, &m))