diff mbox

libnet/tftp: Allow loading to address 0

Message ID 1498650385-17478-1-git-send-email-thuth@redhat.com
State Accepted
Headers show

Commit Message

Thomas Huth June 28, 2017, 11:46 a.m. UTC
It's theoretically possible to load a file to address 0, too, so
we should use a different marker than NULL to check for a valid
buffer.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 lib/libnet/tftp.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Alexey Kardashevskiy June 30, 2017, 6:06 a.m. UTC | #1
On 28/06/17 21:46, Thomas Huth wrote:
> It's theoretically possible to load a file to address 0, too, so
> we should use a different marker than NULL to check for a valid
> buffer.

Thanks, applied.

Out of curiosity - what brought this up? :)



> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  lib/libnet/tftp.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/lib/libnet/tftp.c b/lib/libnet/tftp.c
> index d0c2f13..ab63578 100644
> --- a/lib/libnet/tftp.c
> +++ b/lib/libnet/tftp.c
> @@ -26,6 +26,7 @@
>  
>  #define MAX_BLOCKSIZE 1428
>  #define BUFFER_LEN 256
> +#define INVALID_BUFFER ((void *)-1L)
>  
>  #define ENOTFOUND 1
>  #define EACCESS   2
> @@ -45,7 +46,7 @@
>  
>  /* Local variables */
>  static unsigned char packet[BUFFER_LEN];
> -static unsigned char  *buffer = NULL;
> +static unsigned char  *buffer = INVALID_BUFFER;
>  static unsigned short block;
>  static unsigned short blocksize;
>  static char blocksize_str[6];    /* Blocksize string for read request */
> @@ -337,7 +338,7 @@ int32_t handle_tftp(int fd, uint8_t *pkt, int32_t packetsize)
>  	struct tftphdr *tftp;
>  
>  	/* buffer is only set if we are handling TFTP */
> -	if (buffer == NULL )
> +	if (buffer == INVALID_BUFFER)
>  		return 0;
>  
>  #ifndef __DEBUG__
> @@ -536,7 +537,7 @@ int tftp(filename_ip_t * _fn_ip, unsigned char *_buffer, int _len,
>  	printf("  Receiving data:  ");
>  	print_progress(-1, 0);
>  
> -	// Setting buffer to a non-zero address enabled handling of received TFTP packets.
> +	/* Set buffer to a valid address, enables handling of received packets */
>  	buffer = _buffer;
>  
>  	set_timer(TICKS_SEC);
> @@ -579,8 +580,8 @@ int tftp(filename_ip_t * _fn_ip, unsigned char *_buffer, int _len,
>  		}
>  	}
>  
> -	// Setting buffer to NULL disables handling of received TFTP packets.
> -	buffer = NULL;
> +	/* Setting buffer invalid to disable handling of received packets */
> +	buffer = INVALID_BUFFER;
>  
>  	if (tftp_errno)
>  		return tftp_errno;
>
Thomas Huth June 30, 2017, 6:24 a.m. UTC | #2
On 30.06.2017 08:06, Alexey Kardashevskiy wrote:
> On 28/06/17 21:46, Thomas Huth wrote:
>> It's theoretically possible to load a file to address 0, too, so
>> we should use a different marker than NULL to check for a valid
>> buffer.
> 
> Thanks, applied.

Thanks a lot!

> Out of curiosity - what brought this up? :)

I'm currently trying to re-use the libnet for network booting a s390x
guest, and kernels get loaded to address 0 there:

 https://lists.gnu.org/archive/html/qemu-devel/2017-06/msg05911.html

 Thomas
diff mbox

Patch

diff --git a/lib/libnet/tftp.c b/lib/libnet/tftp.c
index d0c2f13..ab63578 100644
--- a/lib/libnet/tftp.c
+++ b/lib/libnet/tftp.c
@@ -26,6 +26,7 @@ 
 
 #define MAX_BLOCKSIZE 1428
 #define BUFFER_LEN 256
+#define INVALID_BUFFER ((void *)-1L)
 
 #define ENOTFOUND 1
 #define EACCESS   2
@@ -45,7 +46,7 @@ 
 
 /* Local variables */
 static unsigned char packet[BUFFER_LEN];
-static unsigned char  *buffer = NULL;
+static unsigned char  *buffer = INVALID_BUFFER;
 static unsigned short block;
 static unsigned short blocksize;
 static char blocksize_str[6];    /* Blocksize string for read request */
@@ -337,7 +338,7 @@  int32_t handle_tftp(int fd, uint8_t *pkt, int32_t packetsize)
 	struct tftphdr *tftp;
 
 	/* buffer is only set if we are handling TFTP */
-	if (buffer == NULL )
+	if (buffer == INVALID_BUFFER)
 		return 0;
 
 #ifndef __DEBUG__
@@ -536,7 +537,7 @@  int tftp(filename_ip_t * _fn_ip, unsigned char *_buffer, int _len,
 	printf("  Receiving data:  ");
 	print_progress(-1, 0);
 
-	// Setting buffer to a non-zero address enabled handling of received TFTP packets.
+	/* Set buffer to a valid address, enables handling of received packets */
 	buffer = _buffer;
 
 	set_timer(TICKS_SEC);
@@ -579,8 +580,8 @@  int tftp(filename_ip_t * _fn_ip, unsigned char *_buffer, int _len,
 		}
 	}
 
-	// Setting buffer to NULL disables handling of received TFTP packets.
-	buffer = NULL;
+	/* Setting buffer invalid to disable handling of received packets */
+	buffer = INVALID_BUFFER;
 
 	if (tftp_errno)
 		return tftp_errno;