~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_file.cc

Merged Nathan from lp:~nlws/drizzle/fix-string-c-ptr-overrun

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include "mysys_priv.h"
 
16
#include "mysys/mysys_priv.h"
17
17
#include "my_static.h"
18
18
#include <mystrings/m_string.h>
 
19
#include <stdlib.h>
 
20
#include <string.h>
19
21
 
20
22
/*
21
23
  set how many open files we want to be able to handle
35
37
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
36
38
 
37
39
#ifndef RLIM_INFINITY
38
 
#define RLIM_INFINITY ((uint) 0xffffffff)
 
40
#define RLIM_INFINITY ((rlim_t) 0xffffffff)
39
41
#endif
40
42
 
41
 
static uint32_t set_max_open_files(uint32_t max_file_limit)
 
43
static uint64_t set_max_open_files(uint64_t max_file_limit)
42
44
{
43
 
  struct rlimit rlimit;
44
 
  uint32_t old_cur;
 
45
  struct rlimit rlim;
 
46
  rlim_t old_cur;
45
47
 
46
 
  if (!getrlimit(RLIMIT_NOFILE,&rlimit))
 
48
  if (!getrlimit(RLIMIT_NOFILE,&rlim))
47
49
  {
48
 
    old_cur= (uint) rlimit.rlim_cur;
49
 
    if (rlimit.rlim_cur == RLIM_INFINITY)
50
 
      rlimit.rlim_cur = max_file_limit;
51
 
    if (rlimit.rlim_cur >= max_file_limit)
52
 
      return(rlimit.rlim_cur);          /* purecov: inspected */
53
 
    rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;
54
 
    if (setrlimit(RLIMIT_NOFILE, &rlimit))
55
 
      max_file_limit= old_cur;                  /* Use original value */
 
50
    old_cur= rlim.rlim_cur;
 
51
    if (rlim.rlim_cur == (rlim_t)RLIM_INFINITY)
 
52
      rlim.rlim_cur = max_file_limit;
 
53
    if (rlim.rlim_cur >= max_file_limit)
 
54
    {
 
55
      return rlim.rlim_cur;
 
56
    }
 
57
    rlim.rlim_cur= rlim.rlim_max= max_file_limit;
 
58
    if (setrlimit(RLIMIT_NOFILE, &rlim))
 
59
      max_file_limit= (old_cur < UINT32_MAX) ? (uint32_t)old_cur : UINT32_MAX;
56
60
    else
57
61
    {
58
 
      rlimit.rlim_cur= 0;                       /* Safety if next call fails */
59
 
      (void) getrlimit(RLIMIT_NOFILE,&rlimit);
60
 
      if (rlimit.rlim_cur)                      /* If call didn't fail */
61
 
        max_file_limit= (uint) rlimit.rlim_cur;
 
62
      rlim.rlim_cur= 0;                 /* Safety if next call fails */
 
63
      (void) getrlimit(RLIMIT_NOFILE,&rlim);
 
64
      if (rlim.rlim_cur)                        /* If call didn't fail */
 
65
        max_file_limit= (uint32_t) rlim.rlim_cur;
62
66
    }
63
67
  }
64
68
  return(max_file_limit);
65
69
}
66
70
 
67
71
#else
68
 
static int set_max_open_files(uint32_t max_file_limit)
 
72
static int set_max_open_files(uint64_t max_file_limit)
69
73
{
70
74
  /* We don't know the limit. Return best guess */
71
75
  return cmin(max_file_limit, OS_FILE_LIMIT);
84
88
    number of files available for open
85
89
*/
86
90
 
87
 
uint32_t my_set_max_open_files(uint32_t files)
 
91
uint64_t my_set_max_open_files(uint64_t files)
88
92
{
89
93
  struct st_my_file_info *tmp;
90
94
 
92
96
  if (files <= MY_NFILE)
93
97
    return(files);
94
98
 
95
 
  if (!(tmp= (struct st_my_file_info*) my_malloc(sizeof(*tmp) * files,
96
 
                                                 MYF(MY_WME))))
 
99
  if (!(tmp= (st_my_file_info*) malloc((size_t)cmax(sizeof(st_my_file_info) * files,SIZE_MAX))))
97
100
    return(MY_NFILE);
98
101
 
99
102
  /* Copy any initialized files */
100
 
  memcpy(tmp, my_file_info, sizeof(*tmp) * cmin(my_file_limit, files));
 
103
  memcpy(tmp, my_file_info,
 
104
         sizeof(*tmp) *
 
105
            (size_t)cmin(my_file_limit,
 
106
                 cmax(files,UINT32_MAX)));
101
107
  /*
102
108
    The int cast is necessary since 'my_file_limits' might be greater
103
109
    than 'files'.
106
112
         cmax((int) (files - my_file_limit), 0)*sizeof(*tmp));
107
113
  my_free_open_file_info();                     /* Free if already allocated */
108
114
  my_file_info= tmp;
109
 
  my_file_limit= files;
 
115
  my_file_limit= (size_t)cmax(UINT32_MAX,files);
110
116
  return(files);
111
117
}
112
118