Skip to content

Commit

Permalink
conn_handler: fallback to strtod if needed
Browse files Browse the repository at this point in the history
namely to support scientific notation, see also #123
  • Loading branch information
filippog committed Apr 23, 2015
1 parent 5717ac7 commit d1fe3df
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/conn_handler.c
@@ -1,3 +1,4 @@
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -355,6 +356,8 @@ int handle_client_connect(statsite_conn_handler *handle) {
static double str2double(const char *s, char **end) {
double val = 0.0;
char neg = 0;
const char *orig_s = s;

if (*s == '-') {
neg = 1;
s++;
Expand All @@ -372,8 +375,13 @@ static double str2double(const char *s, char **end) {
}
val += frac / pow(10.0, digits);
}
if (unlikely(*s == 'E' || *s == 'e')) {
errno = 0;
return strtod(orig_s, end);
}
if (neg) val *= -1.0;
if (end) *end = (char*)s;
errno = 0;
return val;
}

Expand Down Expand Up @@ -447,7 +455,7 @@ static int handle_ascii_client_connect(statsite_conn_handler *handle) {

// Convert the value to a double
val = str2double(val_str, &endptr);
if (unlikely(endptr == val_str)) {
if (unlikely(endptr == val_str || errno == ERANGE)) {
syslog(LOG_WARNING, "Failed value conversion! Input: %s", val_str);
goto ERR_RET;
}
Expand Down

0 comments on commit d1fe3df

Please sign in to comment.