Skip to content

Commit fd19382

Browse files
rpjdaytorvalds
authored andcommittedJul 25, 2008
lib: allow memparse() to accept a NULL and ignorable second parm
Extend memparse() to allow the caller to use a NULL second parameter, which would represent no interest in returning the address of the end of the parsed string. In numerous cases, callers invoke memparse() to parse a possibly-suffixed string (such as "64K" or "2G" or whatever) and define a character pointer to accept the end pointer being returned by memparse() even though they have no interest in it and promptly throw it away. This (backward-compatible) enhancement allows callers to use NULL in the cases where they just don't care about getting back that end pointer. [[email protected]: coding-style fixes] Signed-off-by: Robert P. J. Day <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent cb345d7 commit fd19382

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed
 

‎lib/cmdline.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ char *get_options(const char *str, int nints, int *ints)
116116
/**
117117
* memparse - parse a string with mem suffixes into a number
118118
* @ptr: Where parse begins
119-
* @retptr: (output) Pointer to next char after parse completes
119+
* @retptr: (output) Optional pointer to next char after parse completes
120120
*
121121
* Parses a string into a number. The number stored at @ptr is
122122
* potentially suffixed with %K (for kilobytes, or 1024 bytes),
@@ -126,11 +126,13 @@ char *get_options(const char *str, int nints, int *ints)
126126
* megabyte, or one gigabyte, respectively.
127127
*/
128128

129-
unsigned long long memparse (char *ptr, char **retptr)
129+
unsigned long long memparse(char *ptr, char **retptr)
130130
{
131-
unsigned long long ret = simple_strtoull (ptr, retptr, 0);
131+
char *endptr; /* local pointer to end of parsed string */
132132

133-
switch (**retptr) {
133+
unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
134+
135+
switch (*endptr) {
134136
case 'G':
135137
case 'g':
136138
ret <<= 10;
@@ -140,10 +142,14 @@ unsigned long long memparse (char *ptr, char **retptr)
140142
case 'K':
141143
case 'k':
142144
ret <<= 10;
143-
(*retptr)++;
145+
endptr++;
144146
default:
145147
break;
146148
}
149+
150+
if (retptr)
151+
*retptr = endptr;
152+
147153
return ret;
148154
}
149155

0 commit comments

Comments
 (0)