History

relocate with mmap: fix for systems not using /tmp

On some systems /tmp may be inaccessible and instead $TMPDIR is supposed to be used. Prefer that and fall back to /tmp if undefined or empty. Also check return values of mkstemp and ftruncate.

Sigrid Solveig Haflínudóttir committed 9 months ago

Commit 0680cb51 parent ca23a091 file · +15 −4Browse files

Unified · Split

tccrun.c+15−4
@@ -120,11 +120,22 @@ static int rt_mem(TCCState *s1, int size) · 119 unmodified lines
120120 #ifdef CONFIG_SELINUX
121121 /* Using mmap instead of malloc */
122122 void *prw;
123− char tmpfname[] = "/tmp/.tccrunXXXXXX";
124− int fd = mkstemp(tmpfname);
123+ static const char *tmpdir;
124+ char tmpfname[256];
125+ int fd;
126+
127+ if (tmpdir == NULL) {
128+ tmpdir = getenv("TMPDIR");
129+ if (tmpdir == NULL || *tmpdir == 0)
130+ tmpdir = "/tmp";
131+ }
132+ snprintf(tmpfname, sizeof(tmpfname), "%s/.tccrunXXXXXX", tmpdir);
133+ fd = mkstemp(tmpfname);
134+ if (fd < 0)
135+ return tcc_error_noabort("tccrun: could not create temp file");
125136 unlink(tmpfname);
126− ftruncate(fd, size);
127−
137+ if (ftruncate(fd, size) < 0)
138+ return tcc_error_noabort("tccrun: could not resize temp file");
128139 ptr = mmap(NULL, size * 2, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
129140 /* mmap RW memory at fixed distance */
130141 prw = mmap((char*)ptr + size, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, 0);

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