- char session_name[NAME_MAX];
-
- ret = snprintf(session_name, NAME_MAX, "session=%s\n", name);
+ char *attr = "session=";
+ /* Max name len accepted plus attribute's len and the NULL byte. */
+ char session_name[NAME_MAX + strlen(attr) + 1];
+
+ /*
+ * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
+ * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
+ */
+ ret = snprintf(session_name, sizeof(session_name), "%s%s\n", attr, name);