Programming 版 (精华区)

发信人: OGG (提心吊胆), 信区: Programming
标  题: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 17:33:59 2005), 站内

strcat(), 字符串的连接.
char *strcat(char *target,const char *source)
{
char *original=target;
while(*target) target++;     // Find the end of the string
while(*target++=*source++);
return(original);
}
要是真这样,那不就把target后面的数据覆盖了.......

--

    雄关漫道真如铁

        而今迈步从头跃


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 218.9.185.19]
────────────────────────────────────────
  pangwa (dream my dream)          于  (Fri Jul  8 17:40:53 2005)  说道:

应该就是这样的吧
strcat,strcpy本来就不是很安全的


发信人: artist (瀚峯), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 17:48:33 2005), 转信

该函数很简洁。安全要程序员自己考虑。

否则怎么办? target另申请空间? 可能会带来更多的问题。

请各位发表高论。

【 在 OGG (提心吊胆) 的大作中提到: 】
: strcat(), 字符串的连接.
: char *strcat(char *target,const char *source)
: {
: ...................

--
The most valuable thing is freedom.


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.239.143]
发信人: SwordLea (飞刀李), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 18:04:31 2005), 转信

我这里有 borland 版本的strcat

/*-----------------------------------------------------------------------*
 * filename - strcat.c
 *
 * function(s)
 *        strcat - appends one string to another
 *-----------------------------------------------------------------------*/

/*
 *      C/C++ Run Time Library - Version 5.0
 *
 *      Copyright (c) 1987, 1992 by Borland International
 *      All Rights Reserved.
 *
 */


#pragma  inline
#include <asmrules.h>
#include <string.h>

/*---------------------------------------------------------------------*

Name            strcat - appends one string to another

Usage           char *strcat(char *destin, const char *source);

Prototype in    string.h

Description     strcat appends a copy of source to the end of destin. The
                length of the resulting string is strlen(destin) +
                strlen(source).

Return value    returns a pointer dest

*---------------------------------------------------------------------*/
#undef strcat                   /* not an intrinsic */

#if defined(__FARFUNCS__)
#include <_farfunc.h>
#endif

char *_CType _FARFUNC strcat(register char *dest, const char *src)
{
        SaveSI
        SaveDI

asm     cld
#if  defined(__LARGE__) || defined(__COMPACT__)
asm     push    ds
#endif
#if LDATA
asm     les     di, dest        /* es:di = dest */
#else
asm     mov     di, dest        /* es:di = dest */
asm     push    ds
asm     pop     es
#endif
asm     mov     dx, di          /* save dest offset in dx */
asm     xor     al, al
asm     mov     cx, -1          /* find end of dest */
asm     repne  scasb
#if LDATA
asm     push    es
#endif
asm     lea     si,[di-1]       /* es:si points to terminating null in dest */
asm     LES_    di, src
asm     mov     cx,-1           /* figure out strlen(src) */
asm     repne  scasb
asm     not     cx              /* CX = strlen(src) + 1 */
asm     sub     di,cx           /* point es:di back to start of src */
#if LDATA
asm     push    es
asm     pop     ds              /* set DS: to seg of src */
asm     pop     es              /* restore ES: as seg of dest */
#endif
asm     xchg    si, di          /* DS:SI = src, ES:DI = dest+strlen(dest) */
asm     test    si,1            /* odd src? */
asm     jz      move_rest
asm     movsb                   /* move a byte to make src even */
asm     dec     cx
move_rest:
asm     shr     cx, 1
asm     rep  movsw
asm     jnc     move_last
asm     movsb
move_last:
asm     xchg    ax, dx                  /* return addr of string */
#if LDATA
asm     mov     dx, es
#endif
#if  defined(__LARGE__) || defined(__COMPACT__)
asm     pop     ds
#endif

#if LDATA
        return( (char *)(MK_LONG) );
#else
        return( (char *)_AX );
#endif
}


【 在 OGG (提心吊胆) 的大作中提到: 】
: strcat(), 字符串的连接.
: char *strcat(char *target,const char *source)
: {
: ...................

--
    俺是个原始人,喜欢到处穷溜达。有天逛到一个黑不溜秋的地方,觉得很气闷,就说了句“要有光!”然后大爆炸就开始了,时间就产生了,宇宙就初具规模了……

※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.224.2]


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.246.244]
发信人: worldguy (稀里糊涂), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 19:53:50 2005), 转信

这些库函数的源代码在哪能找到?

【 在 SwordLea (飞刀李) 的大作中提到: 】
: 我这里有 borland 版本的strcat
: /*-----------------------------------------------------------------------*
:  * filename - strcat.c
: ...................

--
  +--+--+--+--+--+--+--+--+--+--+
  |十|酒|八|七|六|五|四|三|二|一| 
  |分|薄|分|分|欲|谷|季|餐|目|贯| 
  |坦|烟|交|忍|不|皆|不|有|远|知| 
  |荡|断|往|让|张|食|懒|节|眺|足| 
  +--+--+--+--+--+--+--+--+--+--+


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.250.16]
发信人: iamxiaohan (xieyubo.cn), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 20:22:45 2005), 转信

一般编译器的安装目录下都有

【 在 worldguy (稀里糊涂) 的大作中提到: 】
: 这些库函数的源代码在哪能找到?


--
xieyubo@gmail.com


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.236.219]
发信人: cos (COS), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: BBS 哈工大紫丁香站 (Fri Jul  8 20:23:11 2005)

垃圾?你写个?
strcat本来就是要这么干的。

【 在 OGG (提心吊胆) 的大作中提到: 】
: strcat(), 字符串的连接.
: char *strcat(char *target,const char *source)
: {
: char *original=target;
: while(*target) target++;     // Find the end of the string
: while(*target++=*source++);
: return(original);
: }
: ...................



--

※ 来源:·哈工大紫丁香 http://bbs.hit.edu.cn·[FROM: 218.9.120.*]
发信人: iamxiaohan (xieyubo.cn), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 20:30:02 2005), 转信

strcat最多只是一个功能描述,具体实现没有谁规定它必须怎样干
【 在 cos (COS) 的大作中提到: 】
: 垃圾?你写个?
: strcat本来就是要这么干的。


--
xieyubo@gmail.com


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.236.219]
发信人: cos (COS), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: BBS 哈工大紫丁香站 (Fri Jul  8 20:31:57 2005)

strcat描述了字符串地址是不被改变的。
那么,有办法在不覆盖后面内容的情况下,strcat么?
不这么干,还能怎么干?

【 在 iamxiaohan (xieyubo.cn) 的大作中提到: 】
: strcat最多只是一个功能描述,具体实现没有谁规定它必须怎样干



--

※ 来源:·哈工大紫丁香 http://bbs.hit.edu.cn·[FROM: 218.9.120.*]
发信人: iamxiaohan (xieyubo.cn), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 20:33:27 2005), 转信

你可以自己写
【 在 cos (COS) 的大作中提到: 】
: strcat描述了字符串地址是不被改变的。
: 那么,有办法在不覆盖后面内容的情况下,strcat么?
: 不这么干,还能怎么干?
: ...................

--
xieyubo@gmail.com


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.236.219]
发信人: cos (COS), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: BBS 哈工大紫丁香站 (Fri Jul  8 20:37:40 2005)

谁写,也之后是在源串后面直接加。都得覆盖咯

【 在 iamxiaohan (xieyubo.cn) 的大作中提到: 】
: 你可以自己写



--

※ 来源:·哈工大紫丁香 http://bbs.hit.edu.cn·[FROM: 218.9.120.*]
发信人: OGG (提心吊胆), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 21:26:03 2005), 站内

看了一下,好像就是直接覆盖后面吧
asm     mov     di, dest        /* es:di = dest */
asm     push    ds
asm     pop     es
asm     mov     dx, di          /* save dest offset in dx */
asm     xor     al, al
asm     mov     cx, -1          /* find end of dest */
asm     repne  scasb
asm     lea     si,[di-1]       /* es:si points to terminating null in dest */
可是有时候并不会出错,
int main(int argc, char *argv[])
{
        char str1[] = "Hello";
        char str2[] = "fuck";
        char str3[] = "world";
        printf("str1 is:%s\n",str1);
        strcat(str1,str3);
        printf("str1 is:%s\n",str1);
        printf("str2 is:%s\n",str2);
        printf("str3 is:%s\n",str3);
        return 0;
}
中str2就不会被覆盖,难道是编译器给优化了?...

【 在 SwordLea (飞刀李) 的大作中提到: 】
: 我这里有 borland 版本的strcat
: /*-----------------------------------------------------------------------*
:  * filename - strcat.c
: ...................

--

    雄关漫道真如铁

        而今迈步从头跃


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 218.9.185.19]
发信人: cos (COS), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: BBS 哈工大紫丁香站 (Fri Jul  8 22:14:52 2005)

1.it's up to the user to get src str have enough space to contain src+dest.
2.str2 does NOT necessarily follow str1.

【 在 OGG (提心吊胆) 的大作中提到: 】
: 看了一下,好像就是直接覆盖后面吧
: asm     mov     di, dest        /* es:di = dest */
: asm     push    ds
: asm     pop     es
: asm     mov     dx, di          /* save dest offset in dx */
: asm     xor     al, al
: asm     mov     cx, -1          /* find end of dest */
: asm     repne  scasb
: ...................



--

※ 修改:·cos 於 Jul  8 22:17:47 2005 修改本文·[FROM: 218.9.120.*]
※ 来源:·哈工大紫丁香 http://bbs.hit.edu.cn·[FROM: 218.9.120.*]
发信人: OGG (提心吊胆), 信区: Programming
标  题: Re: 在网上看的strcat代码,不知道是不是真的,太垃圾了..
发信站: 哈工大紫丁香 (Fri Jul  8 22:20:50 2005), 站内

我终于弄明白了
gcc什么优化都没作,它只是把str1放在高处str2低一点,str3再低一点
例如
str1:0xbfffe640
str2:0xbfffe638
str3:0xbfffe620
哈哈,唬的我一愣一愣的,现在在str1前面又加了一个str0
这回就有覆盖了,哈

【 在 cos (COS) 的大作中提到: 】
: 1.it's up to the user to let src str have enough space to contain src+dest.
: 2.str2 does NOT necessarily follow str1.


--

    雄关漫道真如铁

        而今迈步从头跃


※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 218.9.185.19]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:208.679毫秒