strdup在传递NULL时转储核心

时间:2022-02-11 13:00:41

strdup(null) dumps core.

strdup(null)转储核心。

Tried in on ubuntu and freeBSD both.

试过ubuntu和freeBSD。

why? Shouldn't it return null?

为什么?它不应该返回null吗?

char *b = NULL;
a = strdup(b);

This will dump core on strdup call.

这将在strdup调用上转储核心。

4 个解决方案

#1


15  

That's quite ok.

那很好。

The documentation implies that it's argument must be string, if it's something else, such as a null pointer, it's anyones guess what'll happen. In essence, you get undefined behavior when passing a NULL pointer to strdup.

文档暗示它的参数必须是字符串,如果它是其他东西,比如空指针,那么任何人都会猜测会发生什么。实质上,在将NULL指针传递给strdup时会出现未定义的行为。

It's quite normal for functions to yield undefined behavor if you pass them something you're not supposed to. Many standard C function such as strcpy, strlen does not accept null pointers either.

如果你传递一些你不应该的东西,那么函数产生未定义的行为是很正常的。许多标准的C函数,如strcpy,strlen也不接受空指针。

#2


6  

strdup returns NULL in case of allocation failure.

在分配失败的情况下,strdup返回NULL。

This is not allocation failure. The behavior when a wild pointer is passed in is undefined. You're required to pass in a valid pointer to a NUL-terminated string.

这不是分配失败。传入Wild指针时的行为是未定义的。您需要传入一个指向NUL终止字符串的有效指针。

#3


1  

I think that it is expected behaviour.

我认为这是预期的行为。

#4


1  

Of course, all the answers are correct. Nonetheless, in case of strdup(), it would be nice to have it to be able to deal with null pointers, too - just return a "null" string too, in that case. Of course, it is easy to define a strdupnullok() function with that behaviour as a wrapper around strdup(), but it is then an additional wrapper and function call, so makes code a tiny bit slower.

当然,所有答案都是正确的。尽管如此,在strdup()的情况下,让它能够处理空指针也是很好的 - 在这种情况下,只返回一个“null”字符串。当然,很容易将strdupnullok()函数定义为strdup()的包装器,但它是一个额外的包装器和函数调用,因此使代码变得慢一点。

#1


15  

That's quite ok.

那很好。

The documentation implies that it's argument must be string, if it's something else, such as a null pointer, it's anyones guess what'll happen. In essence, you get undefined behavior when passing a NULL pointer to strdup.

文档暗示它的参数必须是字符串,如果它是其他东西,比如空指针,那么任何人都会猜测会发生什么。实质上,在将NULL指针传递给strdup时会出现未定义的行为。

It's quite normal for functions to yield undefined behavor if you pass them something you're not supposed to. Many standard C function such as strcpy, strlen does not accept null pointers either.

如果你传递一些你不应该的东西,那么函数产生未定义的行为是很正常的。许多标准的C函数,如strcpy,strlen也不接受空指针。

#2


6  

strdup returns NULL in case of allocation failure.

在分配失败的情况下,strdup返回NULL。

This is not allocation failure. The behavior when a wild pointer is passed in is undefined. You're required to pass in a valid pointer to a NUL-terminated string.

这不是分配失败。传入Wild指针时的行为是未定义的。您需要传入一个指向NUL终止字符串的有效指针。

#3


1  

I think that it is expected behaviour.

我认为这是预期的行为。

#4


1  

Of course, all the answers are correct. Nonetheless, in case of strdup(), it would be nice to have it to be able to deal with null pointers, too - just return a "null" string too, in that case. Of course, it is easy to define a strdupnullok() function with that behaviour as a wrapper around strdup(), but it is then an additional wrapper and function call, so makes code a tiny bit slower.

当然,所有答案都是正确的。尽管如此,在strdup()的情况下,让它能够处理空指针也是很好的 - 在这种情况下,只返回一个“null”字符串。当然,很容易将strdupnullok()函数定义为strdup()的包装器,但它是一个额外的包装器和函数调用,因此使代码变得慢一点。