From 30f7ba0f26ff6c0b2360c49e32944e57ac12fb8a Mon Sep 17 00:00:00 2001 From: hfiref0x Date: Sun, 4 Feb 2018 15:44:05 +0700 Subject: [PATCH] POSIX version update For changelog see Source/Other/changelog.txt --- LightFTP.sha256 | 14 ++-- Source/Other/Release/makefile | 1 - Source/Other/Release/subdir.mk | 11 ++- Source/Other/cfgparse.c | 7 +- Source/Other/changelog.txt | 4 + Source/Other/ftpserv.c | 134 ++++++++++++++++----------------- Source/Other/main.c | 7 +- Source/Other/x_malloc.c | 26 +++++++ Source/Other/x_malloc.h | 13 ++++ 9 files changed, 131 insertions(+), 86 deletions(-) create mode 100644 Source/Other/x_malloc.c create mode 100644 Source/Other/x_malloc.h diff --git a/LightFTP.sha256 b/LightFTP.sha256 index aeafa6e..44de00e 100644 --- a/LightFTP.sha256 +++ b/LightFTP.sha256 @@ -2,17 +2,19 @@ b8a49791ec3e1e7b9d077854dd782942a8215fb17904bfb13093ed077492b49d *Compiled\Other cdb10fa83d7980d2d27e7fb36682659ed481368ae7d923a3b6578765f93d922f *Compiled\Windows\fftp.cfg 3445a33d0d62250b34b44b0764b55c4b3b3a3a1c14e5570c29a6a1f93a8353a7 *Compiled\Windows\fftp32.exe a1aed722776497288304cbc1f51fe3c4f7e3287dc85ecf6185acf88a903fbf8c *Compiled\Windows\fftp64.exe -8c53e41199221e5ef271303db9fa79510cbdcbcbcc207b0157d95b2e654a7adc *Source\Other\cfgparse.c +89aaa11ef9b717765c454e202a150010cc8a30dd8be18f7499068017c022db6c *Source\Other\cfgparse.c 427b7db493e4684ef233f5067170e3d2bfcdf9bef040c5492178cfd1bd1cdc0b *Source\Other\cfgparse.h -a8d515a33fc02c8a25f570e966f9a295099d1b670174341520c3bd277f256b4c *Source\Other\changelog.txt +dcce504f3d12ebf692834f672cecfa8fc28bf5902387ea7d19558a001e8ebaf9 *Source\Other\changelog.txt b8a49791ec3e1e7b9d077854dd782942a8215fb17904bfb13093ed077492b49d *Source\Other\fftp.conf -4cb9eaff8038a22a7f80fa0afa821a80904182de0b2caf98b37e110597ba027a *Source\Other\ftpserv.c +58de3a26bbf00eba739780efbc4e8c7743c169882436106f816010759ec6fd79 *Source\Other\ftpserv.c 7402b915128d13f1bad74b1e7ce52d5cc6892368f0cc0c5b23c91169d080da02 *Source\Other\ftpserv.h -e748ef17e664b9b5e7f18d8c4d4f8e9baaf5e5c5c1a15050ba06382e339547a0 *Source\Other\main.c -e36a2cbee884ea37ee3078eed42f5456874bd61ac5f6f0ea92fbb2592a1e54fd *Source\Other\Release\makefile +e8a711027a2e141134bf126cce28eb4dfeee06d55dc37ca68cf3660429405baa *Source\Other\main.c +ebb995befa74b64aadd88038a62c05184b9d03981f34028924fe6d0d38bd0d89 *Source\Other\x_malloc.c +79876657549a772e6e48fdacc83f18352ad9a23529cab840e169fdfe62d6a01e *Source\Other\x_malloc.h +65c2c882ead0e9266325893b457b8506db2a081eb3936939dedfce52c22c31ea *Source\Other\Release\makefile e7567c8ea6e252acf06be3a1e0c1a2769bdbfa0e29c8fc5c920b047203db084a *Source\Other\Release\objects.mk 45bf7584af2acb3c6c806bd48b0774c661e081efdaff00d735554cde1ed76fbb *Source\Other\Release\sources.mk -4119e84853faeb48ffe14a57ac92352d87451cba3ea20927fea76976eca06d73 *Source\Other\Release\subdir.mk +a481ceeafb98fce03ca27694e6b5d13e858806385f1dd29cd9cb0ef459f00abf *Source\Other\Release\subdir.mk 71f29f165b2554ec548bc56a113f38c0c993c2b34265acfd6d4c64a05f3db1ef *Source\Windows\fftp.sln 8708f082b6072e0ab2c55984c2bd252f80049f2d4dceabf6fd8cf4815331cf7f *Source\Windows\fftp\debug.conf 6315323d32acdc93160ed785f82631e686645370ae62082199f5c8000fc98e08 *Source\Windows\fftp\fftp.vcxproj diff --git a/Source/Other/Release/makefile b/Source/Other/Release/makefile index 5108401..2241106 100644 --- a/Source/Other/Release/makefile +++ b/Source/Other/Release/makefile @@ -38,6 +38,5 @@ clean: -@echo ' ' .PHONY: all clean dependents -.SECONDARY: -include ../makefile.targets diff --git a/Source/Other/Release/subdir.mk b/Source/Other/Release/subdir.mk index 98e413f..c5c0a83 100644 --- a/Source/Other/Release/subdir.mk +++ b/Source/Other/Release/subdir.mk @@ -6,24 +6,27 @@ C_SRCS += \ ../cfgparse.c \ ../ftpserv.c \ -../main.c +../main.c \ +../x_malloc.c OBJS += \ ./cfgparse.o \ ./ftpserv.o \ -./main.o +./main.o \ +./x_malloc.o C_DEPS += \ ./cfgparse.d \ ./ftpserv.d \ -./main.d +./main.d \ +./x_malloc.d # Each subdirectory must supply rules for building sources it contributes %.o: ../%.c @echo 'Building file: $<' @echo 'Invoking: GCC C Compiler' - gcc -std=c99 -O3 -Wall -Wextra -c -fmessage-length=0 -Wno-unused-parameter -Wno-unused-result -fno-ident -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<" + gcc -std=c90 -O3 -Wall -Wextra -c -fmessage-length=0 -Wno-unused-parameter -Wno-unused-result -fno-ident -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<" @echo 'Finished building: $<' @echo ' ' diff --git a/Source/Other/cfgparse.c b/Source/Other/cfgparse.c index f2da3bf..5c07ce2 100644 --- a/Source/Other/cfgparse.c +++ b/Source/Other/cfgparse.c @@ -3,7 +3,7 @@ * * Created on: Aug 20, 2016 * -* Modified on: Feb 02, 2018 +* Modified on: Feb 03, 2018 * * Author: lightftp */ @@ -15,6 +15,7 @@ #include #include #include +#include "x_malloc.h" int ParseConfig( const char *pcfg, @@ -164,9 +165,7 @@ char *InitConfig(char *cfg_filename) fsz = lseek(f_config, 0L, SEEK_END) + 1; lseek(f_config, 0L, SEEK_SET); - buffer = malloc(fsz); - if (buffer == NULL) - break; + buffer = x_malloc(fsz); fsz = read(f_config, buffer, fsz); buffer[fsz] = 0; diff --git a/Source/Other/changelog.txt b/Source/Other/changelog.txt index ce47372..e73e9d4 100644 --- a/Source/Other/changelog.txt +++ b/Source/Other/changelog.txt @@ -2,6 +2,10 @@ - Removed * Fixed/Changed +Feb 3, 2018 ++ more work around malloc calls ++ some redesign in ftpserv.c format_path routine + Feb 2, 2018 + error checking added for several malloc calls diff --git a/Source/Other/ftpserv.c b/Source/Other/ftpserv.c index de7ab48..a79b8b1 100644 --- a/Source/Other/ftpserv.c +++ b/Source/Other/ftpserv.c @@ -3,7 +3,7 @@ * * Created on: Aug 20, 2016 * -* Modified on: Feb 02, 2018 +* Modified on: Feb 03, 2018 * * Author: lightftp */ @@ -29,6 +29,7 @@ #include "ftpserv.h" #include "cfgparse.h" +#include "x_malloc.h" __inline char lowcase_a(char c); @@ -56,16 +57,16 @@ unsigned int g_newid = 0; char lowcase_a(char c) { - if ((c >= 'A') && (c <= 'Z')) - return c + ('a'-'A'); - else - return c; + if ((c >= 'A') && (c <= 'Z')) + return c + ('a'-'A'); + else + return c; } size_t ultostr(unsigned long x, char *s) { unsigned long t=x; - size_t i, r=1; + size_t i, r=1; while ( t >= 10 ) { t /= 10; @@ -125,8 +126,8 @@ int strncmpi(const char *s1, const char *s2, size_t cchars) do { c1 = lowcase_a(*s1); c2 = lowcase_a(*s2); - s1++; - s2++; + ++s1; + ++s2; cchars--; } while ( (c1 != 0) && (c1 == c2) && (cchars>0) ); @@ -149,8 +150,8 @@ int strcmpi(const char *s1, const char *s2) do { c1 = lowcase_a(*s1); c2 = lowcase_a(*s2); - s1++; - s2++; + ++s1; + ++s2; } while ( (c1 != 0) && (c1 == c2) ); return (int)(c1 - c2); @@ -168,7 +169,7 @@ int delete_last_slash(char *s) return 0; while (s[1] != 0) - s++; + ++s; if (*s == '/') { *s = 0; @@ -184,7 +185,7 @@ int add_last_slash(char *s) return 0; while (s[1] != 0) - s++; + ++s; if (*s == '/') return 0; @@ -207,14 +208,14 @@ char *filepath(char *s) */ if (*s == '/') { - s++; - p++; + ++s; + ++p; } while (*s != 0) { if (*s == '/') p = s; - s++; + ++s; } *p = 0; @@ -226,55 +227,68 @@ char *filepath(char *s) * This function filters the path out of ".." members * not allowing user to escape the home directory */ -void format_path(char *p_in, char *p_out) +void format_path(char *input_path, char *filtered_path) { - char *p_in0, *p_out0; - size_t len; + char *p0, *pnext, *fp0; + size_t sl; - if (p_in[0] == '/') + if (*input_path == '/') { - p_out[0] = '/'; - ++p_in; - ++p_out; + ++input_path; + *filtered_path = '/'; + ++filtered_path; } - p_in0 = p_in; - p_out0 = p_out; - *p_out = 0; + p0 = input_path; + pnext = input_path; + fp0 = filtered_path; + *fp0 = 0; - while (p_in != NULL) { - while ((*p_in != '/') && (*p_in != 0)) - ++p_in; + while (1) + { + while ((*pnext != '/') && (*pnext != 0)) + ++pnext; - len = 1 + p_in - p_in0; + sl = pnext - p0; - if ((strncmp(p_in0, "../", 3) == 0) || (strncmp(p_in0, "..\0", 3) == 0)) + while (sl > 0) { - delete_last_slash(p_out0); - p_out = filepath(p_out0); - if (p_out != p_out0) + if (sl == 1) + if (*p0 == '.') + break; + + if (sl == 2) + if ((p0[0] == '.') && (p0[1] == '.')) + { + delete_last_slash(filtered_path); + fp0 = filepath(filtered_path); + if (fp0 != filtered_path) + { + *fp0 = '/'; + ++fp0; + *fp0 = 0; + } + break; + } + + strncpy(fp0, p0, sl); + fp0 += sl; + if (*pnext != 0) { - p_out[0] = '/'; - p_out[1] = 0; - ++p_out; - } - } - else - { - if ((strncmp(p_in0, "./", 2) != 0) && (strncmp(p_in0, "/", 1) != 0)) { - strncpy(p_out, p_in0, len); - p_out += len; - *p_out = 0; + *fp0 = '/'; + ++fp0; } - } + *fp0 = 0; - p_in0 += len; + break; + } - if (*p_in == 0) + if (*pnext == 0) break; - ++p_in; - }; + ++pnext; + p0 = pnext; + } } char *finalpath(char *root_dir, char *current_dir, char *params, char *result_path) @@ -289,11 +303,7 @@ char *finalpath(char *root_dir, char *current_dir, char *params, char *result_pa if (total_len >= PATH_MAX*4) return NULL; - tmp = malloc(PATH_MAX*4); - if (tmp == NULL) - abort(); - - memset(tmp, 0, PATH_MAX*4); + tmp = x_malloc(PATH_MAX*4); strcpy(result_path, root_dir); add_last_slash(result_path); @@ -1499,9 +1509,7 @@ int ftpRNTO(PFTPCONTEXT context, const char *params) if ( params == NULL ) return sendstring(context->ControlSocket, error501); - _text = malloc(PATH_MAX * 4); - if (_text == NULL) - return sendstring(context->ControlSocket, error550_m); + _text = x_malloc(PATH_MAX * 4); if (finalpath( context->RootDir, @@ -1747,9 +1755,7 @@ void *ftp_client_thread(SOCKET *s) memset(&ctx, 0, sizeof(ctx)); ctx.Access = FTP_ACCESS_NOT_LOGGED_IN; ctx.ControlSocket = *s; - ctx.GPBuffer = malloc(PATH_MAX*4); - if (ctx.GPBuffer == NULL) - return NULL; + ctx.GPBuffer = x_malloc(PATH_MAX*4); memset(&laddr, 0, sizeof(laddr)); asz = sizeof(laddr); @@ -1872,13 +1878,7 @@ void *ftpmain(void *p) rv = 1; setsockopt(ftpsocket, SOL_SOCKET, SO_REUSEADDR, &rv, sizeof(rv)); - scb = (SOCKET *)malloc(sizeof(SOCKET)*g_cfg.MaxUsers); - if ( scb == NULL ) { - printf("\r\n not enough free memory\r\n"); - close(ftpsocket); - return 0; - } - + scb = (SOCKET *)x_malloc(sizeof(SOCKET)*g_cfg.MaxUsers); for (i = 0; i +#include +#include + +void * x_malloc(size_t size) +{ + void *result; + + result = malloc(size); + if (result == NULL) + { + printf("\r\nOut of memory\r\n"); + abort(); + } + + memset(result, 0, size); + + return result; +} diff --git a/Source/Other/x_malloc.h b/Source/Other/x_malloc.h new file mode 100644 index 0000000..3deea74 --- /dev/null +++ b/Source/Other/x_malloc.h @@ -0,0 +1,13 @@ +/* + * x_malloc.h + * + * Created on: Feb 3, 2018 + * Author: lightftp + */ + +#ifndef X_MALLOC_H_ +#define X_MALLOC_H_ + +void * x_malloc(size_t size); + +#endif /* X_MALLOC_H_ */