Commit d5dbdb27 authored by Chris's avatar Chris Committed by Corey Hulen

several one-line panic, race, and logic fixes (#7766)

parent 9d32cd42
......@@ -45,11 +45,11 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
fileData := fileArray[0]
file, err := fileData.Open()
defer file.Close()
if err != nil {
c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
buf := bytes.NewBuffer(nil)
io.Copy(buf, file)
......
......@@ -386,10 +386,14 @@ func importTeam(c *Context, w http.ResponseWriter, r *http.Request) {
}
importFromArray, ok := r.MultipartForm.Value["importFrom"]
if !ok || len(importFromArray) < 1 {
c.Err = model.NewAppError("importTeam", "api.team.import_team.no_import_from.app_error", nil, "", http.StatusBadRequest)
return
}
importFrom := importFromArray[0]
fileSizeStr, ok := r.MultipartForm.Value["filesize"]
if !ok {
if !ok || len(fileSizeStr) < 1 {
c.Err = model.NewAppError("importTeam", "api.team.import_team.unavailable.app_error", nil, "", http.StatusBadRequest)
return
}
......@@ -414,11 +418,11 @@ func importTeam(c *Context, w http.ResponseWriter, r *http.Request) {
fileInfo := fileInfoArray[0]
fileData, err := fileInfo.Open()
defer fileData.Close()
if err != nil {
c.Err = model.NewAppError("importTeam", "api.team.import_team.open.app_error", nil, err.Error(), http.StatusBadRequest)
return
}
defer fileData.Close()
var log *bytes.Buffer
switch importFrom {
......
......@@ -156,8 +156,8 @@ func listCommands(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
commands := []*model.Command{}
err := &model.AppError{}
var commands []*model.Command
var err *model.AppError
if customOnly {
if !c.App.SessionHasPermissionToTeam(c.Session, teamId, model.PERMISSION_MANAGE_SLASH_COMMANDS) {
c.SetPermissionError(model.PERMISSION_MANAGE_SLASH_COMMANDS)
......
......@@ -315,11 +315,11 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
fileData := fileArray[0]
file, err := fileData.Open()
defer file.Close()
if err != nil {
c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
buf := bytes.NewBuffer(nil)
io.Copy(buf, file)
......
......@@ -623,10 +623,14 @@ func importTeam(c *Context, w http.ResponseWriter, r *http.Request) {
}
importFromArray, ok := r.MultipartForm.Value["importFrom"]
if !ok || len(importFromArray) < 1 {
c.Err = model.NewAppError("importTeam", "api.team.import_team.no_import_from.app_error", nil, "", http.StatusBadRequest)
return
}
importFrom := importFromArray[0]
fileSizeStr, ok := r.MultipartForm.Value["filesize"]
if !ok {
if !ok || len(fileSizeStr) < 1 {
c.Err = model.NewAppError("importTeam", "api.team.import_team.unavailable.app_error", nil, "", http.StatusBadRequest)
return
}
......@@ -651,11 +655,11 @@ func importTeam(c *Context, w http.ResponseWriter, r *http.Request) {
fileInfo := fileInfoArray[0]
fileData, err := fileInfo.Open()
defer fileData.Close()
if err != nil {
c.Err = model.NewAppError("importTeam", "api.team.import_team.open.app_error", nil, err.Error(), http.StatusBadRequest)
return
}
defer fileData.Close()
var log *bytes.Buffer
switch importFrom {
......
......@@ -44,6 +44,9 @@ func (cfg *AutoPostCreator) UploadTestFile() ([]string, bool) {
path, _ := utils.FindDir("web/static/images")
file, err := os.Open(path + "/" + filename)
if err != nil {
return nil, false
}
defer file.Close()
data := &bytes.Buffer{}
......
......@@ -1185,7 +1185,7 @@ func (a *App) ViewChannel(view *model.ChannelView, userId string, clearPushNotif
}
}
times := map[string]int64{}
var times map[string]int64
if result := <-uchan; result.Err != nil {
return nil, result.Err
} else {
......
......@@ -259,10 +259,10 @@ func (a *App) UploadFiles(teamId string, channelId string, userId string, fileHe
for i, fileHeader := range fileHeaders {
file, fileErr := fileHeader.Open()
defer file.Close()
if fileErr != nil {
return nil, model.NewAppError("UploadFiles", "api.file.upload_file.bad_parse.app_error", nil, fileErr.Error(), http.StatusBadRequest)
}
defer file.Close()
buf := bytes.NewBuffer(nil)
io.Copy(buf, file)
......
......@@ -36,10 +36,10 @@ func WriteSamlFile(fileData *multipart.FileHeader) *model.AppError {
}
file, err := fileData.Open()
defer file.Close()
if err != nil {
return model.NewAppError("AddSamlCertificate", "api.admin.add_certificate.open.app_error", nil, err.Error(), http.StatusInternalServerError)
}
defer file.Close()
configDir, _ := utils.FindDir("config")
out, err := os.Create(configDir + filename)
......
......@@ -774,10 +774,10 @@ func (a *App) GetProfileImage(user *model.User) ([]byte, bool, *model.AppError)
func (a *App) SetProfileImage(userId string, imageData *multipart.FileHeader) *model.AppError {
file, err := imageData.Open()
defer file.Close()
if err != nil {
return model.NewAppError("SetProfileImage", "api.user.upload_profile_user.open.app_error", nil, err.Error(), http.StatusBadRequest)
}
defer file.Close()
// Decode image config first to check dimensions before loading the whole thing into memory later on
config, _, err := image.DecodeConfig(file)
......
......@@ -46,7 +46,7 @@ func jobserverCmdF(cmd *cobra.Command, args []string) {
a.Jobs.StartSchedulers()
}
var signalChan chan os.Signal = make(chan os.Signal)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
......
......@@ -144,7 +144,7 @@ func runServer(configFileLocation string) {
// wait for kill signal before attempting to gracefully shutdown
// the running service
c := make(chan os.Signal)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-c
......
......@@ -77,7 +77,7 @@ func serverForWebClientTestsCmdF(cmd *cobra.Command, args []string) error {
wsapi.Init(a, a.Srv.WebSocketRouter)
a.UpdateConfig(setupClientTests)
c := make(chan os.Signal)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-c
......
......@@ -2171,6 +2171,10 @@
"id": "api.team.import_team.parse.app_error",
"translation": "Could not parse multipart form"
},
{
"id": "api.team.import_team.no_import_from.app_error",
"translation": "Malformed request: importFrom field is not present."
},
{
"id": "api.team.import_team.unavailable.app_error",
"translation": "Malformed request: filesize field is not present."
......
......@@ -138,18 +138,16 @@ func manualTest(c *api.Context, w http.ResponseWriter, r *http.Request) {
// Grab the test ID and pick the test
testname, ok := params["test"]
var err2 *model.AppError
if !ok {
c.Err = model.NewAppError("/manual", "manaultesting.manual_test.parse.app_error", nil, "", http.StatusBadRequest)
return
}
switch testname[0] {
case "autolink":
err2 = testAutoLink(env)
c.Err = testAutoLink(env)
// ADD YOUR NEW TEST HERE!
case "general":
err2 = nil
}
if err != nil {
c.Err = err2
return
}
}
......
......@@ -46,7 +46,6 @@ func userFromGitLabUser(glu *GitLabUser) *model.User {
} else {
user.FirstName = glu.Name
}
strings.TrimSpace(user.Email)
user.Email = glu.Email
userId := strconv.FormatInt(glu.Id, 10)
user.AuthData = &userId
......
......@@ -104,10 +104,10 @@ func ReadFile(path string) ([]byte, *model.AppError) {
}
bucket := Cfg.FileSettings.AmazonS3Bucket
minioObject, err := s3Clnt.GetObject(bucket, path)
defer minioObject.Close()
if err != nil {
return nil, model.NewAppError("ReadFile", "api.file.read_file.s3.app_error", nil, err.Error(), http.StatusInternalServerError)
}
defer minioObject.Close()
if f, err := ioutil.ReadAll(minioObject); err != nil {
return nil, model.NewAppError("ReadFile", "api.file.read_file.s3.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
......
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