From 0e27ce454a5f378bf186725f8fc2c9465005cbd6 Mon Sep 17 00:00:00 2001 From: Adrian Bowyer Date: Tue, 29 Oct 2013 15:48:09 +0000 Subject: [PATCH] Fixed the file listing bug. Either in the RepRap code, or somewhere in the SD file code, the #define for _USE_LFN is defined as fals for RepRap, true in the library file ff.c. The result is that ff.c tried to write long file names into a structure created in RepRap that was too short for them. Result - buffer overflow misery. For the moment I've commented out the bit of code in ff.c that handles long names (look for the string ******), but this needs to be tracked down and dealt with properly. --- Lwip/lwip/src/core/tcp.c | 6 +- Lwip/lwip/src/include/lwip/def.h | 3 +- Lwip/lwip/src/sam/netif/ethernetif.c | 85 ++++++++++++++-------------- SD_HSMCI/utility/ff.c | 51 +++++++++-------- 4 files changed, 78 insertions(+), 67 deletions(-) diff --git a/Lwip/lwip/src/core/tcp.c b/Lwip/lwip/src/core/tcp.c index 8e99b4e..d4c9a18 100644 --- a/Lwip/lwip/src/core/tcp.c +++ b/Lwip/lwip/src/core/tcp.c @@ -2,7 +2,7 @@ * @file * Transmission Control Protocol for IP * - * This file contains common functions for the TCP implementation, such as functinos + * This file contains common functions for the TCP implementation, such as functinos * for manipulating the data structures and the TCP timer functions. TCP functions * related to input and output is found in tcp_in.c and tcp_out.c respectively. * @@ -42,8 +42,12 @@ #include "lwip/src/include/lwip/opt.h" +#pragma message("Testing LWIP_TCP") + #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */ +#pragma message("LWIP_TCP is true") + #include "lwip/src/include/lwip/def.h" #include "lwip/src/include/lwip/mem.h" #include "lwip/src/include/lwip/memp.h" diff --git a/Lwip/lwip/src/include/lwip/def.h b/Lwip/lwip/src/include/lwip/def.h index 126de45..1cbd16f 100644 --- a/Lwip/lwip/src/include/lwip/def.h +++ b/Lwip/lwip/src/include/lwip/def.h @@ -44,7 +44,8 @@ extern "C" { #define LWIP_MIN(x , y) (((x) < (y)) ? (x) : (y)) #ifndef NULL -#define NULL ((void *)0) +#define NULL 0 +//#define NULL ((void *)0) #endif /** Get the absolute difference between 2 u32_t values (correcting overflows) diff --git a/Lwip/lwip/src/sam/netif/ethernetif.c b/Lwip/lwip/src/sam/netif/ethernetif.c index b2d44bc..ae85700 100644 --- a/Lwip/lwip/src/sam/netif/ethernetif.c +++ b/Lwip/lwip/src/sam/netif/ethernetif.c @@ -300,6 +300,7 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p) return ERR_OK; } + /** * \brief Should allocate a pbuf and transfer the bytes of the incoming * packet from the interface into the pbuf. @@ -377,58 +378,58 @@ static struct pbuf *low_level_input(struct netif *netif) * ethernetif. */ -void ethernetif_input(void * pvParameters) -{ - struct netif *netif = (struct netif *)pvParameters; - struct pbuf *p; - - /* move received packet into a new pbuf */ - p = low_level_input( netif ); - if( p == NULL ) - return; - - if( ERR_OK != netif->input( p, netif ) ) - { - pbuf_free(p); - p = NULL; - } -} - - //void ethernetif_input(void * pvParameters) //{ -// // struct netif *netif = (struct netif *)pvParameters; // struct pbuf *p; // -//#ifdef FREERTOS_USED -// for( ;; ) { -// do { -//#endif -// /* move received packet into a new pbuf */ -// p = low_level_input( netif ); -// if( p == NULL ) { -//#ifdef FREERTOS_USED -// /* No packet could be read. Wait a for an interrupt to tell us -// there is more data available. */ -// vTaskDelay(100); -// } -// }while( p == NULL ); -//#else -// return; -// } -//#endif +// /* move received packet into a new pbuf */ +// p = low_level_input( netif ); +// if( p == NULL ) +// return; // -// if( ERR_OK != netif->input( p, netif ) ) { -// pbuf_free(p); -// p = NULL; -// } -//#ifdef FREERTOS_USED +// if( ERR_OK != netif->input( p, netif ) ) +// { +// pbuf_free(p); +// p = NULL; // } -//#endif //} +void ethernetif_input(void * pvParameters) +{ + + struct netif *netif = (struct netif *)pvParameters; + struct pbuf *p; + +#ifdef FREERTOS_USED + for( ;; ) { + do { +#endif + /* move received packet into a new pbuf */ + p = low_level_input( netif ); + if( p == NULL ) { +#ifdef FREERTOS_USED + /* No packet could be read. Wait a for an interrupt to tell us + there is more data available. */ + vTaskDelay(100); + } + }while( p == NULL ); +#else + return; + } +#endif + + if( ERR_OK != netif->input( p, netif ) ) { + pbuf_free(p); + p = NULL; + } +#ifdef FREERTOS_USED + } +#endif +} + + /** * \brief Should be called at the beginning of the program to set up the diff --git a/SD_HSMCI/utility/ff.c b/SD_HSMCI/utility/ff.c index 3f0edb3..dd43496 100644 --- a/SD_HSMCI/utility/ff.c +++ b/SD_HSMCI/utility/ff.c @@ -1870,7 +1870,7 @@ void get_fileinfo ( /* No return code */ #endif *p++ = c; } - if (dir[8] != ' ') { /* Copy name extension */ + if (dir[8] != ' ') { /* Copy name extension */ *p++ = '.'; for (i = 8; i < 11; i++) { c = dir[i]; @@ -1885,6 +1885,7 @@ void get_fileinfo ( /* No return code */ *p++ = c; } } + fno->fattrib = dir[DIR_Attr]; /* Attribute */ fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */ fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */ @@ -1892,28 +1893,33 @@ void get_fileinfo ( /* No return code */ } *p = 0; /* Terminate SFN str by a \0 */ -#if _USE_LFN - if (fno->lfname && fno->lfsize) { - TCHAR *tp = fno->lfname; - WCHAR w, *lfn; +//************ Long file names commented out by AB; seems to be #defined true here, but not +// Wherever the RepRap code is picking up the #define; hence this writes beyond the memory +// allocated to fno + +//#if _USE_LFN +// if (fno->lfname && fno->lfsize) { +// TCHAR *tp = fno->lfname; +// WCHAR w, *lfn; +// +// i = 0; +// if (dj->sect && dj->lfn_idx != 0xFFFF) {/* Get LFN if available */ +// lfn = dj->lfn; +// while ((w = *lfn++) != 0) { /* Get an LFN char */ +//#if !_LFN_UNICODE +// w = ff_convert(w, 0); /* Unicode -> OEM conversion */ +// if (!w) { i = 0; break; } /* Could not convert, no LFN */ +// if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */ +// tp[i++] = (TCHAR)(w >> 8); +//#endif +// if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overflow, no LFN */ +// tp[i++] = (TCHAR)w; +// } +// } +// tp[i] = 0; /* Terminate the LFN str by a \0 */ +// } +//#endif - i = 0; - if (dj->sect && dj->lfn_idx != 0xFFFF) {/* Get LFN if available */ - lfn = dj->lfn; - while ((w = *lfn++) != 0) { /* Get an LFN char */ -#if !_LFN_UNICODE - w = ff_convert(w, 0); /* Unicode -> OEM conversion */ - if (!w) { i = 0; break; } /* Could not convert, no LFN */ - if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */ - tp[i++] = (TCHAR)(w >> 8); -#endif - if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overflow, no LFN */ - tp[i++] = (TCHAR)w; - } - } - tp[i] = 0; /* Terminate the LFN str by a \0 */ - } -#endif } #endif /* _FS_MINIMIZE <= 1 */ @@ -3016,7 +3022,6 @@ FRESULT f_readdir ( FRESULT res; DEF_NAMEBUF; - res = validate(dj->fs, dj->id); /* Check validity of the object */ if (res == FR_OK) { if (!fno) {