From 336842d35cf7e5d68c7f71d961d1fff8c19d49e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Thu, 20 Sep 2012 01:07:08 +0100 Subject: [PATCH] rtsprange: fix formatting and parsing of range floating-point values Other locales might use a comma instead of a floating point for floats, which might lead to parsing errors. https://bugzilla.gnome.org/show_bug.cgi?id=684411 --- gst-libs/gst/rtsp/gstrtsprange.c | 33 ++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/gst-libs/gst/rtsp/gstrtsprange.c b/gst-libs/gst/rtsp/gstrtsprange.c index bd8aae40fb..a5f4b84056 100644 --- a/gst-libs/gst/rtsp/gstrtsprange.c +++ b/gst-libs/gst/rtsp/gstrtsprange.c @@ -55,6 +55,20 @@ #include "gstrtsprange.h" +static gdouble +gst_strtod (const gchar * dstr) +{ + gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, }; + + /* canonicalise floating point string so we can handle float strings + * in the form "24.930" or "24,930" irrespective of the current locale. + * We should always be getting floats in 24.930 format with a floating point, + * but let's accept malformed ones as well, easy mistake to make after all */ + g_strlcpy (s, dstr, sizeof (s)); + g_strdelimit (s, ",", '.'); + return g_ascii_strtod (s, NULL); +} + /* npt-time = "now" | npt-sec | npt-hhmmss * npt-sec = 1*DIGIT [ "." *DIGIT ] * npt-hhmmss = npt-hh ":" npt-mm ":" npt-ss [ "." *DIGIT ] @@ -70,20 +84,16 @@ parse_npt_time (const gchar * str, GstRTSPTime * time) } else if (str[0] == '\0') { time->type = GST_RTSP_TIME_END; } else if (strstr (str, ":")) { - gfloat seconds; gint hours, mins; - sscanf (str, "%2d:%2d:%f", &hours, &mins, &seconds); - + sscanf (str, "%2d:%2d:", &hours, &mins); + str = strchr (str, ':') + 1; + str = strchr (str, ':') + 1; time->type = GST_RTSP_TIME_SECONDS; - time->seconds = ((hours * 60) + mins) * 60 + seconds; + time->seconds = ((hours * 60) + mins) * 60 + gst_strtod (str); } else { - gfloat seconds; - - sscanf (str, "%f", &seconds); - time->type = GST_RTSP_TIME_SECONDS; - time->seconds = seconds; + time->seconds = gst_strtod (str); } return GST_RTSP_OK; } @@ -180,11 +190,14 @@ invalid: static gboolean npt_time_string (const GstRTSPTime * time, GString * string) { + gchar dstrbuf[G_ASCII_DTOSTR_BUF_SIZE] = { 0, }; gboolean res = TRUE;; switch (time->type) { case GST_RTSP_TIME_SECONDS: - g_string_append_printf (string, "%f", time->seconds); + /* need to format floating point value strings as in C locale */ + g_ascii_dtostr (dstrbuf, G_ASCII_DTOSTR_BUF_SIZE, time->seconds); + g_string_append (string, dstrbuf); break; case GST_RTSP_TIME_NOW: g_string_append (string, "now");