cvs commit: ALFS/nALFS/src comm.c nalfs.c

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Mon Feb 23 20:21:14 PST 2004


kpfleming    04/02/23 21:21:14

  Modified:    nALFS/src comm.c nalfs.c
  Log:
  correct bugs in and cleanup control message sending/receiving code
  
  Revision  Changes    Path
  1.11      +48 -49    ALFS/nALFS/src/comm.c
  
  Index: comm.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/comm.c,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- comm.c	11 Feb 2004 05:59:55 -0000	1.10
  +++ comm.c	24 Feb 2004 04:21:14 -0000	1.11
  @@ -296,59 +296,54 @@
    */
   static char *do_read_ctrl_message(int s)
   {
  -	unsigned total_read, size_len;
  -
  +	ssize_t num_read;
  +	size_t to_read = 9;
  +	char size_buf[10];
  +	char *pos;
  +	char *message = NULL;
   
   	if (! is_read_available(s, 0)) {
   		return NULL;
   	}
   
  -	total_read = 0;
  -	size_len = number_len(number_len(CTRL_UNKNOWN)+1+MAX_CTRL_MSG_LEN)+1;
  -
  -	while (total_read < size_len) {
  -		char size_buf[size_len + 1];
  -		char *pos = size_buf + total_read;
  +	pos = &size_buf[0];
   
  -		if (read(s, pos, 1) != 1) {
  +	while (to_read > 0) {
  +		num_read = read(s, pos, to_read);
  +		if (num_read < 0)
   			break;
  -		}
  -
  -		pos[1] = '\0';
  -
  -		++total_read;
  +		to_read -= num_read;
  +		pos += num_read;
  +	}
   
  -		if (*pos == '|') {
  -			unsigned long i, size;
  -			char *message = NULL;
  +	if (to_read > 0)
  +		return NULL;
   
  -			size = strtoul(size_buf, (char **)NULL, 10);
  +	/* overwrite the last character read, which will be '|' */
  +	*--pos = '\0';
   
  -			if (size == ULONG_MAX || size == 0) {
  -				Nprint_err("Can find out msg size from \"%s\".",
  -					size_buf);
  -				break;
  -			}
  +	to_read = strtoul(size_buf, (char **) NULL, 10);
   
  -			message = xmalloc(size + 1);
  -
  -			if ((i = socket_read_max(s, message, size)) > 0) {
  -				message[i] = '\0';
  -				return message;
  -			}
  +	if (to_read == ULONG_MAX || to_read == 0) {
  +		Nprint_err("Can't read msg size from \"%s\".", size_buf);
  +		return NULL;
  +	}
   
  -			xfree(message);
  +	message = xmalloc(to_read + 1);
   
  -			break;
  -		}
  +	if ((num_read = socket_read_max(s, message, to_read)) > 0) {
  +		message[num_read] = '\0';
  +		return message;
   	}
   
  +	xfree(message);
  +
   	return NULL;
   }
   
   ctrl_msg_s *comm_read_ctrl_message(socket_e s)
   {
  -	char *tmp, *body;
  +	char *content, *body;
   	ctrl_msg_s *message;
   
   
  @@ -356,18 +351,15 @@
   		return NULL;
   	}
   
  -	if ((tmp = strchr(body, '|')) == NULL) {
  -		xfree(body);
  -		return NULL;
  -	}
  -	*tmp++ = '\0';
  +	content = body + 5;
  +	*(body + 4) = '\0';
   
   	message = xmalloc(sizeof *message);
   
   	message->type = atoi(body);
   
  -	if (tmp && *tmp) {
  -		message->content = xstrdup(tmp);
  +	if (*content) {
  +		message->content = xstrdup(content);
   	} else {
   		message->content = NULL;
   	}
  @@ -396,20 +388,27 @@
   		va_start(ap, format);
   		result = vsnprintf(buf, buf_size, format, ap);
   		va_end(ap);
  -		if ((result < buf_size) && (result > -1))
  -			break;
  -		if (result > -1)
  -			buf_size += 1;
  -		else
  -			buf_size *= 2;
  -		if ((buf = xrealloc(buf, buf_size)) == NULL) {
  -			Nprint_err("xrealloc() failed: %s", strerror(errno));
  +		if (result >= buf_size) {
  +			buf_size = result + 1;
  +			if ((buf = xrealloc(buf, buf_size)) == NULL) {
  +				Nprint_err("xrealloc() failed: %s", strerror(errno));
  +				xfree(buf);
  +				return -1;
  +			}
  +		} else if (result < 0) {
  +			Nprint_err("vsnprintf() failed: %s", strerror(errno));
  +			xfree(buf);
   			return -1;
  +		} else {
  +			break;
   		}
   	}
   
   	full_message = xmalloc(result + 15);
  -	sprintf(full_message, "%08d|%04d|%s", result + 6, t, buf);
  +	/* message size does _NOT_ include trailing null, as it is not sent by write() */
  +	/* message size does _NOT_ include size prefix, or '|' separator */
  +	sprintf(full_message, "%08d|%04d|%s", result + 5, t, buf);
  +	xfree(buf);
   	ret = write(comm_get_socket(s), full_message, strlen(full_message));
   	xfree(full_message);
   	if (ret == -1) {
  
  
  
  1.42      +4 -3      ALFS/nALFS/src/nalfs.c
  
  Index: nalfs.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/nalfs.c,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- nalfs.c	13 Feb 2004 02:46:53 -0000	1.41
  +++ nalfs.c	24 Feb 2004 04:21:14 -0000	1.42
  @@ -869,7 +869,9 @@
   
   
   	if ((message = comm_read_ctrl_message(FRONTEND_CTRL_SOCK)) != NULL) {
  -		char *content = comm_msg_content(message);
  +		char *content;
  +
  +		content = comm_msg_content(message);
   
   		switch (comm_msg_type(message)) {
   			case CTRL_ELEMENT_STARTED:
  @@ -900,7 +902,6 @@
   				send_element_status(content);
   				break;
   
  -
   			default:
   				Nprint_warn("Unknown control message: %s",
   					content);
  @@ -4586,7 +4587,7 @@
   
   	wstandend(windows.status->name);
   
  -	vwprintw(windows.status->name, (char *) format, ap);
  +	vwprintw(windows.status->name, format, ap);
   
   	if (*opt_log_status_window && fp) {
   		vfprintf(fp, format, ap2);
  
  
  



More information about the alfs-log mailing list