source: rtems/c/src/lib/libc/read.c @ c0438add

4.104.114.84.95
Last change on this file since c0438add was 2d733c42, checked in by Joel Sherrill <joel.sherrill@…>, on 01/20/99 at 15:48:22

More general fix based on bug report and patch from Ian Lance Taylor
<ian@…> to fix this problem:

There is a small bug in rtems_close in c/src/lib/libc/libio.c. It
does not check whether the file descriptor it is passed is open. This
can cause it to make a null dereference if it is passed a file
descriptor which is in the valid range but which was not opened, or
which was already closed.

  • Property mode set to 100644
File size: 1.7 KB
Line 
1/*
2 *  read() - POSIX 1003.1b 6.4.1 - Read From a File
3 *
4 *  COPYRIGHT (c) 1989-1998.
5 *  On-Line Applications Research Corporation (OAR).
6 *  Copyright assigned to U.S. Government, 1994.
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.OARcorp.com/rtems/license.html.
11 *
12 *  $Id$
13 */
14
15#include "libio_.h"
16
17ssize_t read(
18  int         fd,
19  void       *buffer,
20  size_t      count
21)
22{
23  int             rc;  /* XXX change to a size_t when prototype is fixed */
24  rtems_libio_t *iop;
25
26  rtems_libio_check_fd( fd );
27  iop = rtems_libio_iop( fd );
28  rtems_libio_check_is_open(iop);
29  rtems_libio_check_buffer( buffer );
30  rtems_libio_check_count( count );
31  rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ );
32
33  /*
34   *  If this file descriptor is mapped to an external set of handlers,
35   *  then pass the request on to them.
36   */
37
38  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK ) {
39    rtems_libio_read_t fp;
40
41    fp = rtems_libio_handlers[
42           (iop->flags >> LIBIO_FLAGS_HANDLER_SHIFT) - 1].read;
43    if ( fp == NULL )
44      set_errno_and_return_minus_one( EBADF );
45
46    return (*fp)( fd, buffer, count );
47  }
48
49  /*
50   *  Now process the read().
51   */
52
53  if ( !iop->handlers->read )
54    set_errno_and_return_minus_one( ENOTSUP );
55
56  rc = (*iop->handlers->read)( iop, buffer, count );
57
58  if ( rc > 0 )
59    iop->offset += rc;
60
61  return rc;
62}
63
64/*
65 *  _read_r
66 *
67 *  This is the Newlib dependent reentrant version of read().
68 */
69
70#if defined(RTEMS_NEWLIB)
71
72#include <reent.h>
73
74_ssize_t _read_r(
75  struct _reent *ptr,
76  int            fd,
77  void          *buf,
78  size_t         nbytes
79)
80{
81  return read( fd, buf, nbytes );
82}
83#endif
Note: See TracBrowser for help on using the repository browser.