strndup:RFC [英] strndup: RFC

查看:77
本文介绍了strndup:RFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



阅读comp.std.c我注意到有这样的消息:

WG14 Post Portland邮件现在可从WG14网站获得<在 http://www.open获取
-std.org/jtc1/sc22/wg14/



祝你好运

Keld Simonsen


我去了那里,发现有一份报告称为

ISO / IEC JTC1 SC22 WG14 WG14 / N1193

更安全的C库函数规范?

第二部分:动态分配功能


它提出了非常有趣的功能,其中包括getline和

getdelim,我引入了lcc-win32(巧合) )几个星期

前。


它也提出了strdup和strndup。 Lcc-win32已经提出了strdup,

但是strndup丢失了。这是一个提议的实现。我想

,看你的敏锐眼睛是否看到任何虫子或严重的问题。


提前谢谢


jacob

-------------------------------------- -------------------在这里剪切

#include< string.h>

#include< stdlib .h>

/ *

strndup函数复制不超过n个字符(

跟随空字符的字符不会被复制)字符串到动态

分配的缓冲区。复制的字符串应始终为空终止。

* /

char * strndup(const char * string,size_t s)

{

char * p,* r;

if(string == NULL)

返回NULL;

p = string;

while(s 0){

if(* p == 0)

break;

p ++;

s - ;

}

s =(p - string);

r = malloc(1 + s );

if(r){

strncpy(r,string,s);

r [s] = 0;

}

返回r;

}


#ifdef测试

#include < stdio.h>

#define MAXTEST 60

int main(无效)

{

char * table [MAXTEST];

char * str ="快速的棕色狐狸跳过懒狗;


for(int i = 0; i< ; MAXTEST; i ++){

table [i] = strndup(str,i);

}

for(int i = 0; i< MAXTEST; i ++){

printf(" [%4d]%s \ n,i,table [i]);

}

返回0;

}

#endif

Hi
Reading comp.std.c I noticed that there was a message like this:
The WG14 Post Portland mailing is now available from the WG14 web site
at http://www.open-std.org/jtc1/sc22/wg14/

Best regards
Keld Simonsen

I went there and found that there is a report called
ISO/IEC JTC1 SC22 WG14 WG14/N1193
Specification for Safer C Library Functions ?
Part II: Dynamic Allocation Functions

It proposes really interesting functions, among others getline and
getdelim, that I introduced into lcc-win32 (by coincidence) a few weeks
ago.

It proposes strdup and strndup too. Lcc-win32 proposes already strdup,
but strndup was missing. Here is a proposed implementation. I would like
to see if your sharp eyes see any bug or serious problem with it.

Thanks in advance

jacob
---------------------------------------------------------cut here
#include <string.h>
#include <stdlib.h>
/*
The strndup function copies not more than n characters (characters that
follow a null character are not copied) from string to a dynamically
allocated buffer. The copied string shall always be null terminated.
*/
char *strndup(const char *string,size_t s)
{
char *p,*r;
if (string == NULL)
return NULL;
p = string;
while (s 0) {
if (*p == 0)
break;
p++;
s--;
}
s = (p - string);
r = malloc(1+s);
if (r) {
strncpy(r,string,s);
r[s] = 0;
}
return r;
}

#ifdef TEST
#include <stdio.h>
#define MAXTEST 60
int main(void)
{
char *table[MAXTEST];
char *str = "The quick brown fox jumps over the lazy dog";

for (int i=0; i<MAXTEST;i++) {
table[i] = strndup(str,i);
}
for (int i=0; i<MAXTEST;i++) {
printf("[%4d] %s\n",i,table[i]);
}
return 0;
}
#endif

推荐答案

jacob navia说:


< snip>
jacob navia said:

<snip>

我想

看你的敏锐眼睛是否看到任何bug或者它的严重问题。
I would like
to see if your sharp eyes see any bug or serious problem with it.



我没有看到代码在满足其

规范方面存在任何严重问题,尽管我会考虑更换

I don''t see any serious problem with the code in terms of meeting its
specification, although I would consider replacing


strncpy(r,string,s);
strncpy(r,string,s);



with:


memcpy(r,string,s);


因为你已经检测到了终结器,并确切地知道了它的位置。


关注我的是规格本身,在我看来这是

遭受与strncpy相同的缺陷 - 即它没有表明

是否发生了截断。但当然这是设计问题,而不是C $ / b
问题。


-

Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

with:

memcpy(r, string, s);

since you''ve already detected the terminator and know precisely where it is.

The thing that does concern me is the spec itself, which seems to me to
suffer from the same flaw as strncpy - i.e. it gives no indication of
whether truncation occurred. But of course that''s a design issue, not a C
issue.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Richard Heathfieldaécrit:
Richard Heathfield a écrit :

jacob navia说:


< snip>
jacob navia said:

<snip>

>> I我想看看你的敏锐的眼睛是否看到任何虫子或严重的问题。
>>I would like
to see if your sharp eyes see any bug or serious problem with it.




我没有看到任何严重的问题虽然我会考虑更换



I don''t see any serious problem with the code in terms of meeting its
specification, although I would consider replacing


>但代码符合其

规范。函数strncpy(R,串,S);
> strncpy(r,string,s);




with:


memcpy(r,string,s);

,因为你已经检测到终结器,并确切知道它的位置。


与我有关的事情是规范本身,在我看来

遭受与strncpy相同的缺陷 - 即它没有表明

是否发生了截断。但当然这是一个设计问题,而不是C $ / b
问题。



with:

memcpy(r, string, s);

since you''ve already detected the terminator and know precisely where it is.

The thing that does concern me is the spec itself, which seems to me to
suffer from the same flaw as strncpy - i.e. it gives no indication of
whether truncation occurred. But of course that''s a design issue, not a C
issue.



有趣。我没有想到这一点。


你知道一个版本将这些信息反馈给用户吗?

有时候知道这一点很重要。 />

<关于主题>

由于lcc-win32支持可选参数,我可以这样做:

char * strndup(char * str,size_t siz,bool * pTruncated = NULL);

strndup(str,30)将是strndup(str,30,NULL)......

< / off topic>

Interesting. I did not think about that.

You know of a version that gives that information back to the user?
It is sometimes important to know.

< off topic>
Since lcc-win32 supports optional arguments I could do:
char *strndup(char *str,size_t siz,bool *pTruncated=NULL);
strndup(str,30) would be strndup(str,30,NULL)...
< / off topic >


文章< cK ************************* *****@bt.com>,

Richard Heathfield< rj*@see.sig.invalidwrote:
In article <cK******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:

>与我有关的事情是规范本身,在我看来,它受到与strncpy相同的缺陷 - 即它没有表明是否发生了截断。但当然这是一个设计问题,而不是C
问题。
>The thing that does concern me is the spec itself, which seems to me to
suffer from the same flaw as strncpy - i.e. it gives no indication of
whether truncation occurred. But of course that''s a design issue, not a C
issue.



当我使用我自己的strndup版本时,它总是从一个>制作一个

普通字符串。计数"字符串,所以没有问题

截断。我怀疑这是更常见的使用方法,而不是将一个字符串复制到一个可能不够大的缓冲区。


- Richard

-

在一些字母表中需要考虑多达32个字符

- 1963年的X3.4。

When I''ve used my own version of strndup, it''s always been make an
ordinary string from a "counted" string, so there is no question of
truncation. I suspect this is the more common use of it, rather than
copying a string to a buffer that might not be big enough.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


这篇关于strndup:RFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆