aboutsummaryrefslogtreecommitdiff
path: root/src/memset_s.c
diff options
context:
space:
mode:
authoraxtloss <axtlos@getcryst.al>2024-07-11 02:56:56 +0200
committeraxtloss <axtlos@getcryst.al>2024-07-11 02:56:56 +0200
commit3e85fcb0270c9224ab95dac02c737e6676974c8e (patch)
tree2ec96323ef26f9ec1fee8ac6a0b718700e909eda /src/memset_s.c
parente8f6d6c71b45062cc7ec4dcadcecba44af39a15d (diff)
downloadextlib-3e85fcb0270c9224ab95dac02c737e6676974c8e.tar.gz
extlib-3e85fcb0270c9224ab95dac02c737e6676974c8e.tar.bz2
Implement memset_s and improve free_secure
Diffstat (limited to '')
-rw-r--r--src/memset_s.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/memset_s.c b/src/memset_s.c
new file mode 100644
index 0000000..deda5d7
--- /dev/null
+++ b/src/memset_s.c
@@ -0,0 +1,48 @@
+/* memset_s.c
+ *
+ * Copyright 2024 axtlos <axtlos@disroot.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGPL-3.0-only
+ */
+
+
+#define __STDC_WANT_LIB_EXT1__ 1
+#include "extlib.h"
+#include <errno.h>
+
+
+errno_t
+memset_s(void *s, rsize_t smax, int c, rsize_t n)
+{
+ volatile unsigned char *dest = (unsigned char *) s;
+ errno_t ret = EINVAL;
+ rsize_t limit = n < smax ? n : smax;
+
+ if (!s)
+ throw_constraint_handler_s("memset_s: s = NULL", ret);
+ else if (n > RSIZE_MAX)
+ throw_constraint_handler_s("memset_s: n > RSIZE_MAX", ret);
+ else if (smax > RSIZE_MAX)
+ throw_constraint_handler_s("memset_s: smax > RSIZE_MAX", ret);
+ else if (n > smax)
+ throw_constraint_handler_s("memset_s: n > smax", ret);
+ else {
+ while (limit > 0)
+ dest[--limit] = (unsigned char)c;
+ ret = 0;
+ }
+ return ret;
+}
+