Thread (22 messages) 22 messages, 4 authors, 2014-02-06

Re: [PATCH v7 1/3] mm: add kstrdup_trimnl function

From: Sebastian Capella <hidden>
Date: 2014-02-06 23:48:50
Also in: linux-mm

Quoting Andrew Morton (2014-02-05 15:01:01)
On Wed, 05 Feb 2014 14:55:52 -0800 Sebastian Capella [off-list ref] wrote:
quoted
Quoting Andrew Morton (2014-02-05 13:50:52)
quoted
On Tue,  4 Feb 2014 12:43:49 -0800 Sebastian Capella [off-list ref] wrote:
quoted
kstrdup_trimnl creates a duplicate of the passed in
null-terminated string.  If a trailing newline is found, it
is removed before duplicating.  This is useful for strings
coming from sysfs that often include trailing whitespace due to
user input.
hm, why?  I doubt if any caller of this wants to retain leading and/or
trailing spaces and/or tabs.
Hi Andrew,

I agree the common case doesn't usually need leading or trailing whitespace.

Pavel and others pointed out that a valid filename could contain
newlines/whitespace at any position.
The number of cases in which we provide the kernel with a filename via
sysfs will be very very small, or zero.

If we can go through existing code and find at least a few sites which
can usefully employ kstrdup_trimnl() then fine, we have evidence.  But
I doubt if we can do that?
Hi Andrew,

I went through all of the store functions I could find and, though I
found a lot of examples handling \n, I found no other examples
specifically parsing filenames.  Most deal with integers.  Those parsing
commands often use sysfs_streq or otherwise are doing some custom
behavior that wouldn't suit a utility function.

For my purposes, it looks like v2 of the patch seems like the best
starting point based on all of the feedback I've received.  So I'm
moving back to a custom solution for parsing this input.  Unless
someone objects or has comments, I'll post something like the
function below.

static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
			    const char *buf, size_t n)
{
	dev_t res;
	int len = n;
	char *name;

	if (len && buf[len-1] == '\n')
		len--;
	name = kstrndup(buf, len, GFP_KERNEL);
	if (!name)
		return -ENOMEM;

	res = name_to_dev_t(name);
	kfree(name);
	if (!res)
		return -EINVAL;

	lock_system_sleep();
	swsusp_resume_device = res;
	unlock_system_sleep();
	printk(KERN_INFO "PM: Starting manual resume from disk\n");
	noresume = 0;
	software_resume();
	return n;
}


Thanks,

Sebastian

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help