public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] safe_macros: add SAFE_STRTOL
@ 2012-02-09  8:13 Caspar Zhang
  2012-02-09  9:30 ` Wanlong Gao
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Caspar Zhang @ 2012-02-09  8:13 UTC (permalink / raw)
  To: LTP List

[-- Attachment #1: Type: text/plain, Size: 373 bytes --]


usage: SAFE_STRTOL(cleanup_fn, string, min, max);
when no specific range required, min could be LONG_MIN and max could be
LONG_MAX;

return: converted long int type value.

Signed-off-by: Caspar Zhang <caspar@casparzhang.com>
---
 include/safe_macros.h |    5 +++++
 lib/safe_macros.c     |   23 ++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletions(-)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-safe_macros-add-SAFE_STRTOL.patch --]
[-- Type: text/x-patch; name="0001-safe_macros-add-SAFE_STRTOL.patch", Size: 1823 bytes --]

diff --git a/include/safe_macros.h b/include/safe_macros.h
index 12f03ef..fbfe6cd 100644
--- a/include/safe_macros.h
+++ b/include/safe_macros.h
@@ -146,5 +146,10 @@ int safe_truncate(const char *file, const int lineno,
 #define SAFE_TRUNCATE(cleanup_fn, fd, length) \
 	safe_truncate(__FILE__, __LINE__, cleanup_fn, (path), (length))
 
+long safe_strtol(const char *file, const int lineno,
+	    void (cleanup_fn)(void), char *str, long min, long max);
+#define SAFE_STRTOL(cleanup_fn, str, min, max) \
+	safe_strtol(__FILE__, __LINE__, cleanup_fn, (str), (min), (max))
+
 #endif
 #endif
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 569952d..ddb7da4 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -4,6 +4,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <libgen.h>
+#include <limits.h>
 #include <pwd.h>
 #include <stdarg.h>
 #include <stdlib.h>
@@ -315,7 +316,7 @@ int safe_ftruncate(const char *file, const int lineno,
 	    void (cleanup_fn)(void), int fd, off_t length)
 {
 	int rval;
-	
+
 	rval = ftruncate(fd, length);
 	if (rval == -1) {
 		tst_brkm(TBROK|TERRNO, cleanup_fn, "ftruncate failed at %s:%d",
@@ -338,3 +339,23 @@ int safe_truncate(const char *file, const int lineno,
 
 	return rval;
 }
+
+long safe_strtol(const char *file, const int lineno,
+	    void (cleanup_fn)(void), char *str, long min, long max)
+{
+	int rval;
+	char *endptr;
+
+	rval = strtol(str, &endptr, 10);
+	if (rval == LONG_MAX || rval == LONG_MIN)
+		tst_brkm(TBROK|TERRNO, cleanup_fn, "strtol failed at %s:%d",
+			 file, lineno);
+	if (rval >= max || rval <= min)
+		tst_brkm(TBROK, cleanup_fn,
+			 "converted value out of range: %ld - %ld",
+			 min, max);
+	if (endptr == str || (*endptr != '\0' && *endptr != '\n'))
+		tst_brkm(TBROK, cleanup_fn, "Invalid value: %s", str);
+
+	return rval;
+}

[-- Attachment #3: Type: text/plain, Size: 317 bytes --]

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/

[-- Attachment #4: Type: text/plain, Size: 155 bytes --]

_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-03-20  8:51 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-09  8:13 [LTP] [PATCH] safe_macros: add SAFE_STRTOL Caspar Zhang
2012-02-09  9:30 ` Wanlong Gao
2012-02-09  9:45   ` Caspar Zhang
2012-02-09  9:46 ` [LTP] [PATCH v2] " Caspar Zhang
2012-02-09 10:00   ` Wanlong Gao
2012-02-09 10:28     ` Cyril Hrubis
2012-02-09 10:53 ` [LTP] [PATCH v3] " Caspar Zhang
2012-02-09 12:53   ` Cyril Hrubis
2012-02-09 13:12 ` [LTP] [PATCH v4] " Caspar Zhang
2012-02-09 13:59   ` Cyril Hrubis
2012-03-16  9:58   ` Gopal Kalita
2012-03-20  8:49     ` Caspar Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox