summaryrefslogtreecommitdiff
path: root/src/sss_client/common.c
diff options
context:
space:
mode:
authorStephen Gallagher <sgallagh@redhat.com>2010-08-25 10:44:14 -0400
committerStephen Gallagher <sgallagh@redhat.com>2010-10-13 09:49:37 -0400
commit3ba74ad02d3a20d50c068faa02797fafba105508 (patch)
tree2ef51784c47b23ed2e80097f1c9b5ac4c320b07d /src/sss_client/common.c
parent93109c5f1d85c028ce5cf6e31e2249ca90a7f746 (diff)
downloadsssd-3ba74ad02d3a20d50c068faa02797fafba105508.tar.gz
sssd-3ba74ad02d3a20d50c068faa02797fafba105508.tar.bz2
sssd-3ba74ad02d3a20d50c068faa02797fafba105508.zip
Add utility function sss_strnlen()
This is useful for guaranteeing the size of an input buffer.
Diffstat (limited to 'src/sss_client/common.c')
-rw-r--r--src/sss_client/common.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/sss_client/common.c b/src/sss_client/common.c
index 6b79c783..3bfa8928 100644
--- a/src/sss_client/common.c
+++ b/src/sss_client/common.c
@@ -761,3 +761,32 @@ const char *ssscli_err2string(int err)
return _("Unexpected error while looking for an error description");
}
+
+/* Return strlen(str) or maxlen, whichever is shorter
+ * Returns EINVAL if str is NULL, EFBIG if str is longer than maxlen
+ * _len will return the result
+ *
+ * This function is useful for preventing buffer overflow attacks.
+ */
+errno_t sss_strnlen(const char *str, size_t maxlen, size_t *len)
+{
+ if (!str) {
+ return EINVAL;
+ }
+
+#if defined __USE_GNU
+ *len = strnlen(str, maxlen);
+#else
+ *len = 0;
+ while (*len < maxlen) {
+ if (str[*len] == '\0') break;
+ len++;
+ }
+#endif
+
+ if (*len == maxlen && str[*len] != '\0') {
+ return EFBIG;
+ }
+
+ return 0;
+}