Repository: syncthing/syncthing
File: lib/db/transactions.go (Go)

Code before:
	// Update global size counter if necessary

	if !globalChanged {
		// Neither the global state nor the needs of any devices, except
		// the one updated, changed.
		return keyBuf, nil
	}

	// Remove the old global from the global size counter
	if haveOldGlobal {
		if !gotOldGlobal {
			if keyBuf, oldGlobal, err = t.getGlobalFromFileVersion(keyBuf, folder, name, true, oldGlobalFV); err != nil {
				return nil, err
			}
		}
		// Remove the old global from the global size counter
		meta.removeFile(protocol.GlobalDeviceID, oldGlobal)
	}

	// Add the new global to the global size counter
	if !gotGlobal {
		if globalFV.Version.Equal(file.Version) {
			// The inserted file is the global file
			global = file
		} else {
			keyBuf, global, _ = t.getGlobalFromFileVersion(keyBuf, folder, name, true, globalFV)
		}
		gotGlobal = true
	}
	meta.addFile(protocol.GlobalDeviceID, global)

	// check for local (if not already done before)
	if !bytes.Equal(device, protocol.LocalDeviceID[:]) {
		localFV, haveLocal := fl.Get(protocol.LocalDeviceID[:])
		needBefore := haveOldGlobal && Need(oldGlobalFV, haveLocal, localFV.Version)
		needNow := Need(globalFV, haveLocal, localFV.Version)
		if needBefore {
			meta.removeNeeded(protocol.LocalDeviceID, oldGlobal)
			if !needNow {
				if keyBuf, err = t.updateLocalNeed(keyBuf, folder, name, false); err != nil {
					return nil, err
				}
			}
		}
		if needNow {
			meta.addNeeded(protocol.LocalDeviceID, global)
			if !needBefore {
				if keyBuf, err = t.updateLocalNeed(keyBuf, folder, name, true); err != nil {
					return nil, err
				}
			}

Review comment (bug):
I suspect that we should in fact have some sort of error handling here, so the bug is larger than just unused retval

Code after:
	// Update global size counter if necessary

	if !globalChanged {
		// Neither the global state nor the needs of any devices, except
		// the one updated, changed.
		return keyBuf, nil
	}

	// Remove the old global from the global size counter
	if haveOldGlobal {
		if !gotOldGlobal {
			if keyBuf, oldGlobal, err = t.getGlobalFromFileVersion(keyBuf, folder, name, true, oldGlobalFV); err != nil {
				return nil, err
			}
		}
		// Remove the old global from the global size counter
		meta.removeFile(protocol.GlobalDeviceID, oldGlobal)
	}

	// Add the new global to the global size counter
	if !gotGlobal {
		if globalFV.Version.Equal(file.Version) {
			// The inserted file is the global file
			global = file
		} else {
			keyBuf, global, err = t.getGlobalFromFileVersion(keyBuf, folder, name, true, globalFV)
			if err != nil {
				return nil, err
			}
		}
		gotGlobal = true
	}
	meta.addFile(protocol.GlobalDeviceID, global)

	// check for local (if not already done before)
	if !bytes.Equal(device, protocol.LocalDeviceID[:]) {
		localFV, haveLocal := fl.Get(protocol.LocalDeviceID[:])
		needBefore := haveOldGlobal && Need(oldGlobalFV, haveLocal, localFV.Version)
		needNow := Need(globalFV, haveLocal, localFV.Version)
		if needBefore {
			meta.removeNeeded(protocol.LocalDeviceID, oldGlobal)
			if !needNow {
				if keyBuf, err = t.updateLocalNeed(keyBuf, folder, name, false); err != nil {
					return nil, err
				}
			}
		}
		if needNow {
			meta.addNeeded(protocol.LocalDeviceID, global)
			if !needBefore {
				if keyBuf, err = t.updateLocalNeed(keyBuf, folder, name, true); err != nil {