On Thu, Nov 10, 2011 at 4:48 PM, Zdenek Styblik
<zdenek.styb...@gmail.com> wrote:
> Hi,
>
> I'm thinking what would be the best way to get rid of atoi() in
> 'lib/ipmi_main.c' which is used for parsing of 'd', 'p' and 'C'
> arguments.
>
> It seems to me to be a good idea to implement function in
> 'lib/helper.c' called str2short(). The reason is 'lib/helper.c' has
> 'limits.h' already included and 'lib/ipmi_main.c' doesn't.
> I would implement it in general way as str2uchar(), so: int
> str2short(char * str, uint16_t * short_ptr); and error messages/states
> handled outside of this function.
>
> Does it sound reasonable?
>
> Thanks,
> Z.
>
Proposed patch is attached. Note, however, this patch can't be applied
directly against code in CVS, because it is from different version of
ipmitool.
I also haven't checked out other modules for similar problems or
replacements, but I'm planning to do so.
Regards,
Z.
Index: lib/ipmi_main.c
===================================================================
--- lib/ipmi_main.c (revision 21129)
+++ lib/ipmi_main.c (working copy)
@@ -333,13 +333,28 @@
goto out_free;
break;
case 'd':
- devnum = atoi(optarg);
+ if (str2short(optarg, &devnum) != 0) {
+ lprintf(LOG_ERR, "Invalid parameter given or out of range for '-d'.");
+ rc = -1;
+ goto out_free;
+ }
+ /* add check device number is -gt 0 */
break;
case 'p':
- port = atoi(optarg);
+ if (str2short(optarg, &port) != 0) {
+ lprintf(LOG_ERR, "Invalid parameter given or out of range for '-p'.");
+ rc = -1;
+ goto out_free;
+ }
+ /* add check port is -gt 0 */
break;
case 'C':
- cipher_suite_id = atoi(optarg);
+ if (str2short(optarg, &cipher_suite_id) != 0) {
+ lprintf(LOG_ERR, "Invalid parameter given or out of range for '-C'.");
+ rc = -1;
+ goto out_free;
+ }
+ /* add check Cipher is -gt 0 */
break;
case 'v':
verbose++;
Index: lib/helper.c
===================================================================
--- lib/helper.c (revision 21140)
+++ lib/helper.c (working copy)
@@ -108,6 +108,33 @@
}
fprintf(stderr, "\n");
}
+/* Desc: Convert array of chars into uint16_t and check for overflows
+ * @str: array of chars to parse from
+ * @short_ptr: pointer to address where uint16_t will be stored
+ */
+int
+str2short(char * str, uint16_t * short_ptr)
+{
+ int32_t arg_long = 0;
+ char *end_ptr = 0;
+ if (str == NULL || short_ptr == NULL) {
+ /* Some of the parameters to the function are missing or are invalid */
+ return 1;
+ }
+ *short_ptr = 0;
+ errno = 0;
+ arg_long = strtol(str, &end_ptr, 0);
+ if (*end_ptr != '\0' || errno != 0) {
+ /* Invalid input given by user/overflow occured */
+ return 2;
+ }
+ if (arg_long > INT_MAX || arg_long < INT_MIN || arg_long == LONG_MIN ||
+ arg_long == LONG_MAX) {
+ return 3;
+ }
+ *short_ptr = (int16_t)arg_long;
+ return 0;
+}
/* Desc: Convert array of chars into uint8_t and check for overflows
* @str: array of chars to parse from
* @uchr_ptr: pointer to address where uint8_t will be stored
Index: include/ipmitool/helper.h
===================================================================
--- include/ipmitool/helper.h (revision 21129)
+++ include/ipmitool/helper.h (working copy)
@@ -61,6 +61,7 @@
const char * val2str(uint16_t val, const struct valstr * vs);
const char * oemval2str(uint16_t oem,uint16_t val, const struct oemvalstr * vs);
+int str2short(char * str, int16_t * short_ptr);
int str2uchar(char * str, uint8_t * uchr_ptr);
uint16_t str2val(const char * str, const struct valstr * vs);
void print_valstr(const struct valstr * vs, const char * title, int loglevel);
------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Ipmitool-devel mailing list
Ipmitool-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ipmitool-devel