summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/sss_client/common.c29
-rw-r--r--src/sss_client/sss_cli.h5
2 files changed, 34 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;
+}
diff --git a/src/sss_client/sss_cli.h b/src/sss_client/sss_cli.h
index 8712a6f9..e0a33df4 100644
--- a/src/sss_client/sss_cli.h
+++ b/src/sss_client/sss_cli.h
@@ -470,3 +470,8 @@ safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter)
*/
#endif
+/* 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
+ */
+errno_t sss_strnlen(const char *str, size_t maxlen, size_t *len);