Need still try to fill buffer as full as possible if the buffer space
is not enough, commonly, the caller can bear of it (e.g. print warning
and still continue).

Signed-off-by: Chen Gang <gang.c...@asianux.com>
---
 mm/mempolicy.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 27022ca..c81b64f 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -2800,6 +2800,8 @@ out:
  * Convert a mempolicy into a string.
  * Returns the number of characters in buffer (if positive)
  * or an error (negative)
+ * If the buffer space is not enough, it will return -ENOSPC,
+ * and try to fill the buffer as full as possible.
  */
 int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
 {
@@ -2842,11 +2844,10 @@ int mpol_to_str(char *buffer, int maxlen, struct 
mempolicy *pol)
                return -EINVAL;
        }
 
+       strlcpy(p, policy_modes[mode], maxlen);
        l = strlen(policy_modes[mode]);
        if (buffer + maxlen < p + l + 1)
                return -ENOSPC;
-
-       strcpy(p, policy_modes[mode]);
        p += l;
 
        if (flags & MPOL_MODE_FLAGS) {
@@ -2857,10 +2858,15 @@ int mpol_to_str(char *buffer, int maxlen, struct 
mempolicy *pol)
                /*
                 * Currently, the only defined flags are mutually exclusive
                 */
-               if (flags & MPOL_F_STATIC_NODES)
+               if (flags & MPOL_F_STATIC_NODES) {
                        p += snprintf(p, buffer + maxlen - p, "static");
-               else if (flags & MPOL_F_RELATIVE_NODES)
+                       if (buffer + maxlen <= p)
+                               return -ENOSPC;
+               } else if (flags & MPOL_F_RELATIVE_NODES) {
                        p += snprintf(p, buffer + maxlen - p, "relative");
+                       if (buffer + maxlen <= p)
+                               return -ENOSPC;
+               }
        }
 
        if (!nodes_empty(nodes)) {
-- 
1.7.7.6
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to