aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'FICS/utils.c')
-rw-r--r--FICS/utils.c123
1 files changed, 91 insertions, 32 deletions
diff --git a/FICS/utils.c b/FICS/utils.c
index ee7b676..0e1461a 100644
--- a/FICS/utils.c
+++ b/FICS/utils.c
@@ -36,12 +36,22 @@
ignored fgets() retvals.
Markus Uhlin 24/12/02 Fixed a possible array overrun
in truncate_file().
+ Markus Uhlin 25/03/09 truncate_file:
+ fixed null ptr dereference.
+ Markus Uhlin 25/04/06 Fixed Clang Tidy warnings.
+ Markus Uhlin 25/07/21 Replaced non-reentrant functions
+ with their corresponding thread
+ safe version.
+ Markus Uhlin 25/07/28 truncate_file: restricted file
+ permissions upon creation.
*/
#include "stdinclude.h"
#include "common.h"
#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
#include "config.h"
#include "network.h"
@@ -273,10 +283,10 @@ pcommand(int p, char *comstr, ...)
return retval;
}
-PUBLIC int
+PUBLIC void
pprintf(int p, const char *format, ...)
{
- char tmp[10 * MAX_LINE_SIZE];
+ char tmp[10 * MAX_LINE_SIZE] = { '\0' };
int retval;
va_list ap;
@@ -284,8 +294,8 @@ pprintf(int p, const char *format, ...)
retval = vsnprintf(tmp, sizeof tmp, format, ap);
va_end(ap);
+ UNUSED_VAR(retval);
net_send_string(parray[p].socket, tmp, 1);
- return retval;
}
PRIVATE void
@@ -391,7 +401,7 @@ pprintf_noformat(int p, char *format, ...)
}
PUBLIC int
-psend_raw_file(int p, char *dir, char *file)
+psend_raw_file(int p, const char *dir, const char *file)
{
FILE *fp;
char fname[MAX_FILENAME_SIZE] = { '\0' };
@@ -406,9 +416,18 @@ psend_raw_file(int p, char *dir, char *file)
if ((fp = fopen(fname, "r")) == NULL)
return -1;
- while ((num = fread(tmp, sizeof(char), MAX_LINE_SIZE - 1, fp)) > 0) {
- tmp[num] = '\0';
- net_send_string(parray[p].socket, tmp, 1);
+ while (!feof(fp) && !ferror(fp)) {
+ if ((num = fread(tmp, sizeof(char), MAX_LINE_SIZE - 1,
+ fp)) > 0) {
+ tmp[num] = '\0';
+ net_send_string(parray[p].socket, tmp, 1);
+ }
+ }
+
+ if (ferror(fp)) {
+ warnx("%s: %s: the error indicator is set", __func__, fname);
+ fclose(fp);
+ return -1;
}
fclose(fp);
@@ -416,7 +435,7 @@ psend_raw_file(int p, char *dir, char *file)
}
PUBLIC int
-psend_file(int p, char *dir, char *file)
+psend_file(int p, const char *dir, const char *file)
{
FILE *fp;
char fname[MAX_FILENAME_SIZE] = { '\0' };
@@ -436,12 +455,19 @@ psend_file(int p, char *dir, char *file)
if ((fp = fopen(fname, "r")) == NULL)
return -1;
- while (!feof(fp) && --lcount > 0) {
- if (fgets(tmp, sizeof tmp, fp) != NULL && !feof(fp))
- net_send_string(parray[p].socket, tmp, 1);
+ while (--lcount > 0) {
+ if (fgets(tmp, sizeof tmp, fp) == NULL)
+ break;
+ net_send_string(parray[p].socket, tmp, 1);
}
if (!feof(fp)) {
+ if (ferror(fp)) {
+ warnx("%s: %s: the error indicator is set", __func__,
+ fname);
+ fclose(fp);
+ return -1;
+ }
parray[p].last_file = xstrdup(fname);
parray[p].last_file_byte = ftell(fp);
pprintf(p, "Type [next] to see next page.\n");
@@ -497,18 +523,30 @@ pmore_file(int p)
if ((fp = fopen(parray[p].last_file, "r")) == NULL) {
pprintf(p, "File not found!\n");
return -1;
+ } else if (fseek(fp, parray[p].last_file_byte, SEEK_SET) == -1) {
+ pprintf(p, "Unable to set the file position indicator.\n");
+ fclose(fp);
+ return -1;
}
- fseek(fp, parray[p].last_file_byte, SEEK_SET);
-
- while (!feof(fp) && --lcount > 0) {
- if (fgets(tmp, sizeof tmp, fp) != NULL && !feof(fp))
- net_send_string(parray[p].socket, tmp, 1);
+ while (--lcount > 0) {
+ if (fgets(tmp, sizeof tmp, fp) == NULL)
+ break;
+ net_send_string(parray[p].socket, tmp, 1);
}
if (!feof(fp)) {
- parray[p].last_file_byte = ftell(fp);
- pprintf(p, "Type [next] to see next page.\n");
+ if (ferror(fp)) {
+ warnx("%s: %s: the error indicator is set", __func__,
+ parray[p].last_file);
+ fclose(fp);
+ return -1;
+ } else if ((parray[p].last_file_byte = ftell(fp)) == -1) {
+ warn("%s: %s: ftell", __func__, parray[p].last_file);
+ fclose(fp);
+ return -1;
+ } else
+ pprintf(p, "Type [next] to see next page.\n");
} else {
rfree(parray[p].last_file);
parray[p].last_file = NULL;
@@ -723,19 +761,31 @@ fix_time(char *old_time)
}
PUBLIC char *
-strltime(time_t *clock)
+strltime(time_t *p_clock)
{
- struct tm *stm = localtime(clock);
+ struct tm stm = {0};
- return strtime(stm);
+ errno = 0;
+
+ if (localtime_r(p_clock, &stm) == NULL) {
+ warn("%s: localtime_r", __func__);
+ memset(&stm, 0, sizeof stm);
+ }
+ return strtime(&stm);
}
PUBLIC char *
-strgtime(time_t *clock)
+strgtime(time_t *p_clock)
{
- struct tm *stm = gmtime(clock);
+ struct tm stm = {0};
- return strtime(stm);
+ errno = 0;
+
+ if (gmtime_r(p_clock, &stm) == NULL) {
+ warn("%s: gmtime_r", __func__);
+ memset(&stm, 0, sizeof stm);
+ }
+ return strtime(&stm);
}
/*
@@ -762,7 +812,7 @@ tenth_secs(void)
* 2024-11-23 maxxe: changed the return type to 'time_t'
*/
PUBLIC time_t
-untenths(unsigned int tenths)
+untenths(uint64_t tenths)
{
return (tenths / 10 + 331939277 + 0xffffffff / 10 + 1);
}
@@ -807,7 +857,19 @@ truncate_file(char *file, int lines)
fclose(fp);
if (trunc) {
- fp = fopen(file, "w");
+ int fd;
+
+ errno = 0;
+ fd = open(file, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR);
+
+ if (fd < 0) {
+ warn("%s: open", __func__);
+ return 1;
+ } else if ((fp = fdopen(fd, "w")) == NULL) {
+ warn("%s: fdopen", __func__);
+ close(fd);
+ return 1;
+ }
for (i = 0; i < lines; i++) {
fputs(tBuf[bptr], fp);
@@ -955,10 +1017,11 @@ ratstrii(int rat, int reg)
* Fill 't_buffer' with anything matching "want*" in file tree
*/
PRIVATE void
-t_sft(char *want, struct t_tree *t)
+t_sft(const char *want, struct t_tree *t)
{
if (t) {
- int cmp = strncmp(want, t->name, strlen(want));
+ const char *v_want = (want ? want : "");
+ int cmp = strncmp(v_want, t->name, strlen(v_want));
if (cmp <= 0) // If 'want' <= this one, look left
t_sft(want, t->left);
@@ -1041,7 +1104,6 @@ PUBLIC int
search_directory(char *dir, char *filter, char **buffer, int buffersize)
{
int cmp;
- static char nullify = '\0';
static struct t_dirs *ramdirs = NULL;
struct stat statbuf;
struct t_dirs** i;
@@ -1050,9 +1112,6 @@ search_directory(char *dir, char *filter, char **buffer, int buffersize)
t_buffersize = buffersize;
if (!stat(dir, &statbuf)) {
- if (filter == NULL) // NULL becomes pointer to null string
- filter = &nullify;
-
i = &ramdirs;
while (*i) { // Find dir in dir tree