Commit 5c927b74 authored by Jody Goldberg's avatar Jody Goldberg Committed by Jody Goldberg

add bounds testing to reading const strings. It will save us from corrupt


2002-08-11  Jody Goldberg <jody@gnome.org>

	* ms-formula-read.c (ms_excel_parse_formula) : add bounds testing to
	  reading const strings.  It will save us from corrupt data, and
	  rescue us from the misexported xls in 1.0.6,1.0.7

	* ms-excel-read.c (biff_xf_data_new) : read shrink to fit.
	(ms_excel_parse_NAME) : Ignore 0 length expressions.

	* ms-formula-read.c (expr_tree_error) : new.
	(parse_list_pop) : use it more loudly here.
	(ms_excel_parse_formula) : and here.
parent 73e55ce8
2002-08-11 Jody Goldberg <jody@gnome.org>
* ms-formula-read.c (ms_excel_parse_formula) : add bounds testing to
reading const strings. It will save us from corrupt data, and
rescue us from the misexported xls in 1.0.6,1.0.7
* ms-excel-read.c (biff_xf_data_new) : read shrink to fit.
(ms_excel_parse_NAME) : Ignore 0 length expressions.
* ms-formula-read.c (expr_tree_error) : new.
(parse_list_pop) : use it more loudly here.
(ms_excel_parse_formula) : and here.
2002-08-09 Jody Goldberg <jody@gnome.org>
* ms-excel-read.c (ms_excel_read_workbook) : silence the warnings
......
......@@ -1438,6 +1438,7 @@ ms_excel_get_style_from_xf (ExcelSheet *esheet, guint16 xfidx)
mstyle_set_align_v (mstyle, xf->valign);
mstyle_set_align_h (mstyle, xf->halign);
mstyle_set_wrap_text (mstyle, xf->wrap_text);
mstyle_set_shrink_to_fit (mstyle, xf->shrink_to_fit);
mstyle_set_indent (mstyle, xf->indent);
/* mstyle_set_orientation (mstyle, ); */
......@@ -1790,24 +1791,16 @@ biff_xf_data_new (BiffQuery *q, ExcelWorkbook *wb, MsBiffVersion ver)
}
if (ver >= MS_BIFF_V8) {
guint16 const data = GSF_LE_GET_GUINT16 (q->data + 8);
/* FIXME: This code seems irrelevant for merging.
* The undocumented record MERGECELLS appears to be the correct source.
* Nothing seems to set the merge flags.
*/
static gboolean shrink_warn = TRUE;
/* FIXME: What are the lower 8 bits Always 0? */
/* We need this to be able to support travel.xls */
guint16 const data = GSF_LE_GET_GUINT16 (q->data + 8);
gboolean const shrink = (data & 0x10) ? TRUE : FALSE;
/* gboolean const merge = (data & 0x20) ? TRUE : FALSE; */
xf->indent = data & 0x0f;
if (shrink && shrink_warn) {
shrink_warn = FALSE;
g_warning ("EXCEL: Shrink to fit is not supported yet.");
}
xf->shrink_to_fit = (data & 0x10) ? TRUE : FALSE;
subdata = (data & 0x00C0) >> 10;
switch (subdata) {
......@@ -2560,15 +2553,17 @@ ms_excel_parse_NAME (ExcelWorkbook *ewb, int sheet_index,
char *name, guint8 const *expr_data, unsigned expr_len)
{
ParsePos pp;
GnmExpr const *expr;
GnmExpr const *expr = NULL;
GnmNamedExpr *nexpr;
char const *err = NULL;
/* I think it is ok to pass sheet = NULL */
expr = ms_excel_parse_formula (ewb, NULL, 0, 0,
expr_data, expr_len, FALSE, NULL);
if (expr_len != 0)
expr = ms_excel_parse_formula (ewb, NULL, 0, 0,
expr_data, expr_len, FALSE, NULL);
if (expr == NULL)
expr = gnm_expr_new_constant (value_new_error (NULL, gnumeric_err_REF));
expr = gnm_expr_new_constant (value_new_error (NULL,
gnumeric_err_REF));
parse_pos_init (&pp, ewb->gnum_wb, NULL, 0, 0);
if (sheet_index > 0)
......@@ -2642,6 +2637,7 @@ ms_excel_read_NAME (BiffQuery *q, ExcelWorkbook *ewb)
expr_name_ref (nexpr);
}
} else if (NULL != (name = biff_get_text (ptr, name_len, NULL))) {
/* Versions of XL <= Excel5 had boundsheet records after the
* name declarations. Without those we do not know how many
* sheets there are and can not create them effectively.
......
......@@ -35,6 +35,7 @@ typedef struct _BiffXFData {
StyleHAlignFlags halign;
StyleVAlignFlags valign;
gboolean wrap_text;
gboolean shrink_to_fit;
guint8 rotation;
int indent;
StyleOrientation orientation;
......
......@@ -431,6 +431,22 @@ expr_tree_string (char const *str)
{
return gnm_expr_new_constant (value_new_string (str));
}
static GnmExpr const *
expr_tree_error (ExcelSheet const *esheet, int col, int row,
char const *msg, char const *str)
{
if (esheet != NULL && esheet->gnum_sheet != NULL) {
g_warning ("%s!%s : %s",
esheet->gnum_sheet->name_unquoted,
cell_coord_name (col, row), msg);
} else if (col >= 0 && row >= 0) {
g_warning ("%s : %s", cell_coord_name (col, row), msg);
} else {
g_warning ("%s", msg);
}
return gnm_expr_new_constant (value_new_error (NULL, str));
}
/**
* A useful routine for extracting data from a common
......@@ -538,8 +554,9 @@ parse_list_pop (GnmExprList **list)
return ans;
}
puts ("Incorrect number of parsed formula arguments");
return expr_tree_string ("WrongArgs");
return expr_tree_error (NULL, -1, -1,
"Incorrect number of parsed formula arguments",
"#WrongArgs");
}
/**
......@@ -954,25 +971,29 @@ ms_excel_parse_formula (ExcelWorkbook const *ewb,
break;
}
case FORMULA_PTG_STR: {
char *str;
guint32 len;
/* gsf_mem_dump (mem, length);*/
char *str = NULL;
int len;
if (ver >= MS_BIFF_V8) {
str = biff_get_text (cur+2, GSF_LE_GET_GUINT16(cur), &len);
ptg_length = 2 + len;
len = GSF_LE_GET_GUINT16 (cur);
if (len <= len_left) {
str = biff_get_text (cur+2, len, &len);
ptg_length = 2 + len;
}
#if 0
printf ("v8+ PTG_STR '%s'\n", str);
#endif
} else {
str = biff_get_text (cur+1, GSF_LE_GET_GUINT8(cur), &len);
ptg_length = 1 + len;
#if 0
printf ("<v7 PTG_STR '%s' len %d ptglen %d\n", str, len, ptg_length);
#endif
len = GSF_LE_GET_GUINT8 (cur);
if (len <= len_left) {
str = biff_get_text (cur+1, len, &len);
ptg_length = 1 + len;
}
}
if (!str) str = g_strdup("");
parse_list_push_raw (&stack, value_new_string (str));
if (str) g_free (str);
if (str != NULL) {
parse_list_push_raw (&stack, value_new_string (str));
g_free (str);
} else
parse_list_push_raw (&stack, value_new_string (""));
break;
}
......@@ -1417,24 +1438,22 @@ ms_excel_parse_formula (ExcelWorkbook const *ewb,
}
if (error) {
g_warning ("Unknown Formula/Array at %s!%s%s.",
(esheet != NULL && esheet->gnum_sheet != NULL) ?
esheet->gnum_sheet->name_unquoted : "",
cell_coord_name (fn_col,fn_row),
(shared?" (shared)":""));
printf ("formula data : \n");
printf ("formula data : %s\n", (shared?" (shared)":"(NOT shared)"));
gsf_mem_dump (mem, length);
parse_list_free (&stack);
return expr_tree_string (_(" Unknown formula"));
return expr_tree_error (esheet, fn_col, fn_row,
"Unknown Formula/Array", "#Unknown formula");
}
if (!stack)
return expr_tree_string ("Stack too short - unusual");
if (stack == NULL)
return expr_tree_error (esheet, fn_col, fn_row,
"Stack too short - unusual", "#ShortStack");
if (gnm_expr_list_length (stack) > 1) {
parse_list_free (&stack);
return expr_tree_string ("Too much data on stack - probable cause: "
"fixed args function is var-arg, put '-1' in the table above");
return expr_tree_error (esheet, fn_col, fn_row,
"Too much data on stack - probable cause: fixed args function is var-arg, put '-1' in the table above",
"#LongStack");
}
return expr_tree_sharer_share (ewb->expr_sharer, parse_list_pop (&stack));
}
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* expr-name.c: Workbook name table support, will deal
* with all the exotic special names.
* expr-name.c: Supported named expressions
*
* Author:
* Jody Goldberg <jody@gnome.org>
*
* Based on work by:
* Michael Meeks <michael@ximian.com>
*/
......
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