Skip to content

Commit 6e70a6f

Browse files
sidt4joebonrichie
authored andcommitted
Add pk_strtoulong() helper function
1 parent 36e55b2 commit 6e70a6f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/pk-shared.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,34 @@ pk_strtouint (const gchar *text, guint *value)
264264
return TRUE;
265265
}
266266

267+
/**
268+
* pk_strtoulong:
269+
* @text: The string to convert
270+
* @value: The numeric return value
271+
*
272+
* Converts a string into a unsigned long value in a safe way.
273+
*
274+
* Return value: %TRUE if the string was converted correctly
275+
**/
276+
gboolean
277+
pk_strtoulong (const gchar *text, gulong *value)
278+
{
279+
gboolean ret;
280+
guint64 value_raw;
281+
282+
ret = pk_strtouint64 (text, &value_raw);
283+
if (!ret)
284+
return FALSE;
285+
286+
/* out of range */
287+
if (value_raw > G_MAXULONG)
288+
return FALSE;
289+
290+
/* cast back down to value */
291+
*value = (gulong) value_raw;
292+
return TRUE;
293+
}
294+
267295
/**
268296
* pk_strzero:
269297
* @text: The text to check

src/pk-shared.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ gboolean pk_strtoint (const gchar *text,
3939
gint *value);
4040
gboolean pk_strtouint (const gchar *text,
4141
guint *value);
42+
gboolean pk_strtoulong (const gchar *text,
43+
gulong *value);
4244
gboolean pk_strtouint64 (const gchar *text,
4345
guint64 *value);
4446
GDBusNodeInfo *pk_load_introspection (const gchar *filename,

0 commit comments

Comments
 (0)