From: rmccabe@sourceware.org <rmccabe@sourceware.org>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] conga/ricci/common utils.cpp
Date: 5 Feb 2008 18:29:05 -0000 [thread overview]
Message-ID: <20080205182905.22399.qmail@sourceware.org> (raw)
CVSROOT: /cvs/cluster
Module name: conga
Changes by: rmccabe at sourceware.org 2008-02-05 18:29:05
Modified files:
ricci/common : utils.cpp
Log message:
Better string to num conversion
Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/utils.cpp.diff?cvsroot=cluster&r1=1.12&r2=1.13
--- conga/ricci/common/utils.cpp 2008/01/02 20:47:34 1.12
+++ conga/ricci/common/utils.cpp 2008/02/05 18:29:05 1.13
@@ -23,8 +23,12 @@
#include "utils.h"
#include "executils.h"
-#include <openssl/md5.h>
+#include <unistd.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <math.h>
+#include <errno.h>
+#include <openssl/md5.h>
//#include <iostream>
@@ -201,24 +205,44 @@
long long
utils::to_long(const String& str)
{
- return atoll(str.c_str());
+ char *p = NULL;
+ long long ret;
+ ret = strtoll(strip(str).c_str(), &p, 10);
+ if (p != NULL && *p != '\0')
+ throw String("Not a number: ") + str;
+ if (ret == LLONG_MIN && errno == ERANGE)
+ throw String("Numeric underflow: ") + str;
+ if (ret == LLONG_MAX && errno == ERANGE)
+ throw String("Numeric overflow: ") + str;
+ return ret;
}
float
utils::to_float(const String& str)
{
- float num = 0;
+ char *p = NULL;
+ float ret;
- sscanf(strip(str).c_str(), "%f", &num);
- return num;
+ ret = strtof(strip(str).c_str(), &p);
+ if (p != NULL && *p == '\0')
+ throw String("Invalid floating point number: ") + str;
+ if (ret == 0 && errno == ERANGE)
+ throw String("Floating point underflow: ") + str;
+ if ((ret == HUGE_VALF || ret == HUGE_VALL) && errno == ERANGE)
+ throw String("Floating point overflow: ") + str;
+
+ return ret;
}
String
utils::to_string(int value)
{
char tmp[64];
+ int ret;
- sprintf(tmp, "%d", value);
+ ret = snprintf(tmp, sizeof(tmp), "%d", value);
+ if (ret < 0 || (size_t) ret >= sizeof(tmp))
+ throw String("Invalid integer");
return tmp;
}
@@ -226,8 +250,11 @@
utils::to_string(long value)
{
char tmp[64];
+ int ret;
- sprintf(tmp, "%ld", value);
+ ret = snprintf(tmp, sizeof(tmp), "%ld", value);
+ if (ret < 0 || (size_t) ret >= sizeof(tmp))
+ throw String("Invalid long integer");
return tmp;
}
@@ -235,8 +262,11 @@
utils::to_string(long long value)
{
char tmp[64];
+ int ret;
- sprintf(tmp, "%lld", value);
+ ret = snprintf(tmp, sizeof(tmp), "%lld", value);
+ if (ret < 0 || (size_t) ret >= sizeof(tmp))
+ throw String("Invalid long long integer");
return tmp;
}
reply other threads:[~2008-02-05 18:29 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080205182905.22399.qmail@sourceware.org \
--to=rmccabe@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).