- Added more checks if char buffers would be too small.

This commit is contained in:
Juha Heinanen
2019-06-04 20:13:38 +03:00
parent 400a0b774e
commit 50b320967d
+48 -31
View File
@@ -108,8 +108,9 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
{ {
const char *event; const char *event;
char event_buf[256]; char event_buf[256];
char ua_buf[256]; char ua_buf[32];
char call_buf[256]; char call_buf[32];
int len;
struct player *player = baresip_player(); struct player *player = baresip_player();
@@ -119,45 +120,45 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
case UA_EVENT_REGISTERING: case UA_EVENT_REGISTERING:
case UA_EVENT_UNREGISTERING: case UA_EVENT_UNREGISTERING:
case UA_EVENT_REGISTER_OK: case UA_EVENT_REGISTER_OK:
re_snprintf(event_buf, sizeof event_buf, "%s", ua_event_reg_str(ev)); len = re_snprintf(event_buf, sizeof event_buf, "%s", ua_event_reg_str(ev));
break; break;
case UA_EVENT_REGISTER_FAIL: case UA_EVENT_REGISTER_FAIL:
re_snprintf(event_buf, sizeof event_buf, "registering failed,%s", prm); len = re_snprintf(event_buf, sizeof event_buf, "registering failed,%s", prm);
break; break;
case UA_EVENT_CALL_INCOMING: case UA_EVENT_CALL_INCOMING:
if (list_count(ua_calls(ua)) > 1) { if (list_count(ua_calls(ua)) > 1) {
play = mem_deref(play); play = mem_deref(play);
(void)play_file(&play, player, "callwaiting.wav", 3); (void)play_file(&play, player, "callwaiting.wav", 3);
} }
re_snprintf(event_buf, sizeof event_buf, "%s", "call incoming"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "call incoming");
break; break;
case UA_EVENT_CALL_RINGING: case UA_EVENT_CALL_RINGING:
play = mem_deref(play); play = mem_deref(play);
(void)play_file(&play, player, "ringback.wav", -1); (void)play_file(&play, player, "ringback.wav", -1);
re_snprintf(event_buf, sizeof event_buf, "%s", "call ringing"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "call ringing");
break; break;
case UA_EVENT_CALL_PROGRESS: case UA_EVENT_CALL_PROGRESS:
re_snprintf(event_buf, sizeof event_buf, "%s", "call progress"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "call progress");
break; break;
case UA_EVENT_CALL_ESTABLISHED: case UA_EVENT_CALL_ESTABLISHED:
play = mem_deref(play); play = mem_deref(play);
re_snprintf(event_buf, sizeof event_buf, "%s", "call established"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "call established");
break; break;
case UA_EVENT_CALL_MENC: case UA_EVENT_CALL_MENC:
if (prm[0] == '0') if (prm[0] == '0')
re_snprintf(event_buf, sizeof event_buf, "call secure"); len = re_snprintf(event_buf, sizeof event_buf, "call secure");
else if (prm[0] == '1') else if (prm[0] == '1')
re_snprintf(event_buf, sizeof event_buf, "call verify,%s", prm+2); len = re_snprintf(event_buf, sizeof event_buf, "call verify,%s", prm+2);
else if (prm[0] == '2') else if (prm[0] == '2')
re_snprintf(event_buf, sizeof event_buf, "call verified,%s", prm+2); len = re_snprintf(event_buf, sizeof event_buf, "call verified,%s", prm+2);
else else
re_snprintf(event_buf, sizeof event_buf, "%s", "unknown menc event"); len = re_snprintf(event_buf, sizeof event_buf, "%s", "unknown menc event");
break; break;
case UA_EVENT_CALL_TRANSFER: case UA_EVENT_CALL_TRANSFER:
re_snprintf(event_buf, sizeof event_buf, "call transfer,%s", prm); len = re_snprintf(event_buf, sizeof event_buf, "call transfer,%s", prm);
break; break;
case UA_EVENT_CALL_TRANSFER_FAILED: case UA_EVENT_CALL_TRANSFER_FAILED:
re_snprintf(event_buf, sizeof event_buf, "transfer failed,%s", prm); len = re_snprintf(event_buf, sizeof event_buf, "transfer failed,%s", prm);
break; break;
case UA_EVENT_CALL_CLOSED: case UA_EVENT_CALL_CLOSED:
play = mem_deref(play); play = mem_deref(play);
@@ -168,10 +169,10 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
(void)play_file(&play, player, tone, 1); (void)play_file(&play, player, tone, 1);
} }
} }
re_snprintf(event_buf, sizeof event_buf, "call closed,%s", prm); len = re_snprintf(event_buf, sizeof event_buf, "call closed,%s", prm);
break; break;
case UA_EVENT_MWI_NOTIFY: case UA_EVENT_MWI_NOTIFY:
re_snprintf(event_buf, sizeof event_buf, "mwi notify,%s", prm); len = re_snprintf(event_buf, sizeof event_buf, "mwi notify,%s", prm);
break; break;
case UA_EVENT_AUDIO_ERROR: case UA_EVENT_AUDIO_ERROR:
mem_deref(call); mem_deref(call);
@@ -179,6 +180,12 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
default: default:
goto out; goto out;
} }
if (len == -1) {
LOGE("failed to print event to buffer\n");
goto out;
}
event = event_buf; event = event_buf;
BaresipContext *pctx = (BaresipContext*)(&g_ctx); BaresipContext *pctx = (BaresipContext*)(&g_ctx);
@@ -221,7 +228,10 @@ static void message_handler(struct ua *ua, const struct pl *peer, const struct p
LOGD("got message '%.*s' from peer '%.*s'", (int)mbuf_get_left(body), mbuf_buf(body), LOGD("got message '%.*s' from peer '%.*s'", (int)mbuf_get_left(body), mbuf_buf(body),
(int)peer->l, peer->p); (int)peer->l, peer->p);
if (snprintf(peer_buf, 256, "%.*s", (int)peer->l, peer->p) >= 256) {
LOGE("message peer is too long (max 255 charcaters)\n");
return;
}
BaresipContext *pctx = (BaresipContext*)(&g_ctx); BaresipContext *pctx = (BaresipContext*)(&g_ctx);
JavaVM *javaVM = pctx->javaVM; JavaVM *javaVM = pctx->javaVM;
JNIEnv *env; JNIEnv *env;
@@ -238,7 +248,6 @@ static void message_handler(struct ua *ua, const struct pl *peer, const struct p
"(Ljava/lang/String;Ljava/lang/String;[B)V"); "(Ljava/lang/String;Ljava/lang/String;[B)V");
sprintf(ua_buf, "%lu", (unsigned long)ua); sprintf(ua_buf, "%lu", (unsigned long)ua);
jstring javaUA = (*env)->NewStringUTF(env, ua_buf); jstring javaUA = (*env)->NewStringUTF(env, ua_buf);
sprintf(peer_buf, "%.*s", (int)peer->l, peer->p);
jstring javaPeer = (*env)->NewStringUTF(env, peer_buf); jstring javaPeer = (*env)->NewStringUTF(env, peer_buf);
jbyteArray javaMsg; jbyteArray javaMsg;
size = mbuf_get_left(body); size = mbuf_get_left(body);
@@ -448,7 +457,7 @@ Java_com_tutpro_baresip_BaresipService_baresipStart(JNIEnv *env, jobject instanc
} }
LOGD("adding %u accounts", list_count(uag_list())); LOGD("adding %u accounts", list_count(uag_list()));
char ua_buf[256]; char ua_buf[32];
struct ua *ua; struct ua *ua;
for (le = list_head(uag_list()); le != NULL; le = le->next) { for (le = list_head(uag_list()); le != NULL; le = le->next) {
ua = le->data; ua = le->data;
@@ -670,6 +679,7 @@ Java_com_tutpro_baresip_AccountKt_account_1audio_1codec(JNIEnv *env, jobject thi
(*env)->ReleaseStringUTFChars(env, javaAcc, native_acc); (*env)->ReleaseStringUTFChars(env, javaAcc, native_acc);
const struct list *codecl; const struct list *codecl;
char codec_buf[32]; char codec_buf[32];
int len;
struct le *le; struct le *le;
if (acc) { if (acc) {
codecl = account_aucodecl(acc); codecl = account_aucodecl(acc);
@@ -680,7 +690,11 @@ Java_com_tutpro_baresip_AccountKt_account_1audio_1codec(JNIEnv *env, jobject thi
if (i > ix) break; if (i > ix) break;
if (i == ix) { if (i == ix) {
const struct aucodec *ac = le->data; const struct aucodec *ac = le->data;
re_snprintf(codec_buf, sizeof codec_buf, "%s/%u/%u", ac->name, ac->srate, ac->ch); len = re_snprintf(codec_buf, sizeof codec_buf, "%s/%u/%u", ac->name, ac->srate, ac->ch);
if (len == -1) {
LOGE("failed to print audio codec to buffer\n");
codec_buf[0] = '\0';
}
break; break;
} }
} }
@@ -938,10 +952,15 @@ Java_com_tutpro_baresip_Api_ua_1alloc(JNIEnv *env, jobject thiz, jstring javaUri
struct ua *ua; struct ua *ua;
LOGD("allocating UA '%s'\n", uri); LOGD("allocating UA '%s'\n", uri);
int res = ua_alloc(&ua, uri); int res = ua_alloc(&ua, uri);
(*env)->ReleaseStringUTFChars(env, javaUri, uri); char ua_buf[32];
char ua_buf[64];
ua_buf[0] = '\0'; ua_buf[0] = '\0';
if (res == 0) sprintf(ua_buf, "%lu", (unsigned long)ua); if (res == 0) {
sprintf(ua_buf, "%lu", (unsigned long)ua);
LOGD("allocated ua '%lu'\n", (unsigned long)ua);
} else {
LOGE("failed to allocate ua '%s'\n", uri);
}
(*env)->ReleaseStringUTFChars(env, javaUri, uri);
return (*env)->NewStringUTF(env, ua_buf); return (*env)->NewStringUTF(env, ua_buf);
} }
@@ -951,6 +970,7 @@ Java_com_tutpro_baresip_Api_ua_1register(JNIEnv *env, jobject thiz, jstring java
const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0); const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0);
struct ua *ua = (struct ua *)strtoul(native_ua, NULL, 10); struct ua *ua = (struct ua *)strtoul(native_ua, NULL, 10);
(*env)->ReleaseStringUTFChars(env, javaUA, native_ua); (*env)->ReleaseStringUTFChars(env, javaUA, native_ua);
LOGD("registering UA '%s'\n", native_ua);
return ua_register(ua); return ua_register(ua);
} }
@@ -977,6 +997,7 @@ Java_com_tutpro_baresip_Api_ua_1update_1account(JNIEnv *env, jobject thiz, jstri
{ {
const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0); const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0);
struct ua *ua = (struct ua *)strtoul(native_ua, NULL, 10); struct ua *ua = (struct ua *)strtoul(native_ua, NULL, 10);
LOGD("updating account of ua %s\n", native_ua);
(*env)->ReleaseStringUTFChars(env, javaUA, native_ua); (*env)->ReleaseStringUTFChars(env, javaUA, native_ua);
return ua_update_account(ua); return ua_update_account(ua);
} }
@@ -997,7 +1018,7 @@ Java_com_tutpro_baresip_Api_ua_1account(JNIEnv *env, jobject thiz, jstring javaU
const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0); const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0);
struct ua *ua = (struct ua *)strtoul(native_ua, NULL, 10); struct ua *ua = (struct ua *)strtoul(native_ua, NULL, 10);
struct account *acc; struct account *acc;
char acc_buf[64]; char acc_buf[32];
(*env)->ReleaseStringUTFChars(env, javaUA, native_ua); (*env)->ReleaseStringUTFChars(env, javaUA, native_ua);
acc_buf[0] = '\0'; acc_buf[0] = '\0';
if (ua) { if (ua) {
@@ -1072,7 +1093,7 @@ Java_com_tutpro_baresip_Api_ua_1connect(JNIEnv *env, jobject thiz, jstring javaU
int err; int err;
const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0); const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0);
const char *native_uri = (*env)->GetStringUTFChars(env, javaURI, 0); const char *native_uri = (*env)->GetStringUTFChars(env, javaURI, 0);
char call_buf[64]; char call_buf[32];
LOGD("connecting ua %s to %s\n", native_ua, native_uri); LOGD("connecting ua %s to %s\n", native_ua, native_uri);
ua = (struct ua *)strtoul(native_ua, NULL, 10); ua = (struct ua *)strtoul(native_ua, NULL, 10);
re_thread_enter(); re_thread_enter();
@@ -1097,7 +1118,7 @@ Java_com_tutpro_baresip_Api_ua_1call_1alloc(JNIEnv *env, jobject thiz, jstring j
int err; int err;
const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0); const char *native_ua = (*env)->GetStringUTFChars(env, javaUA, 0);
const char *native_xcall = (*env)->GetStringUTFChars(env, javaXCall, 0); const char *native_xcall = (*env)->GetStringUTFChars(env, javaXCall, 0);
char call_buf[64]; char call_buf[32];
LOGD("allocating new call for ua %s xcall %s\n", native_ua, native_xcall); LOGD("allocating new call for ua %s xcall %s\n", native_ua, native_xcall);
ua = (struct ua *)strtoul(native_ua, NULL, 10); ua = (struct ua *)strtoul(native_ua, NULL, 10);
xcall = (struct call *)strtoul(native_xcall, NULL, 10); xcall = (struct call *)strtoul(native_xcall, NULL, 10);
@@ -1236,8 +1257,6 @@ Java_com_tutpro_baresip_Api_call_1audio_1codecs(JNIEnv *env, jobject thiz, jstri
if (len == -1) { if (len == -1) {
LOGE("failed to get audio codecs of call %s\n", native_call); LOGE("failed to get audio codecs of call %s\n", native_call);
codec_buf[0] = '\0'; codec_buf[0] = '\0';
} else {
codec_buf[len] = '\0';
} }
(*env)->ReleaseStringUTFChars(env, javaCall, native_call); (*env)->ReleaseStringUTFChars(env, javaCall, native_call);
return (*env)->NewStringUTF(env, codec_buf); return (*env)->NewStringUTF(env, codec_buf);
@@ -1249,9 +1268,7 @@ Java_com_tutpro_baresip_Api_call_1status(JNIEnv *env, jobject thiz, jstring java
struct call *call = (struct call *)strtoul(native_call, NULL, 10); struct call *call = (struct call *)strtoul(native_call, NULL, 10);
char status_buf[256]; char status_buf[256];
int len = re_snprintf(&(status_buf[0]), 255, "%H", call_status, call); int len = re_snprintf(&(status_buf[0]), 255, "%H", call_status, call);
if (len != -1) { if (len == -1) {
status_buf[len] = '\0';
} else {
LOGE("failed to get status of call %s\n", native_call); LOGE("failed to get status of call %s\n", native_call);
status_buf[0] = '\0'; status_buf[0] = '\0';
} }