Index: rlm_sqlcounter.c
===================================================================
RCS file: /source/radiusd/src/modules/rlm_sqlcounter/rlm_sqlcounter.c,v
retrieving revision 1.11.2.3.2.6
diff -u -r1.11.2.3.2.6 rlm_sqlcounter.c
--- rlm_sqlcounter.c	18 Nov 2006 14:45:19 -0000	1.11.2.3.2.6
+++ rlm_sqlcounter.c	25 Jan 2007 09:03:40 -0000
@@ -175,8 +175,8 @@
 		last = data->reset[len - 1];
 		if (!isalpha((int) last))
 			last = 'd';
-/*		num = atoi(data->reset); */
-		DEBUG("rlm_sqlcounter: num=%d, last=%c",num,last);
+		num = atoi(data->reset);
+		DEBUG("rlm_sqlcounter: num=%u, last=%c",num,last);
 	}
 	if (strcmp(data->reset, "hourly") == 0 || last == 'h') {
 		/*
@@ -392,6 +392,7 @@
 
 	check_pairs = check_pairs; /* shut the compiler up */
 	reply_pairs = reply_pairs;
+	request = request;
 
 	/* first, expand %k, %b and %e in query */
 	sqlcounter_expand(querystr, MAX_QUERY_LEN, data->query, instance);
@@ -596,7 +597,7 @@
 {
 	rlm_sqlcounter_t *data = (rlm_sqlcounter_t *) instance;
 	int ret=RLM_MODULE_NOOP;
-	int counter=0;
+	uint32_t counter=0;
 	int res=0;
 	DICT_ATTR *dattr;
 	VALUE_PAIR *key_vp, *check_vp;
@@ -659,13 +660,18 @@
 	/* Finally, xlat resulting SQL query */
 	radius_xlat(querystr, MAX_QUERY_LEN, responsestr, request, sql_escape_func);
 
-	counter = atoi(querystr);
+	counter = strtoul(querystr, NULL, 10);
 
 
 	/*
 	 * Check if check item > counter
 	 */
-	res=check_vp->lvalue - counter;
+	if (check_vp->lvalue > counter) {
+		res = check_vp->lvalue - counter;
+	} else {
+		res = - ((int) (counter - check_vp->lvalue));
+	}
+
 	if (res > 0) {
 		DEBUG2("rlm_sqlcounter: (Check item - counter) is greater than zero");
 		/*
@@ -692,7 +698,7 @@
 		}
 
 		if ((reply_item = pairfind(request->reply->vps, data->reply_attr)) != NULL) {
-			if (reply_item->lvalue > res)
+			if (reply_item->lvalue > (uint32_t) res)
 				reply_item->lvalue = res;
 		} else {
 			if ((reply_item = paircreate(data->reply_attr, PW_TYPE_INTEGER)) == NULL) {
@@ -705,7 +711,7 @@
 
 		ret=RLM_MODULE_OK;
 
-		DEBUG2("rlm_sqlcounter: Authorized user %s, check_item=%d, counter=%d",
+		DEBUG2("rlm_sqlcounter: Authorized user %s, check_item=%d, counter=%u",
 				key_vp->strvalue,check_vp->lvalue,counter);
 		DEBUG2("rlm_sqlcounter: Sent Reply-Item for user %s, Type=%s, value=%d",
 				key_vp->strvalue,data->reply_name,reply_item->lvalue);
@@ -729,7 +735,7 @@
 
 		ret=RLM_MODULE_REJECT;
 
-		DEBUG2("rlm_sqlcounter: Rejected user %s, check_item=%d, counter=%d",
+		DEBUG2("rlm_sqlcounter: Rejected user %s, check_item=%d, counter=%u",
 				key_vp->strvalue,check_vp->lvalue,counter);
 	}
 
