Deutsch English Français Italiano |
<10375s6$195fs$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: olcott <polcott333@gmail.com> Newsgroups: comp.lang.c Subject: Re: Memory protection between compilation units? Date: Sat, 21 Jun 2025 15:49:10 -0500 Organization: A noiseless patient Spider Lines: 40 Message-ID: <10375s6$195fs$1@dont-email.me> References: <20250611153239.6bc43323@mateusz> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 21 Jun 2025 22:49:11 +0200 (CEST) Injection-Info: dont-email.me; posting-host="c33a34d5810729869e79acc5a916ae39"; logging-data="1349116"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+MijI2zywpe+zFjmEUIrE7" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:3DcT/SGsZccvgyl1QPy3w7OKKNk= Content-Language: en-US In-Reply-To: <20250611153239.6bc43323@mateusz> X-Antivirus-Status: Clean X-Antivirus: Norton (VPS 250621-6, 6/21/2025), Outbound message On 6/11/2025 8:32 AM, Mateusz Viste wrote: > This might not be a strictly C question, but it definitely concerns all > C programmers. > > Earlier today, I fixed an out-of-bounds write bug. An obvious issue: > > static int *socks[0xffff]; > > void update_my_socks(int *sock, int val) { > socks[val & 0xffff] = sock; > } > > While the presented issue is common knowledge for anyone familiar with > C, *locating* the bug was challenging. The program did not crash at the > moment of the out-of-bounds write but much later - somewhere entirely > different, in a different object file that maintained a static pointer > for tracking a position in a linked list. To my surprise, the pointer > was randomly reset to NULL about once a week, causing a segfault. > Tracing this back to an unrelated out-of-bounds write elsewhere in the > code was tedious, to say the least. > > This raises a question: how can such corruptions be detected sooner? > Protected mode prevents interference between programs but doesn’t > safeguard a program from corrupting itself. Is there a way to enforce > memory protection between module files of the same program? After all, > static objects shouldn't be accessible outside their compilation unit. > > How would you approach this? > > Mateusz > https://en.cppreference.com/w/c/types/integer.html One way to fix the problem in the above specific case is to define: void update_my_socks(int *sock, uint16_t val) -- Copyright 2025 Olcott "Talent hits a target no one else can hit; Genius hits a target no one else can see." Arthur Schopenhauer