[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] c-string return question
From: |
Felix |
Subject: |
Re: [Chicken-users] c-string return question |
Date: |
Fri, 14 Oct 2011 03:12:12 -0400 (EDT) |
> My point is, that this code is valid wrt. the manual and apparently
> valid given my understanding of what the C code tries to do:
> make it possible to return on stack strings.
Can you point out the relevant manual section?
> For me it ends up like this:
>
> --------------- #define return(x) C_cblock C_r =
> (C_mpointer(&C_a,(void*)(x))); goto
> --------------- #C_ret; C_cblockend static C_word C_fcall stub26(C_word
> C_buf,C_word
> --------------- #C_a0) C_regparm; C_regparm static C_word C_fcall
> stub26(C_word
> --------------- #C_buf,C_word C_a0){ C_word
> C_r=C_SCHEME_UNDEFINED,*C_a=(C_word*)C_buf;
> --------------- #unsigned int ch=(unsigned int )C_num_to_unsigned_int(C_a0);
> static
> --------------- #unsigned char off[6]={0xFC,0xF8,0xF0,0xE0,0xC0,0x00};
> int size=5; C_char buf[7];
> buf[6]='\0';
> if (ch < 0x80) {
> buf[5]=ch;
> } else {
> buf[size--]=(ch&0x3F)|0x80; ch=ch>>6;
> while (ch) { buf[size--]=(ch&0x3F)|0x80; ch=ch>>6; }
> /* Write the size information into the first byte */
> ++size;
> buf[size]=off[size]|buf[size];
> }
> return(buf+size);
>
> C_ret:
> #undef return
>
> return C_r;}
> ---------------
>
> to be called like this:
>
> ---------
> t3=C_a_i_bytevector(&a,1,C_fix(3));
> t4=C_i_foreign_unsigned_integer_argumentp(t2);
> t5=stub26(t3,t4);
> C_trace("##sys#peek-c-string");
> t6=*((C_word*)lf[5]+1);
> ((C_proc4)(void*)(*((C_word*)t6+1)))(4,t6,t1,t5,C_fix(0));}
> ---------
>
> But somehow the "C_proc4" receives clobbered memory.
"stub26" is called as a normal C function and the returned buffer
will be clobbered if it points to (nonstatic) heap data.
cheers,
felix
Re: [Chicken-users] c-string return question, Jim Ursetto, 2011/10/13
- Re: [Chicken-users] c-string return question, Jörg F . Wittenberger, 2011/10/13
- Re: [Chicken-users] c-string return question, John Cowan, 2011/10/13
- Re: [Chicken-users] c-string return question, Alan Post, 2011/10/13
- Re: [Chicken-users] c-string return question, John Cowan, 2011/10/13
- Re: [Chicken-users] c-string return question, Alan Post, 2011/10/13
- Re: [Chicken-users] c-string return question, Jörg F . Wittenberger, 2011/10/13
- Re: [Chicken-users] c-string return question, Jörg F . Wittenberger, 2011/10/13
Re: [Chicken-users] c-string return question, Jörg F . Wittenberger, 2011/10/13