Debian Codegraph @

debian/patches/Replace-Q_strncpy-to-be-safe-even-if-source-and-destinati.patch

raw
1 From: Poul Sander <[email protected]>
2 Date: Sun, 3 Apr 2022 18:56:44 +0200
3 Subject: Replace Q_strncpy to be safe even if source and destination overlaps
4 and remove a lot of bad usage of strncpy.
5
6 Origin: backport, oaxB53-r2, commit:5a9476eeb65ade97999d7b1bf80813cac6569f4e
7 ---
8 code/cgame/cg_servercmds.c | 9 ++---
9 code/cgame/cg_weapons.c | 2 +-
10 code/game/ai_chat.c | 3 +-
11 code/game/ai_cmd.c | 6 ++--
12 code/game/ai_dmq3.c | 18 ++++------
13 code/game/ai_team.c | 3 +-
14 code/game/g_killspree.c | 52 ++++++++++++++--------------
15 code/game/g_local.h | 6 ++--
16 code/game/g_vote.c | 84 +++++++++++++++++++++-------------------------
17 code/q3_ui/ui_challenges.c | 2 +-
18 code/qcommon/q_shared.c | 26 ++++++++++++--
19 code/ui/ui_shared.c | 3 +-
20 12 files changed, 108 insertions(+), 106 deletions(-)
21
22 diff --git a/code/cgame/cg_servercmds.c b/code/cgame/cg_servercmds.c
23 index 7295d9a..6d08eb4 100644
24 --- a/code/cgame/cg_servercmds.c
25 +++ b/code/cgame/cg_servercmds.c
26 @@ -454,21 +454,18 @@ void CG_ShaderStateChanged(void) {
27 while (o && *o) {
28 n = strstr(o, "=");
29 if (n && *n) {
30 - strncpy(originalShader, o, n-o);
31 - originalShader[n-o] = 0;
32 + Q_strncpyz(originalShader, o, n-o+1);
33 n++;
34 t = strstr(n, ":");
35 if (t && *t) {
36 - strncpy(newShader, n, t-n);
37 - newShader[t-n] = 0;
38 + Q_strncpyz(newShader, n, t-n+1);
39 } else {
40 break;
41 }
42 t++;
43 o = strstr(t, "@");
44 if (o) {
45 - strncpy(timeOffset, t, o-t);
46 - timeOffset[o-t] = 0;
47 + Q_strncpyz(timeOffset, t, o-t+1);
48 o++;
49 trap_R_RemapShader( originalShader, newShader, timeOffset );
50 }
51 diff --git a/code/cgame/cg_weapons.c b/code/cgame/cg_weapons.c
52 index a75707e..efb90ab 100644
53 --- a/code/cgame/cg_weapons.c
54 +++ b/code/cgame/cg_weapons.c
55 @@ -1009,7 +1009,7 @@ void CG_RegisterItemVisuals( int itemNum ) {
56
57 item = &bg_itemlist[ itemNum ];
58
59 - memset( itemInfo, 0, sizeof( &itemInfo ) );
60 + memset( itemInfo, 0, sizeof( *itemInfo ) );
61 itemInfo->registered = qtrue;
62
63 itemInfo->models[0] = trap_R_RegisterModel( item->world_model[0] );
64 diff --git a/code/game/ai_chat.c b/code/game/ai_chat.c
65 index cf21d5e..922aca9 100644
66 --- a/code/game/ai_chat.c
67 +++ b/code/game/ai_chat.c
68 @@ -265,8 +265,7 @@ char *BotMapTitle(void) {
69
70 trap_GetServerinfo(info, sizeof(info));
71
72 - strncpy(mapname, Info_ValueForKey( info, "mapname" ), sizeof(mapname)-1);
73 - mapname[sizeof(mapname)-1] = '\0';
74 + Q_strncpyz(mapname, Info_ValueForKey( info, "mapname" ), sizeof(mapname));
75
76 return mapname;
77 }
78 diff --git a/code/game/ai_cmd.c b/code/game/ai_cmd.c
79 index 8b4bb87..1b0b961 100644
80 --- a/code/game/ai_cmd.c
81 +++ b/code/game/ai_cmd.c
82 @@ -1208,8 +1208,7 @@ void BotMatch_JoinSubteam(bot_state_t *bs, bot_match_t *match) {
83 //get the sub team name
84 trap_BotMatchVariable(match, TEAMNAME, teammate, sizeof(teammate));
85 //set the sub team name
86 - strncpy(bs->subteam, teammate, 32);
87 - bs->subteam[31] = '\0';
88 + Q_strncpyz(bs->subteam, teammate, 32);
89 //
90 trap_BotMatchVariable(match, NETNAME, netname, sizeof(netname));
91 BotAI_BotInitialChat(bs, "joinedteam", teammate, NULL);
92 @@ -1401,8 +1400,7 @@ void BotMatch_StartTeamLeaderShip(bot_state_t *bs, bot_match_t *match) {
93 if (match->subtype & ST_I) {
94 //get the team mate that will be the team leader
95 trap_BotMatchVariable(match, NETNAME, teammate, sizeof(teammate));
96 - strncpy(bs->teamleader, teammate, sizeof(bs->teamleader));
97 - bs->teamleader[sizeof(bs->teamleader)-1] = '\0';
98 + Q_strncpyz(bs->teamleader, teammate, sizeof(bs->teamleader));
99 }
100 //chats for someone else
101 else {
102 diff --git a/code/game/ai_dmq3.c b/code/game/ai_dmq3.c
103 index f8c4173..4bd394a 100644
104 --- a/code/game/ai_dmq3.c
105 +++ b/code/game/ai_dmq3.c
106 @@ -1507,10 +1507,9 @@ char *ClientName(int client, char *name, int size) {
107 BotAI_Print(PRT_ERROR, "ClientName: client out of range\n");
108 return "[client out of range]";
109 }
110 - trap_GetConfigstring(CS_PLAYERS+client, buf, sizeof(buf));
111 - strncpy(name, Info_ValueForKey(buf, "n"), size-1);
112 - name[size-1] = '\0';
113 - Q_CleanStr( name );
114 + trap_GetConfigstring(CS_PLAYERS + client, buf, sizeof (buf));
115 + Q_strncpyz(name, Info_ValueForKey(buf, "n"), size);
116 + Q_CleanStr(name);
117 return name;
118 }
119
120 @@ -1526,9 +1525,8 @@ char *ClientSkin(int client, char *skin, int size) {
121 BotAI_Print(PRT_ERROR, "ClientSkin: client out of range\n");
122 return "[client out of range]";
123 }
124 - trap_GetConfigstring(CS_PLAYERS+client, buf, sizeof(buf));
125 - strncpy(skin, Info_ValueForKey(buf, "model"), size-1);
126 - skin[size-1] = '\0';
127 + trap_GetConfigstring(CS_PLAYERS + client, buf, sizeof (buf));
128 + Q_strncpyz(skin, Info_ValueForKey(buf, "model"), size);
129 return skin;
130 }
131
132 @@ -1636,8 +1634,7 @@ char *EasyClientName(int client, char *buf, int size) {
133 memmove(ptr, ptr+1, strlen(ptr + 1)+1);
134 }
135 }
136 - strncpy(buf, name, size-1);
137 - buf[size-1] = '\0';
138 + Q_strncpyz(buf, name, size);
139 return buf;
140 }
141
142 @@ -3787,8 +3784,7 @@ void BotMapScripts(bot_state_t *bs) {
143
144 trap_GetServerinfo(info, sizeof(info));
145
146 - strncpy(mapname, Info_ValueForKey( info, "mapname" ), sizeof(mapname)-1);
147 - mapname[sizeof(mapname)-1] = '\0';
148 + Q_strncpyz(mapname, Info_ValueForKey(info, "mapname"), sizeof (mapname));
149
150 if (!Q_stricmp(mapname, "q3tourney6")) {
151 vec3_t mins = {700, 204, 672}, maxs = {964, 468, 680};
152 diff --git a/code/game/ai_team.c b/code/game/ai_team.c
153 index c1d0394..94fc92b 100644
154 --- a/code/game/ai_team.c
155 +++ b/code/game/ai_team.c
156 @@ -2138,8 +2138,7 @@ void BotTeamAI(bot_state_t *bs) {
157 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
158 BotSayVoiceTeamOrder(bs, -1, VOICECHAT_STARTLEADER);
159 ClientName(bs->client, netname, sizeof(netname));
160 - strncpy(bs->teamleader, netname, sizeof(bs->teamleader));
161 - bs->teamleader[sizeof(bs->teamleader)-1] = '\0';
162 + Q_strncpyz(bs->teamleader, netname, sizeof(bs->teamleader));
163 bs->becometeamleader_time = 0;
164 }
165 return;
166 diff --git a/code/game/g_killspree.c b/code/game/g_killspree.c
167 index 9a13303..32a9cc2 100644
168 --- a/code/game/g_killspree.c
169 +++ b/code/game/g_killspree.c
170 @@ -223,37 +223,35 @@ static char *fillPlaceHolder( char *stringToSearch, char *placeHolder, char *rep
171 static char output[ MAX_SAY_TEXT ];
172 char *p;
173
174 - if( !( p = strstr( stringToSearch, placeHolder ) ) )
175 - return stringToSearch;
176 -
177 - strncpy( output, stringToSearch, p - stringToSearch );
178 - output[ p - stringToSearch ] = '\0';
179 -
180 - Q_snprintf( output + ( p - stringToSearch ), output - stringToSearch, "%s%s", replaceWith, p + strlen( placeHolder ) );
181 -
182 - return output;
183 + if( !( p = strstr( stringToSearch, placeHolder ) ) )
184 + return stringToSearch;
185 +
186 + Q_snprintf( output, MAX_SAY_TEXT, "%.*s%s%s", (int)(p - stringToSearch), stringToSearch, replaceWith, p+strlen(placeHolder));
187 +
188 + return output;
189 }
190
191 //This concatenate's the message to broadcast to the clients.
192 static char *CreateMessage( gentity_t *ent, char *message, char *spreeNumber )
193 {
194 - static char output[ MAX_SAY_TEXT ] = { "" };
195 -
196 - char name[ MAX_NAME_LENGTH ];
197 - //Do some sanity checks
198 - if( !ent ) {
199 - return output;
200 - } else if ( !*message ) {
201 - return output;
202 - } else if ( !spreeNumber ) {
203 - return output;
204 - }
205 - //Get the player name.
206 - Q_strncpyz( name, ent->client->pers.netname, sizeof( name ) );
207 - //Do Our Replacements
208 - Q_strncpyz( output, fillPlaceHolder( message, "[n]", name ), sizeof( output ) );
209 - Q_strncpyz( output, fillPlaceHolder( output, "[k]", spreeNumber ), sizeof( output ) );
210 - return output;
211 + static char output[ MAX_SAY_TEXT ] = { "" };
212 +
213 + char name[ MAX_NAME_LENGTH ];
214 + //Do some sanity checks
215 + if( !ent ) {
216 + return output;
217 + } else if ( !*message ) {
218 + return output;
219 + } else if ( !spreeNumber ) {
220 + return output;
221 + }
222 + //Get the player name.
223 + Q_strncpyz( name, ent->client->pers.netname, sizeof( name ) );
224 + //Do Our Replacements
225 + Q_strncpyz( output, fillPlaceHolder( message, "[n]", name ), sizeof( output ) );
226 + //The following line has a risk of copying output into output. Q_strncpyz can handle the case.
227 + Q_strncpyz( output, fillPlaceHolder( output, "[k]", spreeNumber ), sizeof( output ) );
228 + return output;
229 }
230
231
232 @@ -514,4 +512,4 @@ void G_checkForMultiKill( gentity_t *ent ) {
233 }
234
235
236 -
237 \ No newline at end of file
238 +
239 diff --git a/code/game/g_local.h b/code/game/g_local.h
240 index 4b82323..05643ca 100644
241 --- a/code/game/g_local.h
242 +++ b/code/game/g_local.h
243 @@ -923,7 +923,7 @@ void PlayerStore_restore(char* guid, playerState_t *ps);
244 //
245 // g_vote.c
246 //
247 -int allowedVote(char *commandStr);
248 +int allowedVote(const char *commandStr);
249 void CheckVote( void );
250 void CountVotes( void );
251 void ClientLeaving(int clientNumber);
252 @@ -950,8 +950,8 @@ typedef struct {
253 extern char custom_vote_info[1024];
254
255 extern t_mappage getMappage(int page);
256 -extern int allowedMap(char *mapname);
257 -extern int allowedGametype(char *gametypeStr);
258 +extern int allowedMap(const char *mapname);
259 +extern int allowedGametype(const char *gametypeStr);
260 extern int allowedTimelimit(int limit);
261 extern int allowedFraglimit(int limit);
262 extern int VoteParseCustomVotes( void );
263 diff --git a/code/game/g_vote.c b/code/game/g_vote.c
264 index 3695674..7e0d37a 100644
265 --- a/code/game/g_vote.c
266 +++ b/code/game/g_vote.c
267 @@ -29,28 +29,25 @@ allowedVote
268 ==================
269 */
270 #define MAX_VOTENAME_LENGTH 14 //currently the longest string is "/map_restart/\0" (14 chars)
271 -int allowedVote(char *commandStr) {
272 - char tempStr[MAX_VOTENAME_LENGTH];
273 - int length;
274 - char voteNames[MAX_CVAR_VALUE_STRING];
275 - trap_Cvar_VariableStringBuffer( "g_voteNames", voteNames, sizeof( voteNames ) );
276 - if(!Q_stricmp(voteNames, "*" ))
277 - return qtrue; //if star, everything is allowed
278 - length = strlen(commandStr);
279 - if(length>MAX_VOTENAME_LENGTH-3)
280 - {
281 - //Error: too long
282 - return qfalse;
283 - }
284 - //Now constructing a string that starts and ends with '/' like: "/clientkick/"
285 - tempStr[0] = '/';
286 - strncpy(&tempStr[1],commandStr,length);
287 - tempStr[length+1] = '/';
288 - tempStr[length+2] = '\0';
289 - if(Q_stristr(voteNames,tempStr) != NULL)
290 - return qtrue;
291 - else
292 - return qfalse;
293 +int allowedVote(const char *commandStr) {
294 + char tempStr[MAX_VOTENAME_LENGTH];
295 + int length;
296 + char voteNames[MAX_CVAR_VALUE_STRING];
297 + trap_Cvar_VariableStringBuffer( "g_voteNames", voteNames, sizeof( voteNames ) );
298 + if(Q_strequal(voteNames, "*" ))
299 + return qtrue; //if star, everything is allowed
300 + length = strlen(commandStr);
301 + if(length>MAX_VOTENAME_LENGTH-3)
302 + {
303 + //Error: too long
304 + return qfalse;
305 + }
306 + //Now constructing a string that starts and ends with '/' like: "/clientkick/"
307 + Q_snprintf(tempStr, sizeof(tempStr), "/%s/", commandStr);
308 + if(Q_stristr(voteNames,tempStr) != NULL)
309 + return qtrue;
310 + else
311 + return qfalse;
312 }
313
314 /*
315 @@ -126,7 +123,7 @@ allowedMap
316 ==================
317 */
318
319 -int allowedMap(char *mapname) {
320 +int allowedMap(const char *mapname) {
321 int length;
322 fileHandle_t file; //To check that the map actually exists.
323 char buffer[MAX_MAPS_TEXT];
324 @@ -173,28 +170,25 @@ allowedGametype
325 ==================
326 */
327 #define MAX_GAMETYPENAME_LENGTH 5 //currently the longest string is "/12/\0" (5 chars)
328 -int allowedGametype(char *gametypeStr) {
329 - char tempStr[MAX_GAMETYPENAME_LENGTH];
330 - int length;
331 - char voteGametypes[MAX_CVAR_VALUE_STRING];
332 - trap_Cvar_VariableStringBuffer( "g_voteGametypes", voteGametypes, sizeof( voteGametypes ) );
333 - if(!Q_stricmp(voteGametypes, "*" ))
334 - return qtrue; //if star, everything is allowed
335 - length = strlen(gametypeStr);
336 - if(length>MAX_GAMETYPENAME_LENGTH-3)
337 - {
338 - //Error: too long
339 - return qfalse;
340 - }
341 - tempStr[0] = '/';
342 - strncpy(&tempStr[1],gametypeStr,length);
343 - tempStr[length+1] = '/';
344 - tempStr[length+2] = '\0';
345 - if(Q_stristr(voteGametypes,tempStr) != NULL)
346 - return qtrue;
347 - else {
348 - return qfalse;
349 - }
350 +int allowedGametype(const char *gametypeStr) {
351 + char tempStr[MAX_GAMETYPENAME_LENGTH];
352 + int length;
353 + char voteGametypes[MAX_CVAR_VALUE_STRING];
354 + trap_Cvar_VariableStringBuffer( "g_voteGametypes", voteGametypes, sizeof( voteGametypes ) );
355 + if(Q_strequal(voteGametypes, "*" ))
356 + return qtrue; //if star, everything is allowed
357 + length = strlen(gametypeStr);
358 + if(length>MAX_GAMETYPENAME_LENGTH-3)
359 + {
360 + //Error: too long
361 + return qfalse;
362 + }
363 + Q_snprintf(tempStr, sizeof(tempStr),"/%s/", gametypeStr);
364 + if(Q_stristr(voteGametypes,tempStr) != NULL)
365 + return qtrue;
366 + else {
367 + return qfalse;
368 + }
369 }
370
371 /*
372 diff --git a/code/q3_ui/ui_challenges.c b/code/q3_ui/ui_challenges.c
373 index c2ed62c..3033c97 100644
374 --- a/code/q3_ui/ui_challenges.c
375 +++ b/code/q3_ui/ui_challenges.c
376 @@ -373,7 +373,7 @@ static void UI_Challenges_Init( void ) {
377 challenges.entryIntText[i].style = UI_RIGHT;
378 challenges.entryIntText[i].string = challenges.entryIntString[i];
379
380 - strncpy(challenges.entryIntString[i],va("%u",challenges.entryInt[i]),MAX_INT_AS_STRING);
381 + Q_snprintf(challenges.entryIntString[i], MAX_INT_AS_STRING, "%u", challenges.entryInt[i]);
382 y += BIGCHAR_HEIGHT+2;
383 }
384
385 diff --git a/code/qcommon/q_shared.c b/code/qcommon/q_shared.c
386 index d1ead36..2832ea3 100644
387 --- a/code/qcommon/q_shared.c
388 +++ b/code/qcommon/q_shared.c
389 @@ -727,11 +727,31 @@ char* Q_strrchr( const char* string, int c )
390 return sp;
391 }
392
393 +/*
394 + Copied from code/game/bg_lib.c
395 + It is here to avoid undifined behavior when source and destination overlap.
396 + This is used quite a lot in the code.
397 + */
398 +static char *qstrncpy(char *strDest, const char *strSource, size_t count) {
399 + char *s;
400 +
401 + s = strDest;
402 + while (*strSource && count) {
403 + *s++ = *strSource++;
404 + count--;
405 + }
406 + while (count--) {
407 + *s++ = 0;
408 + }
409 + return strDest;
410 +}
411 +
412 /*
413 =============
414 Q_strncpyz
415
416 Safe strncpy that ensures a trailing zero
417 +Does not result in undefiend behavior if source=destination
418 =============
419 */
420 void Q_strncpyz( char *dest, const char *src, int destsize ) {
421 @@ -745,8 +765,10 @@ void Q_strncpyz( char *dest, const char *src, int destsize ) {
422 Com_Error(ERR_FATAL,"Q_strncpyz: destsize < 1" );
423 }
424
425 - strncpy( dest, src, destsize-1 );
426 - dest[destsize-1] = 0;
427 + if (dest != src) {
428 + qstrncpy(dest, src, destsize - 1);
429 + }
430 + dest[destsize - 1] = 0;
431 }
432
433 int Q_stricmpn (const char *s1, const char *s2, int n) {
434 diff --git a/code/ui/ui_shared.c b/code/ui/ui_shared.c
435 index d63a796..08eab51 100644
436 --- a/code/ui/ui_shared.c
437 +++ b/code/ui/ui_shared.c
438 @@ -2936,8 +2936,7 @@ void Item_Text_Wrapped_Paint(itemDef_t *item) {
439 start = textPtr;
440 p = strchr(textPtr, '\r');
441 while (p && *p) {
442 - strncpy(buff, start, p-start+1);
443 - buff[p-start] = '\0';
444 + Q_strncpyz(buff, start, p - start + 1);
445 DC->drawText(x, y, item->textscale, color, buff, 0, 0, item->textStyle);
446 y += height + 5;
447 start += p - start + 1;