Skip to contentSkip to conversation

Confused about usercopy_64.c

DISCUSSION Timur Tabi 3 participants 3 messages Mar 10, 2008

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

I'm confused about something in usercopy_64.c:

unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)

{
	if (likely(access_ok(VERIFY_READ, from, n)))
		n = __copy_from_user(to, from, n);
	else
		memset(to, 0, n);
	return n;
}

If access_ok() returns false, then that means that we cannot copy the data from user-space. So why are we returning 'n'? Shouldn't we return zero, to let the caller know that the function failed?

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#1 lore raw
Quoted 11 lines · Timur Tabi

I'm confused about something in usercopy_64.c:

unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)

{
	if (likely(access_ok(VERIFY_READ, from, n)))
		n = __copy_from_user(to, from, n);
	else
		memset(to, 0, n);
	return n;
}
Quoted 3 lines

If access_ok() returns false, then that means that we cannot copy the data from user-space. So why are we returning 'n'? Shouldn't we return zero, to let the caller know that the function failed?

copy_from_user() returns number of bytes _not_ copied.

-Olof

#2 lore raw
In reply to Timur Tabi
Quoted 11 lines · Timur Tabi

I'm confused about something in usercopy_64.c:

unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)

{
	if (likely(access_ok(VERIFY_READ, from, n)))
		n = __copy_from_user(to, from, n);
	else
		memset(to, 0, n);
	return n;
}
Quoted 2 lines

If access_ok() returns false, then that means that we cannot copy the data from user-space. So why are we returning 'n'?

To tell the caller that the function failed. The result is the number of bytes _left_ to be copying, ie. zero means success.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."