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

* Re: [LTP] [PATCH] safe_macros: add SAFE_STRTOL
  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
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 12+ messages in thread
From: Wanlong Gao @ 2012-02-09  9:30 UTC (permalink / raw)
  To: Caspar Zhang; +Cc: LTP List

On 02/09/2012 04:13 PM, Caspar Zhang wrote:

> +
> +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)


Shouldn't we check the errno here?

Thanks
-Wanlong Gao

> +		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;
> +}



------------------------------------------------------------------------------
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/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH] safe_macros: add SAFE_STRTOL
  2012-02-09  9:30 ` Wanlong Gao
@ 2012-02-09  9:45   ` Caspar Zhang
  0 siblings, 0 replies; 12+ messages in thread
From: Caspar Zhang @ 2012-02-09  9:45 UTC (permalink / raw)
  To: gaowanlong; +Cc: LTP List

On 02/09/2012 05:30 PM, Wanlong Gao wrote:
> On 02/09/2012 04:13 PM, Caspar Zhang wrote:
> 
>> +
>> +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)
> 
> 
> Shouldn't we check the errno here?

Ah yes. I'll make a v2 soon.

> 
> Thanks
> -Wanlong Gao
> 
>> +		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;
>> +}
> 
> 


------------------------------------------------------------------------------
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/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v2] safe_macros: add SAFE_STRTOL
  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:46 ` Caspar Zhang
  2012-02-09 10:00   ` Wanlong Gao
  2012-02-09 10:53 ` [LTP] [PATCH v3] " Caspar Zhang
  2012-02-09 13:12 ` [LTP] [PATCH v4] " Caspar Zhang
  3 siblings, 1 reply; 12+ messages in thread
From: Caspar Zhang @ 2012-02-09  9:46 UTC (permalink / raw)
  To: LTP List

[-- Attachment #1: Type: text/plain, Size: 374 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     |   24 +++++++++++++++++++++++-
 2 files changed, 28 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: 1882 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..c6280c6 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,24 @@ 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 ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
+		    || (errno != 0 && rval == 0))
+		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

* Re: [LTP] [PATCH v2] safe_macros: add SAFE_STRTOL
  2012-02-09  9:46 ` [LTP] [PATCH v2] " Caspar Zhang
@ 2012-02-09 10:00   ` Wanlong Gao
  2012-02-09 10:28     ` Cyril Hrubis
  0 siblings, 1 reply; 12+ messages in thread
From: Wanlong Gao @ 2012-02-09 10:00 UTC (permalink / raw)
  To: Caspar Zhang; +Cc: LTP List

On 02/09/2012 05:46 PM, Caspar Zhang wrote:

> +
> +long safe_strtol(const char *file, const int lineno,
> +	    void (cleanup_fn)(void), char *str, long min, long max)
> +{
> +	int rval;


Ah, long rval?

> +	char *endptr;
> +
> +	rval = strtol(str, &endptr, 10);
> +	if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
> +		    || (errno != 0 && rval == 0))
> +		tst_brkm(TBROK|TERRNO, cleanup_fn, "strtol failed at %s:%d",
> +			 file, lineno);
> +	if (rval >= max || rval <= min)


I'm not sure with this check. ;)

Thanks
-Wanlong Gao

> +		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;
> +}



------------------------------------------------------------------------------
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/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v2] safe_macros: add SAFE_STRTOL
  2012-02-09 10:00   ` Wanlong Gao
@ 2012-02-09 10:28     ` Cyril Hrubis
  0 siblings, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2012-02-09 10:28 UTC (permalink / raw)
  To: Wanlong Gao; +Cc: LTP List

Hi!
> > +	char *endptr;
> > +
> > +	rval = strtol(str, &endptr, 10);
> > +	if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
> > +		    || (errno != 0 && rval == 0))
> > +		tst_brkm(TBROK|TERRNO, cleanup_fn, "strtol failed at %s:%d",
> > +			 file, lineno);
> > +	if (rval >= max || rval <= min)
> 
> 
> I'm not sure with this check. ;)

I would expect that the converted  value could reach both max and min.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
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/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v3] safe_macros: add SAFE_STRTOL
  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:46 ` [LTP] [PATCH v2] " Caspar Zhang
@ 2012-02-09 10:53 ` Caspar Zhang
  2012-02-09 12:53   ` Cyril Hrubis
  2012-02-09 13:12 ` [LTP] [PATCH v4] " Caspar Zhang
  3 siblings, 1 reply; 12+ messages in thread
From: Caspar Zhang @ 2012-02-09 10:53 UTC (permalink / raw)
  To: LTP List

[-- Attachment #1: Type: text/plain, Size: 436 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>
---

v2->v3: fix type of rval; make rval able to reach min & max

 include/safe_macros.h |    5 +++++
 lib/safe_macros.c     |   24 +++++++++++++++++++++++-
 2 files changed, 28 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: 1881 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..406e2c6 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,24 @@ 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)
+{
+	long rval;
+	char *endptr;
+
+	rval = strtol(str, &endptr, 10);
+	if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
+		    || (errno != 0 && rval == 0))
+		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

* Re: [LTP] [PATCH v3] safe_macros: add SAFE_STRTOL
  2012-02-09 10:53 ` [LTP] [PATCH v3] " Caspar Zhang
@ 2012-02-09 12:53   ` Cyril Hrubis
  0 siblings, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2012-02-09 12:53 UTC (permalink / raw)
  To: Caspar Zhang; +Cc: LTP List

Hi!
> +
> +long safe_strtol(const char *file, const int lineno,
> +	    void (cleanup_fn)(void), char *str, long min, long max)
> +{
> +	long rval;
> +	char *endptr;

I think that we should set errno to zero here as the call itself doesn't
clear it on success.

> +
> +	rval = strtol(str, &endptr, 10);
> +	if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
> +		    || (errno != 0 && rval == 0))
> +		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);

I would consider printing the file and lineno in these two tst_brkm too
and add '' around the %s that is used to print the passed string.

> +	return rval;
> +}


-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
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/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* [LTP] [PATCH v4] safe_macros: add SAFE_STRTOL
  2012-02-09  8:13 [LTP] [PATCH] safe_macros: add SAFE_STRTOL Caspar Zhang
                   ` (2 preceding siblings ...)
  2012-02-09 10:53 ` [LTP] [PATCH v3] " Caspar Zhang
@ 2012-02-09 13:12 ` Caspar Zhang
  2012-02-09 13:59   ` Cyril Hrubis
  2012-03-16  9:58   ` Gopal Kalita
  3 siblings, 2 replies; 12+ messages in thread
From: Caspar Zhang @ 2012-02-09 13:12 UTC (permalink / raw)
  To: LTP List

[-- Attachment #1: Type: text/plain, Size: 438 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>
---

v3->v4: clear errno before invoking strtol(); better output

 include/safe_macros.h |    5 +++++
 lib/safe_macros.c     |   26 +++++++++++++++++++++++++-
 2 files changed, 30 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: 1948 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..4c8cbb7 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,26 @@ 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)
+{
+	long rval;
+	char *endptr;
+
+	errno = 0;
+	rval = strtol(str, &endptr, 10);
+	if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
+		    || (errno != 0 && rval == 0))
+		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) at %s:%d",
+			 min, max, file, lineno);
+	if (endptr == str || (*endptr != '\0' && *endptr != '\n'))
+		tst_brkm(TBROK, cleanup_fn,
+			 "Invalid value: '%s' at %s:%d", str, file, lineno);
+
+	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

* Re: [LTP] [PATCH v4] safe_macros: add SAFE_STRTOL
  2012-02-09 13:12 ` [LTP] [PATCH v4] " Caspar Zhang
@ 2012-02-09 13:59   ` Cyril Hrubis
  2012-03-16  9:58   ` Gopal Kalita
  1 sibling, 0 replies; 12+ messages in thread
From: Cyril Hrubis @ 2012-02-09 13:59 UTC (permalink / raw)
  To: Caspar Zhang; +Cc: LTP List

Hi!
> 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>

Acked-by: Cyril Hrubis <chrubis@suse.cz>

(feel free to commit)

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
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/
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v4] safe_macros: add SAFE_STRTOL
  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
  1 sibling, 1 reply; 12+ messages in thread
From: Gopal Kalita @ 2012-03-16  9:58 UTC (permalink / raw)
  To: ltp-list


In the file -- lib/safe_macros.c , the header file errno.h needs to be
included.
#include <errno.h>

Otherwise, it gives the Following error  (while compiling)--
safe_macros.c: In function ‘safe_strtol’:
safe_macros.c:350: error: ‘errno’ undeclared (first use in this function)
safe_macros.c:350: error: (Each undeclared identifier is reported only once
safe_macros.c:350: error: for each function it appears in.)
safe_macros.c:352: error: ‘ERANGE’ undeclared (first use in this function)

-- 
View this message in context: http://old.nabble.com/-LTP---PATCH--safe_macros%3A-add-SAFE_STRTOL-tp33291529p33516012.html
Sent from the ltp-list mailing list archive at Nabble.com.


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here 
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

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

* Re: [LTP] [PATCH v4] safe_macros: add SAFE_STRTOL
  2012-03-16  9:58   ` Gopal Kalita
@ 2012-03-20  8:49     ` Caspar Zhang
  0 siblings, 0 replies; 12+ messages in thread
From: Caspar Zhang @ 2012-03-20  8:49 UTC (permalink / raw)
  To: Gopal Kalita; +Cc: ltp-list

On 03/16/2012 05:58 PM, Gopal Kalita wrote:
> 
> In the file -- lib/safe_macros.c , the header file errno.h needs to be
> included.
> #include <errno.h>
> 
> Otherwise, it gives the Following error  (while compiling)--
> safe_macros.c: In function ‘safe_strtol’:
> safe_macros.c:350: error: ‘errno’ undeclared (first use in this function)
> safe_macros.c:350: error: (Each undeclared identifier is reported only once
> safe_macros.c:350: error: for each function it appears in.)
> safe_macros.c:352: error: ‘ERANGE’ undeclared (first use in this function)
> 

Hi, this should have been fixed in commit 9af0bd7b

Thanks,
Caspar

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here 
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[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