From 6fbf2ba87d80f7cf9658bb8810c8116e84067cf5 Mon Sep 17 00:00:00 2001 From: Sebastian Andrzej Siewior Date: Wed, 25 Feb 2015 23:16:52 +0100 Subject: [PATCH] fp_toradix_n: make sure too small strings are recognized If maxlen is too small then we get FP_OKAY as return value but not everything is part of the string. This patch changes it so that the caller learns about this short comming. While at it, drop the doxygen style comment. It is the only of his kind and does no longer match the code. Signed-off-by: Sebastian Andrzej Siewior --- src/bin/fp_toradix_n.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/bin/fp_toradix_n.c b/src/bin/fp_toradix_n.c index 4c43980..1775017 100644 --- a/src/bin/fp_toradix_n.c +++ b/src/bin/fp_toradix_n.c @@ -9,22 +9,6 @@ */ #include -/** - * a: pointer to fp_int representing the input number - * str: output buffer - * radix: number of character to use for encoding of the number - * maxlen: maximum number of the buffer that can be used - * - * The radix value can be in the range 2 to 64. This function converts number - * a into a string str. This function writes at most size bytes (including the - * terminating null byte to str. It behaves like snprintf(3) in regard to this. - * - * Return: If invalid parameter are detected a negative value is returned. On - * success the function returns the number of bytes that would be written if - * the function had enough space. Thus a return value of maxlen or more means - * that the function was not able store all characters and the output is - * incomplete. - */ int fp_toradix_n(fp_int *a, char *str, int radix, int maxlen) { int digs; @@ -77,6 +61,8 @@ int fp_toradix_n(fp_int *a, char *str, int radix, int maxlen) /* append a NULL so the string is properly terminated */ *str = '\0'; + if (maxlen < 1) + return FP_VAL; return FP_OKAY; }