History

tiny_libmaker: strip leading directory to avoid buffer overrun

The arhdr.ar_name has 16 bytes. Long object names esp. with leading directory were causing a buffer overrun which was detected by glibc.

grischka committed 15 years ago

Commit 69fe7581 parent 21c2a681 file · +13 −3Browse files

Unified · Split

win32/tools/tiny_libmaker.c+13−3
@@ -78,6 +78,7 @@ int main(int argc, char **argv) · 77 unmodified lines
7878 int *afpos = NULL;
7979 int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
8080 char afile[260], tfile[260], stmp[20];
81+ char *file, *name;
8182
8283
8384 strcpy(afile, "ar_test.a");
@@ -197,9 +198,18 @@ int main(int argc, char **argv) · 113 unmodified lines
197198 }
198199 }
199200 }
200− memset(&arhdro.ar_name, ' ', sizeof(arhdr.ar_name));
201− strcpy(arhdro.ar_name, argv[iarg]);
202− arhdro.ar_name[strlen(argv[iarg])] = '/';
201+
202+ file = argv[iarg];
203+ for (name = strchr(file, 0);
204+ name > file && name[-1] != '/' && name[-1] != '\\';
205+ --name);
206+ istrlen = strlen(name);
207+ if (istrlen >= sizeof(arhdro.ar_name))
208+ istrlen = sizeof(arhdro.ar_name) - 1;
209+ memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
210+ memcpy(arhdro.ar_name, name, istrlen);
211+ arhdro.ar_name[istrlen] = '/';
212+
203213 sprintf(stmp, "%-10d", fsize);
204214 memcpy(&arhdro.ar_size, stmp, 10);
205215 fwrite(&arhdro, sizeof(arhdro), 1, fo);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t