Esempio n. 1
0
void expr_to_str_test(Node *node)
{
	Query_stmt *ptree=(Query_stmt *)node;
	expr_to_str_for_selectlist(ptree->select_list);
	expr_to_str_for_groupby(ptree->groupby_list);
	expr_to_str_for_orderby(ptree->orderby_list);
	if(ptree->having_list!=NULL)
	expr_to_str(((Having_list*)ptree->having_list)->next,0);
	if(ptree->where_list!=NULL)
	expr_to_str(((Where_list*)ptree->where_list)->next,0);
}
Esempio n. 2
0
static void match_dma_func(const char *fn, struct expression *expr, void *param)
{
	struct expression *arg;
	struct symbol *sym;
	char *name;

	arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
	arg = strip_expr(arg);
	if (!arg)
		return;
	if (arg->type == EXPR_PREOP && arg->op == '&') {
		if (arg->unop->type != EXPR_SYMBOL)
			return;
		name = expr_to_str(arg);
		sm_msg("error: doing dma on the stack (%s)", name);
		free_string(name);
		return;
	}
	if (arg->type != EXPR_SYMBOL)
		return;
	sym = get_type(arg);
	if (!sym || sym->type != SYM_ARRAY)
		return;
	name = expr_to_var(arg);
	sm_msg("error: doing dma on the stack (%s)", name);
	free_string(name);
}
Esempio n. 3
0
void expr_to_str_for_orderby(Node* node)
{
	if(node==NULL)
		return;
	Orderby_list * gblist=(Orderby_list *)node;
	for(Node *p=(Node *)(gblist->next);p!=NULL;)
	{
		Groupby_expr *obexpr=(Groupby_expr *)p;
		expr_to_str(obexpr->args,0);
		p=obexpr->next;
	}
}
Esempio n. 4
0
void expr_to_str_for_groupby(Node* node)
{
	if(node==NULL)
		return;
	Groupby_list *gplist=(Groupby_list *)node;
	for(Node *p=(Node *)(gplist->next);p!=NULL;)
	{
		Groupby_expr *gbexpr=(Groupby_expr *)p;
		expr_to_str(gbexpr->args,0);
		p=gbexpr->next;
	}
}
Esempio n. 5
0
void expr_to_str_for_selectlist(Node *node)
{
	if(node==NULL)
		return;
	for(Node *p=node;p!=NULL;)
	{
		Select_list *selectlist=(Select_list *)p;
		Select_expr *sexpr=(Select_expr *)selectlist->args;
		expr_to_str(sexpr->colname,0);
		p=selectlist->next;
	}
}
Esempio n. 6
0
static void match_strcpy(const char *fn, struct expression *expr, void *unused)
{
	struct expression *dest;
	struct expression *data;
	char *dest_name = NULL;
	char *data_name = NULL;
	int dest_size;
	int data_size;

	dest = get_argument_from_call_expr(expr->args, 0);
	data = get_argument_from_call_expr(expr->args, 1);
	dest_size = get_array_size_bytes(dest);
	if (!dest_size)
		return;

	data_size = get_size_from_strlen(data);
	if (!data_size)
		data_size = get_array_size_bytes(data);

	/* If the size of both arrays is known and the destination
	 * buffer is larger than the source buffer, we're okay.
	 */
	if (data_size && dest_size >= data_size)
		return;

	dest_name = expr_to_str(dest);
	data_name = expr_to_str(data);

	if (data_size)
		sm_msg("error: %s() '%s' too large for '%s' (%d vs %d)",
			fn, data_name, dest_name, data_size, dest_size);
	else if (option_spammy)
		sm_msg("warn: %s() '%s' of unknown size might be too large for '%s'",
			fn, data_name, dest_name);

	free_string(dest_name);
	free_string(data_name);
}
Esempio n. 7
0
static struct smatch_state *alloc_my_state(struct expression *expr)
{
	struct smatch_state *state;
	char *name;

	state = __alloc_smatch_state(0);
	expr = strip_expr(expr);
	name = expr_to_str(expr);
	if (!name)
		name = alloc_string("");
	state->name = alloc_sname(name);
	free_string(name);
	state->data = expr;
	return state;
}
Esempio n. 8
0
static void match_condition(struct expression *expr)
{
	struct symbol *type;
	char *str;

	if (expr->type != EXPR_DEREF)
		return;
	type = get_type(expr);
	if (!type || type->type != SYM_ARRAY)
		return;
	if (get_macro_name(expr->pos))
		return;

	str = expr_to_str(expr);
	sm_msg("warn: this array is probably non-NULL. '%s'", str);
	free_string(str);
}
Esempio n. 9
0
string expr_to_str(Node * node,int level)
{
	string str="";
	int thislevel=0;
	switch(node->type)
	{
		case t_expr_func:
		{
			Expr_func * funcnode=(Expr_func *)node;
			if(strcmp(funcnode->funname,"CASE3")==0)
			{
				str="case ";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+" end";
			}
			else if(strcmp(funcnode->funname,"CASE4")==0)
			{
				str="case ";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+" else ";
				str=str+expr_to_str(funcnode->parameter2,0);
				str=str+" end";
			}
			else if(strcmp(funcnode->funname,"WHEN1")==0)
			{
				str="when ";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+" then ";
				str=str+expr_to_str(funcnode->parameter2,0);
			}
			else if(strcmp(funcnode->funname,"WHEN2")==0)
			{
				str="when ";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+" then ";
				str=str+expr_to_str(funcnode->parameter2,0);
			}
			else if(strcmp(funcnode->funname,"FSUBSTRING0")==0)
			{
				str="substr(";
				str=str+expr_to_str(funcnode->args,0);
				str=str+",";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FSUBSTRING1")==0)
			{
				str="substr(";
				str=str+expr_to_str(funcnode->args,0);
				str=str+",";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+",";
				str=str+expr_to_str(funcnode->parameter2,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FTRIM0")==0)
			{
				str="trim(";
				str=str+"both ";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+"from ";
				str=str+expr_to_str(funcnode->parameter2,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FTRIM1")==0)
			{
				str="trim(";
				str=str+"trailing ";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+"from ";
				str=str+expr_to_str(funcnode->parameter2,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FTRIM2")==0)
			{
				str="trim(";
				str=str+"leading ";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+"from ";
				str=str+expr_to_str(funcnode->parameter2,0);
				str=str+")";

			}
			else if(strcmp(funcnode->funname,"FTRIM3")==0)
			{
				str="trim(";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FUPPER")==0)
			{
				str="upper(";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FCAST")==0)
			{
				str="case(";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+" as ";
				str=str+expr_to_str(funcnode->parameter2,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FCOALESCE")==0)
			{

			}
			else if(strcmp(funcnode->funname,"FCOUNTALL")==0)
			{
				str="count(*)";
			}
			else if(strcmp(funcnode->funname,"FCOUNT")==0)
			{
				str="count(";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FSUM")==0)
			{
				str="sum(";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FAVG")==0)
			{
				str="avg(";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FMIN")==0)
			{
				str="min(";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FMAX")==0)
			{
				str="max(";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+")";
			}
			else if(strcmp(funcnode->funname,"FDATE_ADD")==0)
			{
				Expr_func *datefunc=(Expr_func *)funcnode->parameter1;
				str="date_add(";
				str=str+expr_to_str(funcnode->args,0);
				str=str+",interval ";
				str=str+expr_to_str(datefunc->args,0);
				if(strcmp(datefunc->funname,"INTERVAL_DAY")==0)
				{
					str=str+" day)";
				}
				else if(strcmp(datefunc->funname,"INTERVAL_WEEK")==0)
				{
					str=str+" week)";
				}
				else if(strcmp(datefunc->funname,"INTERVAL_MONTH")==0)
				{
					str=str+" month)";
				}
				else if(strcmp(datefunc->funname,"INTERVAL_YEAR")==0)
				{
					str=str+" year)";
				}
				else if(strcmp(datefunc->funname,"INTERVAL_QUARTER")==0)
				{
					str=str+" quarter)";
				}

			}
			else if(strcmp(funcnode->funname,"FDATE_SUB")==0)
			{
				Expr_func *datefunc=(Expr_func *)funcnode->parameter1;
				str="date_sub(";
				str=str+expr_to_str(funcnode->args,0);
				str=str+",interval ";
				str=str+expr_to_str(datefunc->args,0);
				if(strcmp(datefunc->funname,"INTERVAL_DAY")==0)
				{
					str=str+" day)";
				}
				else if(strcmp(datefunc->funname,"INTERVAL_WEEK")==0)
				{
					str=str+" week)";
				}
				else if(strcmp(datefunc->funname,"INTERVAL_MONTH")==0)
				{
					str=str+" month)";
				}
				else if(strcmp(datefunc->funname,"INTERVAL_YEAR")==0)
				{
					str=str+" year)";
				}
				else if(strcmp(datefunc->funname,"INTERVAL_QUARTER")==0)
				{
					str=str+" quarter)";
				}
			}
			else if(strcmp(funcnode->funname,"BA")==0)
			{
				str=expr_to_str(funcnode->args,0);
				str=str+" between ";
				str=str+expr_to_str(funcnode->parameter1,0);
				str=str+" and ";
				str=str+expr_to_str(funcnode->parameter2,0);
			}
			else
			{
				SQLParse_elog("expr_to_str doesn't exist this function !!!");
			}
			funcnode->str=(char *)malloc(str.size()+1);
			strcpy(funcnode->str,str.c_str());
		}break;
		case t_expr_cal:
		{
			Expr_cal * calnode=(Expr_cal *)node;
			thislevel=getlevel(calnode);
			if(strcmp(calnode->sign,"--")==0)
			{
				str="-";
			}
			else if(strcmp(calnode->sign,"++")==0)
			{
				str="+";
			}
			else if(strcmp(calnode->sign,"!")==0)
			{
				str="!";
			}
			else if(strcmp(calnode->sign,"NOT")==0)
			{
				str="not";
			}
			else
			{
				str=expr_to_str(calnode->lnext,thislevel);
				if(strcmp(calnode->sign,"CMP")==0)
				{
					switch(calnode->cmp)
					{
						case 1://"<"
						{
							str=str+"<";
						}break;
						case 2://">"
						{
							str=str+">";
						}break;
						case 3://"<>"
						{
							str=str+"!=";
						}break;
						case 4://"="
						{
							str=str+"=";
						}break;
						case 5://"<="
						{
							str=str+"<=";
						}break;
						case 6://">="
						{
							str=str+">=";
						}break;
						default:
						{
						}
					}
				}
				else if(strcmp(calnode->sign,"ANDOP")==0)
				{
					str=str+"and";
				}
				else if(strcmp(calnode->sign,"OR")==0)
				{
					str=str+"or";
				}
				else
				{
					str=str+calnode->sign;
				}
			}

	    	str=str+expr_to_str(calnode->rnext,thislevel);
			calnode->str=(char *)malloc(str.size()+1);
			strcpy(calnode->str,str.c_str());
			if(thislevel<level)
			{
				str="("+str+")";
			}
//			memcpy(calnode->str,str.c_str(),str.size());
		}break;
		case t_name:
		case t_name_name:
		{
			Columns *col=(Columns *)node;
			if(col->parameter1==NULL)
			{
				str=str+string(col->parameter2);
			}
			else
			{
				str=str+string(col->parameter2);
			}
		}break;
		case t_stringval:
		{
			Expr * exprval=(Expr *)node;
			str=str+string(exprval->data);
		}break;
		case t_intnum:
		{
			Expr * exprval=(Expr *)node;
			str=str+string(exprval->data);
		}break;
		case t_approxnum:
		{
			Expr * exprval=(Expr *)node;
			str=str+string(exprval->data);
		}break;
		case t_bool:
		{

		}break;
		default:
		{

		}
	}
	return str;
}