[LLVMdev] Another memory fun
Jonathan Johnson
jon at alacatialabs.com
Sun Jan 6 16:43:43 PST 2008
I'm somewhat new here, but if I'm wrong, hopefully someone will chime
in :)
On Jan 6, 2008, at 3:20 PM, Zalunin Pavel wrote:
> but why this code don't work:
>
> ; ModuleID = 'sample.lz'
> @.str1 = internal global [6 x i8] c"world\00" ; <[6 x
> i8]*> [#uses=1]
> @.str2 = internal global [7 x i8] c"hello \00" ; <[7 x
> i8]*> [#uses=1]
> @.str7 = internal global [7 x i8] c"father\00" ; <[7 x
> i8]*> [#uses=1]
> @.str8 = internal global [8 x i8] c"mother \00" ; <[8 x
> i8]*> [#uses=1]
All of the strings here are allocated with exact sizes for their
contents...
> declare i32 @puts(i8*)
>
> declare i8* @strcat(i8*, i8*)
>
> declare i32 @strlen(i8*)
>
> declare void @llvm.memcpy.i32(i8*, i8*, i32, i32)
>
> define i32 @main() {
> mainBlock:
> %str3 = getelementptr [7 x i8]* @.str2, i64 0, i64
> 0 ; <i8*> [#uses=2]
> %str4 = getelementptr [6 x i8]* @.str1, i64 0, i64
> 0 ; <i8*> [#uses=1]
> call i8* @strcat( i8* %str3, i8* %str4 ) ;
> <i8*>:0 [#uses=0]
And here, you're attempting to call strcat on "hello " with "world".
strcat does not create a new string, it just writes to the first
pointer wherever it finds the first NULL byte. With the first string
only having an allocated size of 7, adding 6 more characters will
overwrite memory.
HTH,
Jon
--
Jonathan Johnson
President
Alacatia Labs, Inc.
http://www.alacatialabs.com/
More information about the llvm-dev
mailing list