smartnode/telebot

too frequent fatal errors

Closed this issue · 8 comments

my bot stops working many times. sometimes even stops after a minute after starting.
most errors are:
free(): invalid pointer (I don't use free() in my code)
double free or corruption (out)
unable to get forwarded from

this is the bot code if you want

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <telebot.h>

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
int lines = 0;
int line = 0;
char texts[10][10000];
int main(int argc, char *argv[])
{
	printf("Welcome to surveillancebot\n");
	FILE *fp = fopen(".token", "r");
	if (fp == NULL)
	{
		printf("Failed to open .token file\n");
		return -1;
	}
	FILE *fptr = fopen(".warnings", "rb");
	if(fptr == NULL)
	{
		printf("Failed to open .warnings file\n");
		return -1;
	}
	fseek(fptr, 0, SEEK_END);
	long int size = ftell(fptr);
	fseek(fptr, 0, SEEK_SET);
	char tmpdst;
	int ch = 0;
	for(long int curs = 0; curs < size; curs++)
	{
		fread(&tmpdst, 1, 1, fptr);
		if(tmpdst != '\n')
		{
			texts[lines][ch] = tmpdst;
			ch++;
		}
		else
		{
			lines++;
			ch = 0;
		}
	}
	fclose(fptr);
	char token[1024];
	if (fscanf(fp, "%s", token) == 0)
	{
		printf("Failed to read token\n");
		fclose(fp);
		return -1;
	}
	fclose(fp);

	telebot_handler_t handle;
	if(telebot_create(&handle, token) != TELEBOT_ERROR_NONE)
	{
		printf("Telebot create failed\n");
		return -1;
	}

	telebot_user_t me;
	if(telebot_get_me(handle, &me) != TELEBOT_ERROR_NONE)
	{
		printf("Failed to get bot information\n");
		telebot_destroy(handle);
		return -1;
	}

	printf("ID: %d\n", me.id);
	printf("First Name: %s\n", me.first_name);
	printf("User Name: %s\n", me.username);

	telebot_put_me(&me);

	int index, count, offset = -1;
	telebot_error_e ret;
	telebot_message_t message;
	telebot_update_type_e update_types[] = {TELEBOT_UPDATE_TYPE_MESSAGE};

	while(1)
	{
		telebot_update_t *updates;
		ret = telebot_get_updates(handle, offset, 20, 0, update_types, 0, &updates, &count);
		if (ret != TELEBOT_ERROR_NONE)
			continue;
		for (index = 0; index < count; index++)
		{
			message = updates[index].message;
			if(message.text || message.caption)
			{
				char* mess;
				if(message.text)
				{
					mess = message.text;
				}
				else
				{
					mess = message.caption;
				}
				char newmess[22000];
				int ind, i = 0;
				for(ind = 0; mess[ind] != '\0'; ind++)
				{
					if(mess[ind] != '\'' && mess[ind] != '"')
					{
						newmess[i] = mess[ind];
						i++;
					}
				}
				newmess[i] = '\0';
				char cmd[22009];
				cmd[0] = '\0';
				strcat(cmd, "filter '");
				strcat(cmd, newmess);
				strcat(cmd, "'");
				if(system(cmd))
				{
					char* str;
					str = texts[line];
					line = (line + 1) % lines;
					ret = telebot_send_message(handle, message.chat->id, str, "HTML", false, false, updates[index].message.message_id, "");
				}
				if(ret != TELEBOT_ERROR_NONE)
				{
					printf("Failed to send message: %d \n", ret);
				}
			}
			offset = updates[index].update_id + 1;
		}
		telebot_put_updates(updates, count);
		sleep(5);
	}
	telebot_destroy(handle);
	return 0;
}

Can you run the binary with gdb and provide backtrace?

this is the first error I got in the startup:
`
[ERROR][telebot_parser_get_message:491]Failed to get from message object
[ERROR][telebot_parser_get_message:491]Failed to get from message object
[Detaching after vfork from child process 5733]
[Detaching after vfork from child process 5734]
[Detaching after vfork from child process 5735]
[Detaching after vfork from child process 5736]
[Detaching after vfork from child process 5737]
[Detaching after vfork from child process 5738]
[Detaching after vfork from child process 5739]
[Detaching after vfork from child process 5740]
[Detaching after vfork from child process 5741]
[Detaching after vfork from child process 5742]
[Detaching after vfork from child process 5743]
[Detaching after vfork from child process 5744]
[Detaching after vfork from child process 5745]
[Detaching after vfork from child process 5746]
[Detaching after vfork from child process 5747]
[Detaching after vfork from child process 5748]
[Detaching after vfork from child process 5749]
[Detaching after vfork from child process 5750]
free(): invalid pointer

Thread 1 "hisb" received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
`

[ERROR][telebot_parser_get_message:491]Failed to get from message object
[Detaching after vfork from child process 21293]
munmap_chunk(): invalid pointer
--Type for more, q to quit, c to continue without paging--

Thread 1 "hisb" received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.

[ERROR][telebot_parser_get_message:491]Failed to get from message object
[Detaching after vfork from child process 22149]
double free or corruption (out)

Thread 1 "hisb" received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.

is it possible that you didn't update the id holder when telegram increased id integer size?

Telebot is based on Telegram Bot API 4.7, after that I did not have time to update the code.
Probably size of the id is the problem. I cannot be sure.

It appears to be the exact issue I faced in #60.
I experienced the same [ERROR][telebot_parser_get_message:491]Failed to get <forward from> from message object before a crash.
This means that the issue is already fixed in the latest commit.