3 ms·
It should be noted that in this particular case it only happens when optimisations are enabled (-O2). With -O2: https://godbolt.org/#g:!((g:!((g:!((h:codeEdito
by hddqsb 4y ago
It should be noted that in this particular case it only happens when optimisations are enabled (-O2).
With -O2: https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:'%23include+%3Cstdint.h%3E%0A%23include+%3Cstdio.h%3E%0A%23include+%3Cstdlib.h%3E%0A%0Auint8_t+tab%5B0x1ff+%2B+1%5D%3B%0A%0Auint8_t+f(int32_t+x)%0A%7B%0A++++if+(x+%3C+0)%0A++++++++return+0%3B%0A++++int32_t+i+%3D+x+*+0x1ff+/+0xffff%3B%0A++++if+(i+%3E%3D+0+%26%26+i+%3C+sizeof(tab))+%7B%0A++++++++printf(%22tab%5B%25d%5D+looks+safe+because+%25d+is+between+%5B0%3B%25d%5B%5Cn%22,+i,+i,+(int)sizeof(tab))%3B%0A++++++++return+tab%5Bi%5D%3B%0A++++%7D%0A%0A++++return+0%3B%0A%7D%0A%0Aint+main(int+ac,+char+**av)%0A%7B%0A++++return+f(atoi(av%5B1%5D))%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:cg122,deviceViewOpen:'1',filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!(),options:'-O2',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+gcc+12.2+(Editor+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4 https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename...
Without -O2: https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:'%23include+%3Cstdint.h%3E%0A%23include+%3Cstdio.h%3E%0A%23include+%3Cstdlib.h%3E%0A%0Auint8_t+tab%5B0x1ff+%2B+1%5D%3B%0A%0Auint8_t+f(int32_t+x)%0A%7B%0A++++if+(x+%3C+0)%0A++++++++return+0%3B%0A++++int32_t+i+%3D+x+*+0x1ff+/+0xffff%3B%0A++++if+(i+%3E%3D+0+%26%26+i+%3C+sizeof(tab))+%7B%0A++++++++printf(%22tab%5B%25d%5D+looks+safe+because+%25d+is+between+%5B0%3B%25d%5B%5Cn%22,+i,+i,+(int)sizeof(tab))%3B%0A++++++++return+tab%5Bi%5D%3B%0A++++%7D%0A%0A++++return+0%3B%0A%7D%0A%0Aint+main(int+ac,+char+**av)%0A%7B%0A++++return+f(atoi(av%5B1%5D))%3B%0A%7D'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:cg122,deviceViewOpen:'1',filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!(),options:'',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+gcc+12.2+(Editor+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4 https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename...
[Edited. I originally wrote "It should be noted that this only happens when..." but inserted "in this particular case" after @eru pointed it out.]