Commit b9b2b802 authored by Morten Welinder's avatar Morten Welinder Committed by Morten Welinder

Don't access memory that isn't ours to access. Constify.

2000-04-27  Morten Welinder  <terra@diku.dk>

	* src/plugin.c (plugin_load_plugins_in_dir): Don't access memory
 	that isn't ours to access.  Constify.
	(plugin_data_last_modified): Constify.
	(plugin_data_get_title): Constify.
	(plugin_data_get_descr): Constify.
	(plugin_data_get_size): Constify.
	(plugin_data_get_user_data): Constify.


2000-04-25  Morten Welinder  <terra@diku.dk>

	* numtheory.c (gnumeric_ithprime): Up limit to 1000000.
	(ithprime): Rework memory handling; up limit; optimize to not even
 	try even numbers.
	(function_names): rename "nt_ithprime" back to "ithprime" as no
	conflict is likely.
parent e8492f00
2000-04-27 Morten Welinder <terra@diku.dk>
* src/plugin.c (plugin_load_plugins_in_dir): Don't access memory
that isn't ours to access. Constify.
(plugin_data_last_modified): Constify.
(plugin_data_get_title): Constify.
(plugin_data_get_descr): Constify.
(plugin_data_get_size): Constify.
(plugin_data_get_user_data): Constify.
2000-04-27 Karl EICHWALDER <ke@suse.de>
* doc/translating.sgml: Typo.
......
2000-04-27 Morten Welinder <terra@diku.dk>
* src/plugin.c (plugin_load_plugins_in_dir): Don't access memory
that isn't ours to access. Constify.
(plugin_data_last_modified): Constify.
(plugin_data_get_title): Constify.
(plugin_data_get_descr): Constify.
(plugin_data_get_size): Constify.
(plugin_data_get_user_data): Constify.
2000-04-27 Karl EICHWALDER <ke@suse.de>
* doc/translating.sgml: Typo.
......
2000-04-27 Morten Welinder <terra@diku.dk>
* src/plugin.c (plugin_load_plugins_in_dir): Don't access memory
that isn't ours to access. Constify.
(plugin_data_last_modified): Constify.
(plugin_data_get_title): Constify.
(plugin_data_get_descr): Constify.
(plugin_data_get_size): Constify.
(plugin_data_get_user_data): Constify.
2000-04-27 Karl EICHWALDER <ke@suse.de>
* doc/translating.sgml: Typo.
......
2000-04-27 Morten Welinder <terra@diku.dk>
* src/plugin.c (plugin_load_plugins_in_dir): Don't access memory
that isn't ours to access. Constify.
(plugin_data_last_modified): Constify.
(plugin_data_get_title): Constify.
(plugin_data_get_descr): Constify.
(plugin_data_get_size): Constify.
(plugin_data_get_user_data): Constify.
2000-04-27 Karl EICHWALDER <ke@suse.de>
* doc/translating.sgml: Typo.
......
2000-04-27 Morten Welinder <terra@diku.dk>
* src/plugin.c (plugin_load_plugins_in_dir): Don't access memory
that isn't ours to access. Constify.
(plugin_data_last_modified): Constify.
(plugin_data_get_title): Constify.
(plugin_data_get_descr): Constify.
(plugin_data_get_size): Constify.
(plugin_data_get_user_data): Constify.
2000-04-27 Karl EICHWALDER <ke@suse.de>
* doc/translating.sgml: Typo.
......
2000-04-27 Morten Welinder <terra@diku.dk>
* src/plugin.c (plugin_load_plugins_in_dir): Don't access memory
that isn't ours to access. Constify.
(plugin_data_last_modified): Constify.
(plugin_data_get_title): Constify.
(plugin_data_get_descr): Constify.
(plugin_data_get_size): Constify.
(plugin_data_get_user_data): Constify.
2000-04-27 Karl EICHWALDER <ke@suse.de>
* doc/translating.sgml: Typo.
......
2000-04-27 Morten Welinder <terra@diku.dk>
* src/plugin.c (plugin_load_plugins_in_dir): Don't access memory
that isn't ours to access. Constify.
(plugin_data_last_modified): Constify.
(plugin_data_get_title): Constify.
(plugin_data_get_descr): Constify.
(plugin_data_get_size): Constify.
(plugin_data_get_user_data): Constify.
2000-04-27 Karl EICHWALDER <ke@suse.de>
* doc/translating.sgml: Typo.
......
2000-04-25 Morten Welinder <terra@diku.dk>
* numtheory.c (gnumeric_ithprime): Up limit to 1000000.
(ithprime): Rework memory handling; up limit; optimize to not even
try even numbers.
(function_names): rename "nt_ithprime" back to "ithprime" as no
conflict is likely.
......@@ -16,24 +16,23 @@
#define OUT_OF_BOUNDS "#LIMIT!"
/* ------------------------------------------------------------------------- */
/* number-theoretic functions by */
#define PTABLE_CHUNK 64
static int
intpow (int p, int v)
{
int temp;
if (v == 0) return 1;
if (v == 1) return p;
temp = intpow (p, v/2);
return temp * temp * (v % 2 ? p : 1);
temp = intpow (p, v / 2);
temp *= temp;
return (v % 2) ? temp * p : temp;
}
static int *table = NULL;
#define PTABLE_CHUNK 64
#define ITHPRIME_LIMIT 1000000
static int *prime_table = NULL;
static int
ithprime (int i, int *res)
......@@ -41,50 +40,44 @@ ithprime (int i, int *res)
static int computed = 0;
static int allocated = 0;
int candidate, index;
if (i < 1 || i > 32768)
if (i < 1 || i > ITHPRIME_LIMIT)
return 1;
if (i > computed) {
int candidate;
if (i <= computed) {
*res = table[i-1];
return 0;
}
if (!allocated) {
allocated = PTABLE_CHUNK;
table = g_malloc (allocated * sizeof(int));
}
if (!computed) {
table[0] = 2;
table[1] = 3;
computed = 2;
}
candidate = table[computed - 1];
while (i > computed) {
candidate++;
for (index = 0;
candidate % table[index] != 0 &&
table[index] * table[index] < candidate;
index++);
if (candidate % table[index] != 0) {
computed++;
if (computed > allocated) {
allocated += PTABLE_CHUNK;
table = g_realloc (table,
allocated * sizeof(int));
if (i > allocated) {
g_assert (PTABLE_CHUNK >= 2);
allocated = MAX (i, allocated + PTABLE_CHUNK);
prime_table = g_renew (int, prime_table, allocated);
if (computed == 0) {
prime_table[computed++] = 2;
prime_table[computed++] = 3;
}
}
table[computed-1] = candidate;
candidate = prime_table[computed - 1];
/*
* Note, that the dandidate is odd since we filled in the first
* two prime numbers.
*/
while (i > computed) {
gboolean prime = TRUE;
int j;
candidate += 2; /* Skip even candidates. */
for (j = 1; prime_table[j] * prime_table[j] <= candidate; j++)
if (candidate % prime_table[j] == 0) {
prime = FALSE;
break;
}
if (prime)
prime_table[computed++] = candidate;
}
}
*res = table[i-1];
*res = prime_table[i - 1];
return 0;
}
......@@ -92,18 +85,19 @@ static void
walk_factorization (int n, void *data,
void (*walk_term) (int p, int v, void *data))
{
int index = 1, p = 1, v;
int index = 1, p = 2, v;
while (n > 1 && p * p <= n) {
ithprime (index, &p);
v = 0;
while (!(n % p )) {
v ++;
while (n % p == 0) {
v++;
n /= p;
}
if (v) {
/* We found a prime factor, p, with arity v. */
walk_term (p, v, data);
}
......@@ -111,29 +105,14 @@ walk_factorization (int n, void *data,
}
if (n > 1) {
/*
* A number, n, with no factors from 2 to sqrt (n) is a
* prime number. The arity is 1.
*/
walk_term (n, 1, data);
}
}
static void
walk_for_phi (int p, int v, void *data)
{
* (int *) data *= intpow (p, v - 1) * (p - 1);
}
static void
walk_for_d (int p, int v, void *data)
{
* (int *) data *= (v + 1);
}
static void
walk_for_sigma (int p, int v, void *data)
{
* (int *) data *=
( v == 1 ? p + 1 : (intpow (p, v + 1) - 1) / (p - 1) );
}
/* ------------------------------------------------------------------------- */
static char *help_phi = {
......@@ -145,9 +124,15 @@ static char *help_phi = {
"This function only takes one argument."
"\n"
"@EXAMPLES=\n"
"@SEEALSO=NT_D, NT_ITHPRIME, NT_SIGMA")
"@SEEALSO=NT_D, ITHPRIME, NT_SIGMA")
};
static void
walk_for_phi (int p, int v, void *data)
{
*((int *)data) *= intpow (p, v - 1) * (p - 1);
}
static Value *
gnumeric_phi (FunctionEvalInfo *ei, Value **args)
{
......@@ -163,6 +148,7 @@ gnumeric_phi (FunctionEvalInfo *ei, Value **args)
return value_new_int (phi);
}
/* ------------------------------------------------------------------------- */
static char *help_d = {
N_("@FUNCTION=NT_D\n"
......@@ -172,9 +158,15 @@ static char *help_d = {
"This function only takes one argument."
"\n"
"@EXAMPLES=\n"
"@SEEALSO=NT_ITHPRIME, NT_PHI, NT_SIGMA")
"@SEEALSO=ITHPRIME, NT_PHI, NT_SIGMA")
};
static void
walk_for_d (int p, int v, void *data)
{
* (int *) data *= (v + 1);
}
static Value *
gnumeric_d (FunctionEvalInfo *ei, Value **args)
{
......@@ -190,6 +182,7 @@ gnumeric_d (FunctionEvalInfo *ei, Value **args)
return value_new_int (d);
}
/* ------------------------------------------------------------------------- */
static char *help_sigma = {
N_("@FUNCTION=NT_SIGMA\n"
......@@ -199,9 +192,18 @@ static char *help_sigma = {
"This function only takes one argument."
"\n"
"@EXAMPLES=\n"
"@SEEALSO=NT_D, NT_ITHPRIME, NT_PHI")
"@SEEALSO=NT_D, ITHPRIME, NT_PHI")
};
static void
walk_for_sigma (int p, int v, void *data)
{
* (int *) data *=
( v == 1 ? p + 1 : (intpow (p, v + 1) - 1) / (p - 1) );
}
/* ------------------------------------------------------------------------- */
static Value *
gnumeric_sigma (FunctionEvalInfo *ei, Value **args)
{
......@@ -218,16 +220,18 @@ gnumeric_sigma (FunctionEvalInfo *ei, Value **args)
}
static char *help_ithprime = {
N_("@FUNCTION=NT_ITHPRIME\n"
"@SYNTAX=NT_ITHPRIME(i)\n"
N_("@FUNCTION=ITHPRIME\n"
"@SYNTAX=ITHPRIME(i)\n"
"@DESCRIPTION="
"The NT_ITHPRIME function returns the @ith prime.\n"
"The ITHPRIME function returns the @ith prime.\n"
"This function only takes one argument."
"\n"
"@EXAMPLES=\n"
"@SEEALSO=NT_D, NT_SIGMA")
};
/* ------------------------------------------------------------------------- */
static Value *
gnumeric_ithprime (FunctionEvalInfo *ei, Value **args)
{
......@@ -236,7 +240,7 @@ gnumeric_ithprime (FunctionEvalInfo *ei, Value **args)
i = value_get_as_int (args [0]);
if (i < 1)
return value_new_error (ei->pos, gnumeric_err_NUM);
else if (i > 32768)
else if (i > ITHPRIME_LIMIT)
return value_new_error (ei->pos, OUT_OF_BOUNDS);
ithprime (i, &p);
......@@ -246,7 +250,7 @@ gnumeric_ithprime (FunctionEvalInfo *ei, Value **args)
/* ------------------------------------------------------------------------- */
static const char *function_names[] = {
"nt_phi", "nt_d", "nt_sigma", "nt_ithprime"
"nt_phi", "nt_d", "nt_sigma", "ithprime"
};
static const int function_count =
......@@ -279,8 +283,8 @@ cleanup_plugin (PluginData *pd)
if (sym) symbol_unref (sym);
}
g_free (table);
table = 0;
g_free (prime_table);
prime_table = 0;
}
#define NUMTHEORY_TITLE _("Number Theory Plugin")
......@@ -296,7 +300,7 @@ init_plugin (CommandContext *context, PluginData *pd)
cat = function_get_category (_("Number Theory"));
function_add_args (cat, "nt_ithprime","f",
function_add_args (cat, "ithprime","f",
"number", &help_ithprime, gnumeric_ithprime);
function_add_args (cat, "nt_phi", "f",
"number", &help_phi, gnumeric_phi);
......
......@@ -24,7 +24,7 @@ PERL_CFLAGS = $(PERL_CCCDLFLAGS) \
#noinst_PROGRAMS = libgnum_perl.so
plugin_PROGRAMS = libgnum_perl.so
libgnum_perl.so_SOURCES = perl.c
libgnum_perl_so_SOURCES = perl.c
libgnum_perl.so: perl.o xsinit.o ext.timestamp
-$(PERL_LD) $(PERL_LDDLFLAGS) -o libgnum_perl.so perl.o xsinit.o $(PERL_LDOPTS)
......
......@@ -179,13 +179,13 @@ void
plugin_unload (CommandContext *context, PluginData *pd)
{
g_return_if_fail (pd != NULL);
if (pd->can_unload && !pd->can_unload (pd)) {
gnumeric_error_plugin_problem (context,
_("Plugin is still in use.\n"));
return;
}
if (pd->cleanup_plugin)
pd->cleanup_plugin (pd);
......@@ -194,18 +194,20 @@ plugin_unload (CommandContext *context, PluginData *pd)
}
static void
plugin_load_plugins_in_dir (CommandContext *context, char *directory)
plugin_load_plugins_in_dir (CommandContext *context, const char *directory)
{
DIR *d;
struct dirent *e;
if ((d = opendir (directory)) == NULL)
return;
while ((e = readdir (d)) != NULL){
if (strncmp (e->d_name + strlen (e->d_name) - 3, ".so", 3) == 0){
int len;
len = strlen (e->d_name);
if (len > 3 && strncmp (e->d_name + len - 3, ".so", 3) == 0){
char *plugin_name;
plugin_name = g_strconcat (directory, e->d_name, NULL);
plugin_load (context, plugin_name);
g_free (plugin_name);
......@@ -218,7 +220,7 @@ static void
load_all_plugins (CommandContext *context)
{
char *plugin_dir;
/* Load the user plugins */
plugin_dir = gnumeric_usr_plugin_dir ();
if (plugin_dir != NULL) {
......@@ -269,19 +271,19 @@ plugin_data_init (PluginData *pd, PluginCanUnloadFn can_unload_fn,
}
const gchar *
plugin_data_get_filename (PluginData *pd)
plugin_data_get_filename (const PluginData *pd)
{
return pd->file_name;
}
const gchar *
plugin_data_get_title (PluginData *pd)
plugin_data_get_title (const PluginData *pd)
{
return pd->title;
}
const gchar *
plugin_data_get_descr (PluginData *pd)
plugin_data_get_descr (const PluginData *pd)
{
return pd->descr;
}
......@@ -304,16 +306,16 @@ plugin_data_set_user_data (PluginData *pd, void *user_data)
* Returns the private data of this plugin
*/
void *
plugin_data_get_user_data (PluginData *pd)
plugin_data_get_user_data (const PluginData *pd)
{
return pd->user_data;
return (void *)pd->user_data;
}
/*
* Returns the size of the plugin in bytes
*/
off_t
plugin_data_get_size (PluginData *pd)
plugin_data_get_size (const PluginData *pd)
{
return pd->size;
}
......@@ -323,7 +325,7 @@ plugin_data_get_size (PluginData *pd)
* In UNIX-like time
*/
time_t
plugin_data_last_modified (PluginData *pd)
plugin_data_last_modified (const PluginData *pd)
{
return pd->modified;
}
......@@ -33,16 +33,16 @@ gboolean plugin_version_mismatch (CommandContext *cmd, PluginData *pd,
char const * const plugin_version);
void *plugin_data_set_user_data (PluginData *pd, void *user_data);
void *plugin_data_get_user_data (PluginData *pd);
void *plugin_data_get_user_data (const PluginData *pd);
gboolean plugin_data_init (PluginData *pd, PluginCanUnloadFn can_unload_fn,
PluginCleanupFn cleanup_fn,
const gchar *title, const gchar *descr);
const gchar *plugin_data_get_filename (PluginData *pd);
const gchar *plugin_data_get_title (PluginData *pd);
const gchar *plugin_data_get_descr (PluginData *pd);
off_t plugin_data_get_size (PluginData *pd);
time_t plugin_data_last_modified(PluginData *pd);
const gchar *plugin_data_get_filename (const PluginData *pd);
const gchar *plugin_data_get_title (const PluginData *pd);
const gchar *plugin_data_get_descr (const PluginData *pd);
off_t plugin_data_get_size (const PluginData *pd);
time_t plugin_data_last_modified(const PluginData *pd);
#endif /* GNUMERIC_PLUGIN_H */
......@@ -2451,7 +2451,7 @@ cb_scroll_wheel_support (GtkWidget *w, GdkEventButton *event, Workbook *wb)
/* This is a stub routine to handle scroll wheel events
* Unfortunately the toplevel window is currently owned by the workbook
* rather than a workbook-view so we can not really scroll things
* unless we scrolled al lthe views at once which is ugly.
* unless we scrolled all the views at once which is ugly.
*/
#if 0
if (event->button == 4)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment